一.最终效果

二.实现思路

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

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

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

4.updateEditorGeometrey()不变;

代码如下:

comboboxDelegate.h

 #ifndef COMBODELEGATE_H
#define COMBODELEGATE_H #include <QItemDelegate> class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:
ComboDelegate(QObject *parent = ); 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; }; #endif // COMBODELEGATE_H

comboboxDelegate.cpp

 #include <QComboBox>
#include <QDebug> ComboDelegate::ComboDelegate(QObject *parent) :
QItemDelegate(parent)
{
} QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const
{ QComboBox *comboBox = new QComboBox();
comboBox->setObjectName("comboBox"); //为该对象设置名字,否则后面使用findchild()函数会出错
//editor->lineEdit()->setAlignment(Qt::AlignCenter);
comboBox->setEditable(true);
//editor->setStyleSheet("QComboBox{border:1px solid gray;}""QComboBox QAbstractItemView::item{height:25px;}"); //editor->setView(new QListView());
comboBox->addItem("N");
comboBox->addItem("m");
comboBox->addItem("m/s");
comboBox->installEventFilter(const_cast<ComboDelegate*>(this)); QLabel *label = new QLabel;
label->setObjectName("label");
label->setText(tr("m/s")); QHBoxLayout *hLay = new QHBoxLayout;
hLay->addWidget(comboBox);
hLay->addWidget(label); QWidget *wighet = new QWidget(parent);
wighet->setLayout(hLay);
return wighet;
} void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
{
//QString str =index.model()->data(index).toString();
QString str = "m";
//QString str = "meos";
QWidget *box = static_cast<QWidget*>(editor);
//QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
//QComboBox *comboBox = static_cast<QComboBox *>(box->findChild<QComboBox *>("editor"));
//int i = comboBox->findText(str);
//comboBox->setCurrentIndex(i);
//QComboBox *combo = new QComboBox(comboBox);
QLabel *label = editor->findChild<QLabel *>("label");
//label->setText(str);
qDebug("Test:%s",qPrintable(label->text()));
//label->setText(tr("1"));
//box->findChild<QComboBox *>("editor")->setCurrentIndex(i);
} void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QWidget *box = static_cast<QWidget*>(editor);
QComboBox *comboBox= box->findChild<QComboBox *>();
QString str = comboBox->currentText();
model->setData(index,str);
} void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}

三. 注意以下几个函数:

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

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

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

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

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 MYWIDGET_H ...

  2. Qt kdChart 甘特图案例

    KDChart  甘特图在Qt中的加载使用案例,代码来自官方 mainwindow.h /******************************************************* ...

  3. QT_地图导航

    //地图显示功能 #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...

  4. Qt5_简易画板_详细注释

    代码下载链接:  http://pan.baidu.com/s/1hsc41Ek 密码: 5hdg 显示效果如下: 代码附有详细注释(代码如下) /*** * 先新建QMainWindow, 项目名称 ...

  5. qt_文本编辑器实现_附带详细注释和源码下载

    源码下载: 链接: http://pan.baidu.com/s/1c21EVRy 密码: qub8 实现主要的功能有:新建,打开,保存,另存为,查找(查找的时候需要先将光标放到最下面位置才能查全,不 ...

  6. 列表框QListWidget类

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

  7. 读Qt Demo——Basic Layouts Example

    此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局: 例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\wi ...

  8. C++ GUI Qt4编写的文本编辑器

    mainwindow.h: #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMenu> #include <QAction& ...

  9. QT_校园导航(绘制路线已实现)_Updata_详细注释

    //MainWidget.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include " ...

随机推荐

  1. CentOS中的 yum upgrade 和 yum update 的区别

    通过 man yum 的帮助信息了解 yum update 和 yum upgrade: update If run without any packages, update will update ...

  2. 前台页面上传data image图片,java后台接收图片保存

    最近在项目中有这么一个需求,就是上传一个视频文件,然后要获取视频文件的第一帧图片,这个可以通过canvas获取得到,得到的是一个dataURL,之后还要将这个图片上传到云,这个时候如何操作就不清楚了, ...

  3. ubuntu下的python请求库的安装——Selenium,ChromeDriver,GeckoDriver,PhantomJS,aiohttp

    Selenium安装: pip3 install selenium ChromeDriver安装: 在这链接下载对应版本:https://chromedriver.storage.googleapis ...

  4. Linux的数据传输

    1. sz 与 rz sz:将选定的文件从本地发送(send)到远端机器 rz:运行该命令会弹出一个文件选择窗口,从本地选择文件夹,接收(receive)从远端的文件 mac 下使用 brew 安装: ...

  5. PAT (Basic Level) Practice 1032 挖掘机技术哪家强

    个人练习 为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 10​^5的正整数 N,即参赛人数 ...

  6. 007---TCP VS UDP

  7. JAVA8 Stream API的使用

    /** * @auther hhh * @date 2018/12/31 12:48 * @description Stream流:用来处理数组.集合的API * 1.不是数据结构,没有内部存储(只是 ...

  8. vim 智能提示插件 javacomplete安装

    *** 从https://github.com/vim-scripts/javacomplete网站中下载javacomplete *** 新建javacomplete-master文件夹,将java ...

  9. tomcat8.5配置高并发

    最近部署的tomcat应用,有一天压测的时候,测试一致反馈下载不了,结果查看日志才发现如下错误: INFO: Maximum number of threads (200) created for c ...

  10. 北京Uber优步司机奖励政策(2月29日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...