一.最终效果

二.实现思路

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. 用JQ实现的一个简单轮播

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>lb ...

  2. jQuery基本toggle() toggleClass() 使用

    今天来学习一下jQuery的基本函数的使用,很简单. 首先写一个button做控制按钮,然后写一个div用按钮控制idv做动画,从而测试JQuery的动画函数 <head> <met ...

  3. vue实现多级弹窗

    webpack + vue 实现 弹窗功能 对于刚入门webpack + vue 不久的新人来说,这技术,确实有些不太友好,相比较于直接操纵dom元素的jQuery,直接操纵数据的 vue 在webp ...

  4. php的基础知识(三)

    12.函数: 函数的功能: 定义:在真实的项目开发过程中,有些代码会重复利用,我们可以把它提出来,做成公共的代码,供团队来使用,这个我们封装的代码段,就是函数(功能). 优点: 1.提高代码的利用率. ...

  5. 大数据学习--day06(Eclipse、数组)

    Eclipse.数组 Eclipse 的基本设置   调节控制条字体大小. Window -> Preferences -> General -> Appearance -> ...

  6. swig与python

    当你觉得python慢的时候,当你的c/c++代码难以用在python上的时候,你可能会注意这篇文章.swig是一个可以把c/c++代码封装为python库的工具.(本文封装为python3的库) 文 ...

  7. SQL条件判断中字符串后面有空格的问题

    也不知何时才有的概念,还是以前一直没有注意,从哪也没有听说过的定义,今天又遇见了一个小坑,特记录下来,防止再陷坑! 才疏学浅,文笔有限,简单点说吧,就是在写SQL Server语句时,以前使用了 WH ...

  8. 北京Uber优步司机奖励政策(3月27日)

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

  9. 西安Uber优步司机奖励政策(12月21日-12.27日)

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

  10. vijos p1027休息中的小呆

    休息中的小呆 描述 当大家在考场中接受考验(折磨?)的时候,小呆正在悠闲(欠扁)地玩一个叫“最初梦想”的游戏.游戏描述的是一个叫pass的有志少年在不同的时空穿越对抗传说中的大魔王chineseson ...