一.最终效果

二.实现思路

1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget;

2.setEditorData()中,通过1中返回的widget找到label,设置参数;

3.setModelData()中,通过1中返回的widget找到combobox,找到当前选中的index,将其更新到model中;

4.updateEditorGeometrey()不变;

代码如下:

comboboxDelegate.h

 1 #ifndef COMBODELEGATE_H
 2 #define COMBODELEGATE_H
 3
 4 #include <QItemDelegate>
 5
 6 class ComboDelegate : public QItemDelegate
 7 {
 8     Q_OBJECT
 9 public:
10     ComboDelegate(QObject *parent = 0);
11
12     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex    &index) const;
13     void setEditorData(QWidget *editor, const QModelIndex &index) const;
14     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
15     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const  QModelIndex &index) const;
16
17 };
18
19 #endif // COMBODELEGATE_H

comboboxDelegate.cpp

 1 #include <QComboBox>
 2 #include <QDebug>
 3
 4 ComboDelegate::ComboDelegate(QObject *parent) :
 5 QItemDelegate(parent)
 6 {
 7 }
 8
 9 QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
10 {
11
12     QComboBox *comboBox = new QComboBox();
13     comboBox->setObjectName("comboBox");    //为该对象设置名字,否则后面使用findchild()函数会出错
14     //editor->lineEdit()->setAlignment(Qt::AlignCenter);
15     comboBox->setEditable(true);
16     //editor->setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:25px;}");
17
18     //editor->setView(new QListView());
19     comboBox->addItem("N");
20     comboBox->addItem("m");
21     comboBox->addItem("m/s");
22     comboBox->installEventFilter(const_cast<ComboDelegate*>(this));
23
24     QLabel *label = new QLabel;
25     label->setObjectName("label");
26     label->setText(tr("m/s"));
27
28     QHBoxLayout *hLay = new QHBoxLayout;
29     hLay->addWidget(comboBox);
30     hLay->addWidget(label);
31
32     QWidget *wighet = new QWidget(parent);
33     wighet->setLayout(hLay);
34     return wighet;
35 }
36
37 void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
38 {
39     //QString str =index.model()->data(index).toString();
40     QString str = "m";
41     //QString str = "meos";
42     QWidget *box = static_cast<QWidget*>(editor);
43     //QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
44     //QComboBox *comboBox = static_cast<QComboBox *>(box->findChild<QComboBox *>("editor"));
45     //int i = comboBox->findText(str);
46     //comboBox->setCurrentIndex(i);
47     //QComboBox *combo = new QComboBox(comboBox);
48     QLabel *label = editor->findChild<QLabel *>("label");
49     //label->setText(str);
50     qDebug("Test:%s",qPrintable(label->text()));
51     //label->setText(tr("1"));
52     //box->findChild<QComboBox *>("editor")->setCurrentIndex(i);
53 }
54
55 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
56 {
57     QWidget *box = static_cast<QWidget*>(editor);
58     QComboBox *comboBox= box->findChild<QComboBox *>();
59     QString str = comboBox->currentText();
60     model->setData(index,str);
61 }
62
63 void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
64 {
65     editor->setGeometry(option.rect);
66 }

三. 注意以下几个函数:

1.comboBox->setObjectName("comboBox");

2.QWidget *box = static_cast<QWidget*>(editor);

3.QLabel *label = editor->findChild<QLabel *>("label");

4.QComboBox *comboBox= box->findChild<QComboBox *>();

转载于:https://www.cnblogs.com/wang-kai/p/6566124.html

Delegate(QLabel和QComboBox)相关推荐

  1. 2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl

     1.新建一个空项目(其它项目 -> 空 QT 项目): 2  添加新文件(选择C++Class) MyWidget.h #ifndef MYWIDGET_H #define MYWIDGE ...

  2. QComboBox 设置下拉列表颜色

    1.在 QComboBox 的父 Widget 中设置 QComboBox 的样式 QComboBox{background-color: rgb(255, 255, 255);border:1px ...

  3. Python 图形界面框架 PyQt5 使用指南!

    作者:钱魏Way https://www.biaodianfu.com/pyqt5.html 使用Python开发图形界面的软件其实并不多,相对于GUI界面,可能Web方式的应用更受人欢迎.但对于像我 ...

  4. Qt 第二章 创建对话框--纯代码实现改变形状的对话框(二)

    如果我们想用代码生成改变形状对话框怎么实现,琢磨着试着一步步实现,用Designer设计出来的改变形状对话框如下图所示: 从上面初步了解到,创建改变形状对话框是由PushButton.GroupBox ...

  5. 列表框QListWidget类

    QListWidget类也是GUI中常用的类,它从QListView下派生: class Q_GUI_EXPORT QListWidget : public QListView {Q_OBJECT 常 ...

  6. python PyQt5中文教程☞【第八节】PyQt5控件(II)

    引用文章:http://code.py40.com/pyqt5/ 在这里我们将继续介绍PyQt5控件.我们将介绍QPixmap.QLineEdit QSplitter,QComboBox. QPixm ...

  7. QT读写Sqlite数据库三种方式

    QT对一些基本的数据库的访问封装,可谓是极大的方便的我们开发人员,现在我们就来说下QT对Sqlite这个数据库的读写,Sqlite是一个比较小型的本地数据库,对于保存一些软件配置参数或量不是很大的数据 ...

  8. Qt实践录:常见控件操作示例1

    本文记录QT常见控件的操作示例.包括:QPushBotton.QLabel.QComboBox.QSlider.QSpinBox.编辑框(QLineEdit/QPlainTextEdit/QTextE ...

  9. Python 利用PyQt5写一个简易的串口助手

    学习单片机,STM32好长时间了,也做了一些项目,一直想利用上位机实现电脑与单片机之间的传输数据,利用串口助手是最直接的通信方式,但串口助手不适合做上位机,如果会写串口助手的软件,基本能写自己写一个上 ...

最新文章

  1. 【转载】Linux命令-自动挂载文件/etc/fstab功能详解[转]
  2. 程序语言基础:解释程序基本原理笔记
  3. 非正弦周期电流电路和信号的频谱
  4. jQuery实现一个图片左右滚动
  5. VMware 虚拟机安装OSX el capitan 11.12
  6. Android开发笔记(八十八)同步与加锁
  7. mysql 独享表空间_Mysql 独享表空间
  8. python 匿名函数 与 重要的内置函数
  9. 使用MVC2模式创建新闻网站
  10. python 高斯烟羽模型_高斯扩散模型-高斯烟羽大气污染扩散模型
  11. 进销存excel_简单易操作!Excel免费进销存管理系统!不花钱的进销存管理软件...
  12. 知道PDF密码,想要移除如何操作?
  13. 微信支付商户平台,企业付款,企业向个人付款接口总结
  14. Linux高级命令find,grep,sed,awk
  15. 名悦集团:开电动车会比开燃油车省钱吗?
  16. Python用socket、多线程实现一对一聊天室
  17. iOS耳机红外线遥控器
  18. Javascript-蔬菜运算价格
  19. eclipse 3.7 中英文自由切换
  20. python图灵_python图灵机器人

热门文章

  1. rabbitmq java集群_RabbitMQ集群整合SpringBoot2.x
  2. mysql第四章表单查询样题_查询mysql表单中前10条,然后在li中循环输出。
  3. wpf 加载资源html,从资源文件加载WPF样式
  4. 基于php的应用程序,基于PHP的Web应用程序和群发邮件
  5. poi插入图片浮于文字上方_Word插入手写签名
  6. 从零入门 Serverless | SAE 的远程调试和云端联调
  7. java 删除文件失败_java 文件删除失败(被进程占用)
  8. java文件头_对java文件头的解析
  9. 冒险者传说pc6java_冒险者传说
  10. 全局样式_CAD新手福利:不懂标注样式修改的请进来一看