Qt MVC结构之QItemDelegate介绍

QItemDelegate

当我们想重新实现一个代理时,可以子类化QItemDelegate。实现item编辑时特定的效果,比如在item编辑时我们设置一个QSpinBox返回。
创建一个QApplication项目,然后我们新增一个类,类名叫做spinboxdelegate。
<!--more-->

  1. class SpinBoxDelegate : public QItemDelegate
  2. {
  3. Q_OBJECT
  4. public:
  5. explicit SpinBoxDelegate(QObject * parent=0);
  6. QWidget * createEditor(QWidget* parent, const QStyleOptionViewItem &option,
  7. const QModelIndex &index) const override;
  8. void setEditorData(QWidget *editor, const QModelIndex &index) const override;
  9. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
  10. void updateEditorGeometry(QWidget *editor,
  11. const QStyleOptionViewItem &option,
  12. const QModelIndex &index) const override;
  13. };

SpinBoxDelegate类中声明了几个函数,这些函数在QItemDelegate继承而来,通过重写实现我们自己定义的代理功能。
createEditor函数是在item被双击后进入编辑状态时触发的,返回一个QWidget控件用来管理编辑。
setModelData是在item被修改后触发的,将改动的内容写入model中。
setEditorData是在item被双击进入编辑状态时,将model的内容写入editor中。
updateEditorGeometry是刷新editor的矩形区域,因为随着item变大或者拉伸,它的区域也要随之刷新。

具体实现

1 创建editor, 返回一个spinbox

  1. QWidget * SpinBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option,
  2. const QModelIndex &index) const{
  3. QSpinBox * editor = new QSpinBox(parent);
  4. editor->setMinimum(0);
  5. editor->setMaximum(100);
  6. return editor;
  7. }

2 在编辑状态时,将model的数据写入editor

  1. void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
  2. int value = index.model()->data(index, Qt::EditRole).toInt();
  3. QSpinBox * spinBox = static_cast<QSpinBox*>(editor);
  4. spinBox->setValue(value);
  5. }

3 编辑完成时将editor的内容写入model

  1. void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
  2. QSpinBox * spinBox = static_cast<QSpinBox*>(editor);
  3. spinBox->interpretText();
  4. int value = spinBox->value();
  5. model->setData(index, value, Qt::EditRole);
  6. }

4 刷新矩形区域

  1. void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
  2. const QStyleOptionViewItem &option,
  3. const QModelIndex &index) const {
  4. editor->setGeometry(option.rect);
  5. }

接下来我们在MainWindow的构造函数里创建两个model和view,我们对其中的一个view使用我们自定义的delegate

  1. MainWindow::MainWindow(QWidget *parent) :
  2. QMainWindow(parent),
  3. ui(new Ui::MainWindow)
  4. {
  5. ui->setupUi(this);
  6. QStandardItemModel * model = new QStandardItemModel(7,4,this);
  7. for(int row=0; row < 7; row++){
  8. for(int column = 0; column < 4; column++){
  9. QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));
  10. model->setItem(row, column, item);
  11. }
  12. }
  13. QTableView* _table_view = new QTableView;
  14. _table_view->setModel(model);
  15. setCentralWidget(_table_view);
  16. this->resize(800,800);
  17. QTableView* _table_view2 = new QTableView;
  18. SpinBoxDelegate * delegate = new SpinBoxDelegate(this);
  19. QStandardItemModel * model2 = new QStandardItemModel(7,4,this);
  20. for(int row=0; row < 7; row++){
  21. for(int column = 0; column < 4; column++){
  22. QStandardItem * item = new QStandardItem(QString("%1").arg(row*4+column));
  23. model2->setItem(row, column, item);
  24. }
  25. }
  26. _table_view2->setModel(model2);
  27. _table_view2->setItemDelegate(delegate);
  28. _table_view2->show();
  29. _table_view2->resize(800,800);
  30. }

运行程序后,双击两个view的item,可以看到效果的不同
https://cdn.llfc.club/1671606628462.jpg

源码链接

源码链接
https://gitee.com/secondtonone1/qt-learning-notes

热门评论

热门文章

  1. Linux环境搭建和编码

    喜欢(594) 浏览(15420)
  2. MarkDown在线编辑器

    喜欢(514) 浏览(15636)
  3. 聊天项目(28) 分布式服务通知好友申请

    喜欢(507) 浏览(7062)
  4. vscode搭建windows C++开发环境

    喜欢(596) 浏览(97062)
  5. 使用hexo搭建个人博客

    喜欢(533) 浏览(13783)

最新评论

  1. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。
  2. 处理网络粘包问题 zyouth: //消息的长度小于头部规定的长度,说明数据未收全,则先将部分消息放到接收节点里 if (bytes_transferred < data_len) { memcpy(_recv_msg_node->_data + _recv_msg_node->_cur_len, _data + copy_len, bytes_transferred); _recv_msg_node->_cur_len += bytes_transferred; ::memset(_data, 0, MAX_LENGTH); _socket.async_read_some(boost::asio::buffer(_data, MAX_LENGTH), std::bind(&CSession::HandleRead, this, std::placeholders::_1, std::placeholders::_2, shared_self)); //头部处理完成 _b_head_parse = true; return; } 把_b_head_parse = true;放在_socket.async_read_some前面是不是更好
  3. C++ 线程池原理和实现 mzx2023:两种方法解决,一种是改排序算法,就是当线程耗尽的时候,使用普通递归,另一种是当在线程池commit的时候,判断线程是否耗尽,耗尽的话就直接当前线程执行task
  4. 利用指针和容器实现文本查询 越今朝:应该添加一个过滤功能以解决部分单词无法被查询的问题: eg: "I am a teacher."中的teacher无法被查询,因为在示例代码中teacher.被解释为一个单词从而忽略了teacher本身。
  5. 无锁并发队列 TenThousandOne:_head  和 _tail  替换为原子变量。那里pop的逻辑,val = _data[h] 可以移到循环外面吗

个人公众号

个人微信