一.最终效果

二.实现思路

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. Django学习笔记2

    1.BookInfo.objects.all() objects:是Manager类型的对象,用于与数据库进行交互 当定义模型类时没有指定管理器,则Django会为模型类提供一个名为objects的管 ...

  2. Django学习之mysql增删改查

    上节介绍了如何使用命令行操作mysql增删改查,现在介绍如何使用python管理mysql 使用pip 下载完mysql后,mysql会以pymysql模块的形式存储在pycharm的包文件里.我们通 ...

  3. c语言:矩阵相乘-矩阵相加 新手练习1

    #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> voi ...

  4. vim 粘贴文本,格式混乱 tab

    粘贴的代码如上.修改方法: 方法一: set paste 贴完后,设置 set nopaste 恢复代码缩进. 方法二:修改配置文件 vim /etc/vim/vimrc set pastetoggl ...

  5. Tomcat7 调优及 JVM 参数优化

      Tomcat 的缺省配置是不能稳定长期运行的,也就是不适合生产环境,它会死机,让你不断重新启动,甚至在午夜时分唤醒你.对于操作系统优化来说,是尽可能的增大可使用的内存容量.提高CPU 的频率,保证 ...

  6. 2 进程multiprocessing [mʌltɪ'prəʊsesɪŋ] time模块

    1.multiprocessing模块 multiprocessing模块就是跨平台版本的多进程模块. multiprocessing模块提供了一个Process类来代表一个进程对象, 2.Proce ...

  7. 3991: [SDOI2015]寻宝游戏

    3991: [SDOI2015]寻宝游戏 https://www.lydsy.com/JudgeOnline/problem.php?id=3991 分析: 虚树+set. 要求树上许多点之间的路径的 ...

  8. springboot整合kafka应用

    1.kafka在消息传递的使用非常普遍,相对于activemq来说kafka的分布式管理和使用更加灵活. 2.activemq的搭建和使用可以参考: activemq搭建和springmvc的整合:h ...

  9. hdu2147kiki's game(找规律)

    kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)Total ...

  10. Linux命令应用大词典-第23章 进程和服务管理

    23.1 ps:报告当前进程的快照 23.2 top:显示当前正在运行的进程 23.3 pgrep:按名称和其他属性查找进程 23.4 pidof:查找正在运行的进程的进程号 23.5 pstree: ...