怎么用QTableView修改数据库内容

2024-12-04 08:27:52
推荐回答(1个)
回答1:

这个需要重写一些model的方法

当然你需要编辑也要实现自己的delegate

class MyTableDelgate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MyTableDelgate(QObject *parent = 0);

protected:
//basic function for a read-only delegate, you can draw anything with the painter
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

//edit 5 function
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

private:
QLineEdit * m_LineEdit;
};

model

class MyTableDelgate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit MyTableDelgate(QObject *parent = 0);

protected:
//basic function for a read-only delegate, you can draw anything with the painter
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;

//edit 5 function
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
void updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const;

};