Qt—MVC架構


【1】代理應用示例源碼

用代碼說事,比較靠譜。

代碼目錄:三個自定義類,重實現QStyledItemDelegate類。main函數應用示例。

(1)ComboDelegate.h

 1 #ifndef COMBODELEGATE_H  2 #define COMBODELEGATE_H
 3 
 4 #include <QStyledItemDelegate>
 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 <QComboBox>
 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<ComboDelegate*>(this)); 19     return pEditor; 20 } 21 
22 void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
23 { 24     QString strText = index.model()->data(index).toString(); 25     QComboBox *pCombox = NULL; 26     pCombox = static_cast<QComboBox*>(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)const
35 { 36     QComboBox *pCombox = NULL; 37     pCombox = static_cast<QComboBox*>(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)const
46 { 47  Q_UNUSED(index); 48     editor->setGeometry(option.rect); 49 }

(3)DateDelegate.h

 1 #ifndef DATEDELEGATE_H  2 #define DATEDELEGATE_H
 3 
 4 #include <QStyledItemDelegate>
 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 <QDateTimeEdit>
 3 
 4 DateDelegate::DateDelegate(QObject *parent)  5  : QStyledItemDelegate(parent)  6 {}  7 
 8 // 首先創建要進行代理的窗體
 9 QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
10 { 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<DateDelegate*>(this));  // 調用這個函數安裝事件過濾器,使這個對象可以捕獲QDateTimeEdit對象的事件
18     return pEditor; 19 } 20 
21 // 這個是初始化作用,初始化代理控件的數據
22 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
23 { 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<QDateTimeEdit*>(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) const
38 { 39   QDateTimeEdit *pEditor = NULL; 40   pEditor = static_cast<QDateTimeEdit*>(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) const
51 { 52  Q_UNUSED(index); 53     editor->setGeometry(option.rect); 54 }

(5)SpinDelegate.h

 1 #ifndef SPINDELEGATE_H  2 #define SPINDELEGATE_H
 3 
 4 #include <QStyledItemDelegate>
 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 <QSpinBox>
 4 
 5 SpinDelegate::SpinDelegate(QObject *parent)  6  : QStyledItemDelegate(parent)  7 {  8 }  9 
10 QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
11 { 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<SpinDelegate*>(this)); 18     return pEditor; 19 } 20 
21 void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
22 { 23     int value = index.model()->data(index).toInt(); 24     QSpinBox *pSpinbox = NULL; 25     pSpinbox = static_cast<QSpinBox*>(editor); 26     if (pSpinbox != NULL) 27  { 28         pSpinbox->setValue(value); 29  } 30 } 31 
32 void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
33 { 34     QSpinBox *pSpinbox = NULL; 35     pSpinbox = static_cast<QSpinBox*>(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) const
44 { 45  Q_UNUSED(index); 46 
47     editor->setGeometry(option.rect); 48 }

(7)main.cpp

 1 #include <QApplication>
 2 #include <QFile>
 3 #include <QDebug>
 4 #include <QWidget>
 5 #include <QTableView>
 6 #include <QTextStream>
 7 #include <QStandardItemModel>
 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     do
34  { 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); // 綁定Model
51     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,工人,1500
Wang,1987-11-25,醫生,2500
Sun,1967-10-05,軍人,500
Zhang,1978-01-12,律師,4500

 

【3】運行效果圖

運行效果圖:

第二列編輯圖(時間日期控件):

第三列編輯圖(下拉框控件):

第四列編輯圖(微調框控件):

 

Good Good Study, Day Day Up.

順序 選擇 循環 總結


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM