Delegate(QLabel和QComboBox)
一.最终效果

二.实现思路
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)的更多相关文章
- 2.QLabel,QPushButton,QLineEdit,QComboBox,QCheckBox,QRadioButton,QTextEdit,QTextBrowser,QGroupBox,QSl
1.新建一个空项目(其它项目->空QT项目): 2 添加新文件(选择C++Class) MyWidget.h #ifndef MYWIDGET_H #define MYWIDGET_H ...
- Qt kdChart 甘特图案例
KDChart 甘特图在Qt中的加载使用案例,代码来自官方 mainwindow.h /******************************************************* ...
- QT_地图导航
//地图显示功能 #ifndef MAPWIDGET_H #define MAPWIDGET_H #include <QGraphicsView> #include <QLabel& ...
- Qt5_简易画板_详细注释
代码下载链接: http://pan.baidu.com/s/1hsc41Ek 密码: 5hdg 显示效果如下: 代码附有详细注释(代码如下) /*** * 先新建QMainWindow, 项目名称 ...
- qt_文本编辑器实现_附带详细注释和源码下载
源码下载: 链接: http://pan.baidu.com/s/1c21EVRy 密码: qub8 实现主要的功能有:新建,打开,保存,另存为,查找(查找的时候需要先将光标放到最下面位置才能查全,不 ...
- 列表框QListWidget类
QListWidget类也是GUI中常用的类,它从QListView下派生: class Q_GUI_EXPORT QListWidget : public QListView { Q_OBJECT ...
- 读Qt Demo——Basic Layouts Example
此例程主要展示用代码方式创建控件并用Layout管理类对其进行布局: 例程来自Qt5.2,如过是默认安装,代码位于:C:\Qt\Qt5.2.0\5.2.0\mingw48_32\examples\wi ...
- C++ GUI Qt4编写的文本编辑器
mainwindow.h: #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMenu> #include <QAction& ...
- QT_校园导航(绘制路线已实现)_Updata_详细注释
//MainWidget.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include " ...
随机推荐
- Redis学习笔记(一)
定义 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库. 从该定义中抽出几个关键信息,以表示Redis的特性: 存储结构:key-val ...
- eclipse 安装 lombok
转载自http://bbs.itmayiedu.com/article/1527769518449 由于项目中有 @Slf4j 注解等,而 eclipse 需要安装 lombok 插件才能正常编译.由 ...
- MPP调研
一.MMP数据库 MPP是massively parallel processing,一般指使用多个SQL数据库节点搭建的数据仓库系统.执行查询的时候,查询可以分散到多个SQL数据库节点上执行,然后汇 ...
- 隐藏Windows不常用设置项
Windows10的设置里面有很多我们不想看见的项目,例如"轻松使用","隐私","游戏","Cortana"等,我们可 ...
- IOS和Android系统区别详解
IOS系统(非开源,不可扩展) iphone沙盒机制解释:应用程序位于文件系统的严格限制部分,程序不能直接访问其他应用程序. 1.iOS的编程语言Objective-C 2.IOS采用的是沙盒运行机制 ...
- Go正则处理
Go语言通过regexp标准包为正则表达式提供了官方支持 包中有三个函数判定是否匹配,匹配返回true,否则返回false,这三个函数只是输入源不同 func Match(pattern string ...
- python类的多态
1. 什么是多态 多态指的是同一种/类事物的不同形态 2. 为何要用多态 多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象 多态性的精髓:统一 ...
- CSS 兼容iPhone X、iPhone XS及iPhone XR
@media only screen and (device-width: 375px) and (device-height: 812px) and (-webkit-device-pixel-ra ...
- 【转】Odoo装饰器: one装饰
one装饰器的作用是对每一条记录都执行对应的方法,相当于traditional-style中的function,无返回值! 应用举例: 定义的columns now = fields.Datetime ...
- POJ2431_Expedition_KEY
题目传送门 由题目可得,在一条路上有N个加油站,在距离终点a[i](细节)的位置上,你需要通过长度为L的路,油箱的容量是无限的,但是初始只有P点油,经过每个加油站时可以选择加b[i]的油,问最少加油几 ...