关于QStyledItemDelegate在tableview控件中的使用心得
2016-11-01 13:17
399 查看
Qt中的委托由Delegate类来实现,Delegate类在Qt中的继承框架如下
![](https://img-blog.csdn.net/20161101130149995?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
图中可以看出QAbstractItemDelegate主要被两个类继承:QItemDelegate和QStyleItemDelegate。他们的区别是是否沿袭了用户提供的Qt style sheets。如果是使用了Qt style
sheets,选择QStyleItemDelegate类较好。
使用Delegate类时必须重载的一些函数:
(1)创建自定义widget并返回之,例如combobox,textedit等。
(2)是将model中当前有的数据设置到代理上。自己从model取出数据,自己setValue到editor上。
(3)是将editor上的数据保存到Model中。
(4)就是将editor设置到一定位置,并且有一定大小,使这个editor看起来像是正好嵌入到格子里面一样。用的是option.rect。
下面实现一个在QTableview中插入QComboBox控件的小例子。
main函数:
delegate函数
对于委托原理不是很清楚的同学,可以将每个函数下面的QMessageBox函数去注释,看一看程序到底是怎么执行的便可一目了然了。
程序运行结果
![](https://img-blog.csdn.net/20161101131505843)
图中可以看出QAbstractItemDelegate主要被两个类继承:QItemDelegate和QStyleItemDelegate。他们的区别是是否沿袭了用户提供的Qt style sheets。如果是使用了Qt style
sheets,选择QStyleItemDelegate类较好。
使用Delegate类时必须重载的一些函数:
(1)QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const; (2)void setEditorData(QWidget *editor, const QModelIndex &index) const; (3)void setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const; (4)void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &index) const;其中
(1)创建自定义widget并返回之,例如combobox,textedit等。
(2)是将model中当前有的数据设置到代理上。自己从model取出数据,自己setValue到editor上。
(3)是将editor上的数据保存到Model中。
(4)就是将editor设置到一定位置,并且有一定大小,使这个editor看起来像是正好嵌入到格子里面一样。用的是option.rect。
下面实现一个在QTableview中插入QComboBox控件的小例子。
main函数:
#include "delegate.h" #include <QApplication> #include <QHeaderView> #include <QStandardItemModel> #include <QTableView> int main(int argc, char *argv[]) { QApplication app(argc, argv); QStandardItemModel model(4, 2); QTableView tableView; tableView.setModel(&model); ComboBoxDelegate delegate; tableView.setItemDelegateForColumn(1,&delegate); tableView.horizontalHeader()->setStretchLastSection(true); model.setHorizontalHeaderLabels(QStringList() << "名字"<< "成绩"); model.setData(model.index(0, 0, QModelIndex()),QVariant("张三")); model.setData(model.index(1, 0, QModelIndex()),QVariant("赵四")); model.setData(model.index(2, 0, QModelIndex()),QVariant("李五")); model.setData(model.index(3, 0, QModelIndex()),QVariant("王六")); model.setData(model.index(0, 1, QModelIndex()),QVariant("B")); model.setData(model.index(1, 1, QModelIndex()),QVariant("C")); model.setData(model.index(2, 1, QModelIndex()),QVariant("A")); model.setData(model.index(3, 1, QModelIndex()),QVariant("B")); tableView.setWindowTitle(QObject::tr("演示程序")); tableView.show(); return app.exec(); }
delegate函数
#include "delegate.h" #include <QComboBox> #include <QMessageBox> ComboBoxDelegate::ComboBoxDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QComboBox *editor = new QComboBox(parent); editor->insertItem(0, "A"); editor->insertItem(1, "B"); editor->insertItem(2, "C"); editor->insertItem(3, "D"); editor->insertItem(4, "E"); return editor; // QMessageBox::information(NULL, "提示", "0"); } void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString value = index.model()->data(index, Qt::EditRole).toString(); QComboBox *ComboBox = static_cast<QComboBox*>(editor); ComboBox->setCurrentText(value); //QMessageBox::information(NULL, "提示", "1"); } void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *ComboBox = static_cast<QComboBox*>(editor); QString value = ComboBox->currentText(); model->setData(index, value, Qt::EditRole); // QMessageBox::information(NULL, "提示", "2"); } void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const { editor->setGeometry(option.rect); //QMessageBox::information(NULL, "提示", "3"); }
对于委托原理不是很清楚的同学,可以将每个函数下面的QMessageBox函数去注释,看一看程序到底是怎么执行的便可一目了然了。
程序运行结果
相关文章推荐
- qt利用委托:QStyledItemDelegate,实现tableview 的表格项的自定义显示,如插入图片,绘制控件
- 关于COCOS2DX里面TableView控件使用的问题
- ios关于tableview重用机制的使用心得一
- Qt tableview 使用ComboBox作为delegate时,实现单个cell中的comboBox的item动态变化以及独特性
- 关于C++Builder 中使用 VCLSkin 换肤控件的一点心得!在OCX(ActiveX)中的应用。
- ios中关于delegate(委托)的使用心得
- ios中关于delegate(委托)的使用心得
- 使用ASP.NET Atlas ItemView控件显示集合中的单个数据
- 关于ASP.NET中使用ViewState的心得。
- 关于android下面girdview item同时实现onclick和ontouch的一点心得.
- 按钮控件的 BCN_HOTITEMCHANGE 事件响应函数使用心得
- 使用ASP.NET Atlas ItemView控件显示集合中的单个数据
- ios中关于delegate(委托)的使用心得
- ios中关于delegate(委托)的使用心得
- ios中关于delegate(委托)的使用心得
- 关于VB.NET中动态设置控件坐标的使用心得休会
- ios中关于delegate(委托)的使用心得
- ios中关于delegate(委托)的使用心得
- ios中关于delegate(委托)的使用心得
- ios中关于delegate(委托)的使用心得