Qt—MVC架构_天天微头条

【1】代理应用示例源码


(资料图片)

用代码说事,比较靠谱。

代码目录:三个自定义类,重实现QStyledItemDelegate类。main函数应用示例。

(1)ComboDelegate.h

1 #ifndef COMBODELEGATE_H 2 #define COMBODELEGATE_H 3  4 #include 5  6 class ComboDelegate : public QStyledItemDelegate 7 { 8 public: 9     ComboDelegate(QObject *parent = NULL);10 11 protected:12     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;13     void setEditorData(QWidget *editor, const QModelIndex &index) const;14     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;15     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;16 };17 18 #endif // COMBODELEGATE_H

(2)ComboDelegate.cpp

1 #include "ComboDelegate.h" 2 #include 3  4 ComboDelegate::ComboDelegate(QObject *parent) 5     : QStyledItemDelegate(parent) 6 {} 7  8 QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 9 {10     Q_UNUSED(option);11     Q_UNUSED(index);12 13     QStringList list;14     list << "工人" << "农民" << "军人" << "律师";15 16     QComboBox *pEditor = new QComboBox(parent);17     pEditor->addItems(list);18     pEditor->installEventFilter(const_cast(this));19     return pEditor;20 }21 22 void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const23 {24     QString strText = index.model()->data(index).toString();25     QComboBox *pCombox = NULL;26     pCombox = static_cast(editor);27     if (pCombox != NULL)28     {29         int nIndex = pCombox->findText(strText);30         pCombox->setCurrentIndex(nIndex);31     }32 }33 34 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const35 {36     QComboBox *pCombox = NULL;37     pCombox = static_cast(editor);38     if (pCombox != NULL)39     {40         QString strText = pCombox->currentText();41         model->setData(index, strText);42     }43 }44 45 void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)const46 {47     Q_UNUSED(index);48     editor->setGeometry(option.rect);49 }

(3)DateDelegate.h

1 #ifndef DATEDELEGATE_H 2 #define DATEDELEGATE_H 3  4 #include 5  6 class DateDelegate : public QStyledItemDelegate 7 { 8 public: 9     DateDelegate(QObject *parent = NULL);10 11 protected:12     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;13     void setEditorData(QWidget *editor, const QModelIndex &index) const;14     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;15     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;16 };17 18 #endif // DATEDELEGATE_H

(4)DateDelegate.cpp

1 #include "DateDelegate.h" 2 #include 3  4 DateDelegate::DateDelegate(QObject *parent) 5     : QStyledItemDelegate(parent) 6 {} 7  8 // 首先创建要进行代理的窗体 9 QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const10 {11     Q_UNUSED(option);12     Q_UNUSED(index);13 14     QDateTimeEdit *pEditor = new QDateTimeEdit(parent);      // 一个日历的控件15     pEditor->setDisplayFormat("yyyy-MM-dd");   // 日期时间的显示格式16     pEditor->setCalendarPopup(true);   // 以下拉的方式显示17     pEditor->installEventFilter(const_cast(this));  // 调用这个函数安装事件过滤器,使这个对象可以捕获QDateTimeEdit对象的事件18     return pEditor;19 }20 21 // 这个是初始化作用,初始化代理控件的数据22 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const23 {24   // 先用这个index返回这个model然后用这个model得到index对应的数据25   QString strDate = index.model()->data(index).toString();26   QDate date = QDate::fromString(strDate, Qt::ISODate);     // 根据QString类型得到相应的时间类型27   QDateTimeEdit *pEditor = NULL;28   pEditor = static_cast(editor);    // 强转为QDateTimeEdit*类型29   if (pEditor != NULL)30   {31       pEditor->setDate(date);      // 设置代理控件的显示数据32   }33 }34 35 // 将代理控件里面的数据更新到视图控件中36 // void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;37 void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const38 {39   QDateTimeEdit *pEditor = NULL;40   pEditor = static_cast(editor);    // 得到时间41   if (pEditor != NULL)42   {43       QDate date = pEditor->date();    // 得到时间44       model->setData(index, QVariant(date.toString(Qt::ISODate)));    // 把值放到相应的index里面45   }46 }47 48 // 代理中数据的改变放到model中49 // void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;50 void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const51 {52     Q_UNUSED(index);53     editor->setGeometry(option.rect);54 }

(5)SpinDelegate.h

1 #ifndef SPINDELEGATE_H 2 #define SPINDELEGATE_H 3  4 #include 5  6 class SpinDelegate : public QStyledItemDelegate 7 { 8 public: 9     SpinDelegate(QObject *parent = NULL);10 11 protected:12     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;13     void setEditorData(QWidget *editor, const QModelIndex &index) const;14     void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;15     void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;16 };17 18 #endif // SPINDELEGATE_H

(6)SpinDelegate.cpp

1 #include "SpinDelegate.h" 2  3 #include 4  5 SpinDelegate::SpinDelegate(QObject *parent) 6     : QStyledItemDelegate(parent) 7 { 8 } 9 10 QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const11 {12     Q_UNUSED(option);13     Q_UNUSED(index);14 15     QSpinBox *pEditor = new QSpinBox(parent);16     pEditor->setRange(0, 30000);17     pEditor->installEventFilter(const_cast(this));18     return pEditor;19 }20 21 void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const22 {23     int value = index.model()->data(index).toInt();24     QSpinBox *pSpinbox = NULL;25     pSpinbox = static_cast(editor);26     if (pSpinbox != NULL)27     {28         pSpinbox->setValue(value);29     }30 }31 32 void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const33 {34     QSpinBox *pSpinbox = NULL;35     pSpinbox = static_cast(editor);36     if (pSpinbox != NULL)37     {38         int value = pSpinbox->value();39         model->setData(index, value);40     }41 }42 43 void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const44 {45     Q_UNUSED(index);46 47     editor->setGeometry(option.rect);48 }

(7)main.cpp

1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include "DateDelegate.h" 9 #include "ComboDelegate.h"10 #include "SpinDelegate.h"11 12 int main(int argc, char *argv[])13 {14     QApplication a(argc, argv);15 16     QStandardItemModel model(4, 4);17     model.setHeaderData(0, Qt::Horizontal, QLatin1String("Name"));18     model.setHeaderData(1, Qt::Horizontal, QLatin1String("Birthday"));19     model.setHeaderData(2, Qt::Horizontal, QLatin1String("Job"));20     model.setHeaderData(3, Qt::Horizontal, QLatin1String("Income"));21 22     QFile file(QLatin1String("/mnt/liuy/info"));23     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))24     {25         qDebug() << "open the file failed...";26         return -1;27     }28 29     QTextStream out(&file);30     QString line;31     model.removeRows(0, model.rowCount(QModelIndex()), QModelIndex());32     int row = 0;33     do34     {35         line = out.readLine();36         if (!line.isEmpty())37         {38             model.insertRows(row, 1, QModelIndex());39             QStringList pieces = line.split(",", QString::SkipEmptyParts);40             model.setData(model.index(row, 0, QModelIndex()), pieces.value(0));41             model.setData(model.index(row, 1, QModelIndex()), pieces.value(1));42             model.setData(model.index(row, 2, QModelIndex()), pieces.value(2));43             model.setData(model.index(row, 3, QModelIndex()), pieces.value(3));44             ++row;45         }46     } while(!line.isEmpty());47     file.close();48 49     QTableView tableView;50     tableView.setModel(&model); // 绑定Model51     tableView.setWindowTitle(QLatin1String("Delegate"));52 53     DateDelegate dateDelegate;54     tableView.setItemDelegateForColumn(1, &dateDelegate); // 第一列代理55     ComboDelegate comboDelegate;56     tableView.setItemDelegateForColumn(2, &comboDelegate);// 第二列代理57     SpinDelegate spinDelegate;58     tableView.setItemDelegateForColumn(3, &spinDelegate); // 第三列代理59 60     tableView.resize(500, 300); // 重置大小61     tableView.show();62 63     return a.exec();64 }

备注:此示例运行环境为UBuntu + Qt5.3.2

【2】读取的信息文件

文件info 内容如下(注意:文件格式):

Liu,1977-01-05,工人,1500Wang,1987-11-25,医生,2500Sun,1967-10-05,军人,500Zhang,1978-01-12,律师,4500

【3】运行效果图

运行效果图:

第二列编辑图(时间日期控件):

第三列编辑图(下拉框控件):

第四列编辑图(微调框控件):

【领 QT开发教程 学习资料, 点击下方链接莬费领取↓↓ ,先码住不迷路~】

点击这里:

关键词:

推荐DIY文章
主机存在磨损或划痕风险 PICO4便携包宣布召回
穿越湖海!特斯拉Cybertruck电动皮卡可以当“船”用
vivoXFold+折叠旗舰开售 配备蔡司全焦段旗舰四摄
飞凡R7正式上市 全系标配换电架构
中兴Axon30S开售 拥有黑色蓝色两款配色
荣耀MagicBookV14 2022正式开售 搭载TOF传感器
it