当然你需要编辑也要实现自己的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
}
好像有一个函数是倒序,你查查具体是那一个函数,我用过一次,不过我不建议你这么写,不知道这是你的需求,还是你只是希望在数据多的时候看到最后一行,如果是后者,你可以定义滑块的为止,这样你就直接将数据定位到你最后一行,也是可以实现的,我开始用的是倒叙输出,但是这样设计有已缺陷就是,如果你在添加一个新的数据后,他刷新数据后,第一个数据依然在第一行,看的特别不舒服。希望对你有帮助回复:Qt::BackgroundRole改成Qt::ForegroundRole,就是文字颜色.(0,5)单元格就是index.column()==5&&index.row()==0
subclass你的Model,重载data、setData函数,以data()为例:
QVariant MyTestModel::data(const QModelIndex &index, int role) const
{
switch(role)
{
case Qt::DisplayRole:
return QVariant(QString(tr("%1")).arg((index.column() + 1) * 1000 + index.row() + 1))
case Qt::BackgroundRole:
switch(index.column() % 3)
{
case 0:
return QVariant(QColor(Qt::red))
case 1:
return QVariant(QColor(Qt::green))
case 2:
return QVariant(QColor(Qt::blue))
default://only to disable warning
return QVariant(QColor(Qt::white))
}
break
default:
return QVariant()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)