怎么用QTableView修改数据库内容
1个回答
2015-02-08 · 知道合伙人数码行家
关注
展开全部
这个需要重写一些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;
};
当然你需要编辑也要实现自己的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;
};
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询