QtMVC架构
【1】代理应用示例源码
用代码说事,比较靠谱。
代码目录:三个自定义类,重实现QStyledItemDelegate类。main函数应用示例。
(1)ComboDelegate.h #ifndef COMBODELEGATE_H #define COMBODELEGATE_H #include class ComboDelegate : public QStyledItemDelegate { public: ComboDelegate(QObject *parent = NULL); protected: 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
(2)ComboDelegate.cpp #include "ComboDelegate.h" #include ComboDelegate::ComboDelegate(QObject *parent) : QStyledItemDelegate(parent) {} QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); Q_UNUSED(index); QStringList list; list << "工人" << "农民" << "军人" << "律师"; QComboBox *pEditor = new QComboBox(parent); pEditor->addItems(list); pEditor->installEventFilter(const_cast(this)); return pEditor; } void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString strText = index.model()->data(index).toString(); QComboBox *pCombox = NULL; pCombox = static_cast(editor); if (pCombox != NULL) { int nIndex = pCombox->findText(strText); pCombox->setCurrentIndex(nIndex); } } void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const { QComboBox *pCombox = NULL; pCombox = static_cast(editor); if (pCombox != NULL) { QString strText = pCombox->currentText(); model->setData(index, strText); } } void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)const { Q_UNUSED(index); editor->setGeometry(option.rect); }
(3)DateDelegate.h #ifndef DATEDELEGATE_H #define DATEDELEGATE_H #include class DateDelegate : public QStyledItemDelegate { public: DateDelegate(QObject *parent = NULL); protected: 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 // DATEDELEGATE_H
(4)DateDelegate.cpp #include "DateDelegate.h" #include DateDelegate::DateDelegate(QObject *parent) : QStyledItemDelegate(parent) {} // 首先创建要进行代理的窗体 QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); Q_UNUSED(index); QDateTimeEdit *pEditor = new QDateTimeEdit(parent); // 一个日历的控件 pEditor->setDisplayFormat("yyyy-MM-dd"); // 日期时间的显示格式 pEditor->setCalendarPopup(true); // 以下拉的方式显示 pEditor->installEventFilter(const_cast(this)); // 调用这个函数安装事件过滤器,使这个对象可以捕获QDateTimeEdit对象的事件 return pEditor; } // 这个是初始化作用,初始化代理控件的数据 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { // 先用这个index返回这个model然后用这个model得到index对应的数据 QString strDate = index.model()->data(index).toString(); QDate date = QDate::fromString(strDate, Qt::ISODate); // 根据QString类型得到相应的时间类型 QDateTimeEdit *pEditor = NULL; pEditor = static_cast(editor); // 强转为QDateTimeEdit*类型 if (pEditor != NULL) { pEditor->setDate(date); // 设置代理控件的显示数据 } } // 将代理控件里面的数据更新到视图控件中 // void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QDateTimeEdit *pEditor = NULL; pEditor = static_cast(editor); // 得到时间 if (pEditor != NULL) { QDate date = pEditor->date(); // 得到时间 model->setData(index, QVariant(date.toString(Qt::ISODate))); // 把值放到相应的index里面 } } // 代理中数据的改变放到model中 // void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index); editor->setGeometry(option.rect); }
(5)SpinDelegate.h #ifndef SPINDELEGATE_H #define SPINDELEGATE_H #include class SpinDelegate : public QStyledItemDelegate { public: SpinDelegate(QObject *parent = NULL); protected: 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 // SPINDELEGATE_H
(6)SpinDelegate.cpp #include "SpinDelegate.h" #include SpinDelegate::SpinDelegate(QObject *parent) : QStyledItemDelegate(parent) { } QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); Q_UNUSED(index); QSpinBox *pEditor = new QSpinBox(parent); pEditor->setRange(0, 30000); pEditor->installEventFilter(const_cast(this)); return pEditor; } void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { int value = index.model()->data(index).toInt(); QSpinBox *pSpinbox = NULL; pSpinbox = static_cast(editor); if (pSpinbox != NULL) { pSpinbox->setValue(value); } } void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QSpinBox *pSpinbox = NULL; pSpinbox = static_cast(editor); if (pSpinbox != NULL) { int value = pSpinbox->value(); model->setData(index, value); } } void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(index); editor->setGeometry(option.rect); }
(7)main.cpp #include #include #include #include #include #include #include #include "DateDelegate.h" #include "ComboDelegate.h" #include "SpinDelegate.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QStandardItemModel model(4, 4); model.setHeaderData(0, Qt::Horizontal, QLatin1String("Name")); model.setHeaderData(1, Qt::Horizontal, QLatin1String("Birthday")); model.setHeaderData(2, Qt::Horizontal, QLatin1String("Job")); model.setHeaderData(3, Qt::Horizontal, QLatin1String("Income")); QFile file(QLatin1String("/mnt/liuy/info")); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "open the file failed..."; return -1; } QTextStream out(&file); QString line; model.removeRows(0, model.rowCount(QModelIndex()), QModelIndex()); int row = 0; do { line = out.readLine(); if (!line.isEmpty()) { model.insertRows(row, 1, QModelIndex()); QStringList pieces = line.split(",", QString::SkipEmptyParts); model.setData(model.index(row, 0, QModelIndex()), pieces.value(0)); model.setData(model.index(row, 1, QModelIndex()), pieces.value(1)); model.setData(model.index(row, 2, QModelIndex()), pieces.value(2)); model.setData(model.index(row, 3, QModelIndex()), pieces.value(3)); ++row; } } while(!line.isEmpty()); file.close(); QTableView tableView; tableView.setModel(&model); // 绑定Model tableView.setWindowTitle(QLatin1String("Delegate")); DateDelegate dateDelegate; tableView.setItemDelegateForColumn(1, &dateDelegate); // 第一列代理 ComboDelegate comboDelegate; tableView.setItemDelegateForColumn(2, &comboDelegate);// 第二列代理 SpinDelegate spinDelegate; tableView.setItemDelegateForColumn(3, &spinDelegate); // 第三列代理 tableView.resize(500, 300); // 重置大小 tableView.show(); return a.exec(); }
备注:此示例运行环境为UBuntu + Qt5.3.2
【2】读取的信息文件
文件info 内容如下(注意:文件格式): Liu,1977-01-05,工人,1500 Wang,1987-11-25,医生,2500 Sun,1967-10-05,军人,500 Zhang,1978-01-12,律师,4500
【3】运行效果图
运行效果图:
第二列编辑图(时间日期控件):
第三列编辑图(下拉框控件):
第四列编辑图(微调框控件):
如何顺利当妈,听听过来人怎么说(建议收藏)本周姐妹茶话会话题是科学备孕指南这期的茶话会,请妈妈们给正在为备孕迷茫的姐妹一些建议吧科学备孕应该怎么备,有哪些事一定要做,有哪些备孕期的好物?一起来看看大家是怎么说的吧(作者娃娃
只要你想生娃就尽管生,结不结婚并不重要!四川省开始放大招了,四川省生育登记服务管理办法将于2月15日起施行。主要修订以下四方面内容四川生育登记取消结婚限制取消办理生育登记时生育数量的限制简化生育登记的要求增加信息共享的要
为什么孕期容易出现口腔问题?孕期的激素变化一般来说孕妈妈受到孕激素的变化抵抗力减弱的同时牙齿会产生充血,血管增生等现象,有些孕妈妈甚至会严重到牙龈肿胀发炎。孕期饮食习惯的改变怀孕后孕妈妈会特别容易饿,同时也会
想从源头解决便秘问题?需要辩证,看看你是哪种类型便秘是现在很多人都感到困扰的问题,虽不是什么大病,但也难以根治。很多人都知道,香蕉西瓜绿豆等食物都有排毒通便的功效,但有些常便秘的人,吃多了以后就渐渐发现失效了,这时你就要考虑自己
一天该喝多少水?怎么喝才刚刚好?霞姨来到神经外科,问道医生,我觉得头很痛,很胀,实在很不舒服。医师问从什么时候开始的呢?我一个月前听了某大师说,我们要多喝水净化身心,水是经过大师加持过的,一天要喝一万毫升。惠姨说
健康饮食的基础健康饮食需做到膳食均衡,膳食均衡的基本原则是食物多样化。古代黄帝内经早就提出五谷为养五果为助五畜为益五菜为充的多样化饮食原则。中国居民膳食指南也推荐平均每天摄入12种以上食物,每周
看王天一特大的微博后送牢骚太盛防肠断风物长宜放眼量共勉在王郑2023十番棋争霸赛以流产的方式落下帷幕时,王天一特大曾在直播间发言呼吁大家不要再攻击郑惟桐,他认为该事件持续发酵,势必会影响中国象棋的发展,对谁都没有好处。他还强调希望粉丝
女排姐妹花贡献31分!拦网17比6碾压对手,巴西主攻空砍26分20222023赛季的巴西女排超级联赛第十二轮结束,具备争夺实力的乌贝兰迪亚海滩3比1击败了巴西利亚,海滩队的多米尼加攻手马丁内斯姐妹联手砍下31分,在拦网环节17比6大比分领先对
波津MSG中有孩子们穿我球衣他们试图与我击掌ampampamp告诉我重回纽约直播吧1月19日讯今日NBA常规赛,奇才在客场116105战胜尼克斯。赛后,奇才球员波尔津吉斯接受了记者采访。谈到面对旧主尼克斯队,波津讲道好多了,对我的嘘声也少了,我想是球迷们对
美国国际贸易委员会正式对OLED面板及其组件启动337调查科技战略美国国际贸易委员会正式对OLED面板及其组件启动337调查据中国贸易救济信息网1月27日消息,美国国际贸易委员会(ITC)投票决定对特定用于移动设备的有源矩阵有机发光二极管
田富达新政协会议上最年轻的代表台湾民主自治同盟的优秀领导人,高山族人民的杰出代表,台湾民主自治同盟中央委员会原副主席,中国共产党的优秀党员田富达同志因病于2023年1月19日在北京逝世,享年93岁。田富达见证了