QT学习:代理(Delegate)练习
利用Delegate設計表格中控件。
實現步驟如下。
(1)首先,加載表格數據,以便后面的操作。源文件“main.cpp”中的具體代碼如下:
(2)選擇“構建”→“構建項目"DateDelegate"”菜單項,首先按照如下圖所示的格式編輯本例所用的數據文件“test.txt”,保存在項目build-DateDelegate- Desktop_Qt_5_9_0_MinGW_32bit-Debug目錄下。
然后運行程序,效果如圖所示:
(3)在上圖中,使用手動的方式實現對生日的錄入編輯。下面使用日歷編輯框QDateTimeEdit 控件實現對生日的編輯,用自定義的Delegate來實現。
(4)DateDelegate 繼承自QItemDelegate類。頭文件“datedelegate.h”中的具體代碼如下:
(5)源文件“datedelegate.cpp”中的具體代碼如下:
#include "datedelegate.h" #include <QDateTimeEdit> DateDelegate::DateDelegate(QObject *parent):QItemDelegate(parent) { }createEditor()函數的具體實現代碼如下:
QWidget *DateDelegate::createEditor(QWidget *parent,const QStyleOptionView Item &/*option*/,const QModelIndex &/*index*/) const {QDateTimeEdit *editor = new QDateTimeEdit(parent); //新建一個QDateTimeEdit對象作為編輯時的輸入控件。editor->setDisplayFormat("yyyy-MM-dd"); //設置該QDateTimeEdit對象的顯示格式為yyyy-MM-dd,此為ISO標準顯示方式。editor->setCalendarPopup(true); //設置日歷選擇的顯示以Popup的方式,即下拉菜單方式顯示。editor->installEventFilter(const_cast<DateDelegate*>(this)); //調用QObject類的installEvent Filter()函數安裝事件過濾器,使DateDelegate能夠捕獲QDateTimeEdit對象的事件。return editor; }setEditorData()函數的具體代碼如下:
void DateDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const {QString dateStr= index.model()->data(index).toString(); //獲取指定index數據項的數據。調用QModelIndex的model()函數可獲得提供index的Model對象,data()函數返回的是一個QVariant對象,toString()函數將它轉換為一個QString類型數據。QDate date = QDate::fromString(dateStr,Qt::ISODate); //通過QDate的fromString()函數將以QString類型表示的日期數據轉換為QDate類型。Qt::ISODate表示QDate類型的日期是以ISO格式保存的,這樣最終轉換獲得的QDate數據也是ISO格式,使控件顯示與表格顯示保持一致。QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);//將editor轉換為QDateTimeEdit對象,以獲得編輯控件的對象指針。edit->setDate(date); //設置控件的顯示數據 }setModelData()函數的具體代碼如下:
void DateDelegate::setModelData(QWidget *editor,QAbstractItemModel *model, const QModelIndex &index) const {QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor); //通過緊縮轉換獲得編輯控件的對象指針。QDate date = edit->date(); //獲得編輯控件中的數據更新。model->setData(index,QVariant(date.toString(Qt::ISODate))); //調用setData()函數將數據修改更新到Model中。 }updateEditorGeometry()函數的具體代碼如下:
void DateDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option,const QModelIndex &index) const {editor->setGeometry(option.rect); }(6)在“main.cpp”文件中添加如下代碼:
#include "datedelegate.h"在語句tableView.setModel(&model);后面添加如下代碼:
DateDelegate dateDelegate; tableView.setItemDelegateForColumn(1,&dateDelegate);(7)此時運行程序,雙擊第1行第2列,將顯示如下圖所示的日歷編輯框控件。
下面使用下拉列表框QComboBox控件實現對職業類型的輸入編輯,使用自定義的Delegate實現。
(1)ComboDelegate繼承自QItemDelegate類。
頭文件“combodelegate.h”中的具體代碼如下:
(2)源文件“combodelegate.cpp”中的具體代碼如下:
#include "combodelegate.h" #include <QComboBox> ComboDelegate::ComboDelegate(QObject *parent):QItemDelegate(parent) { }createEditor()函數中創建了一個QComboBox控件,并插入可顯示的條目,安裝事件過濾器。具體代碼如下:
QWidget *ComboDelegate::createEditor(QWidget *parent,const QStyleOptionView Item &/*option*/,const QModelIndex &/*index*/) const {QComboBox *editor = new QComboBox(parent);editor->addItem("工人");editor->addItem("農民");editor->addItem("醫生");editor->addItem("律師");editor->addItem("軍人");editor->installEventFilter(const_cast<ComboDelegate*>(this));return editor; }setEditorData()函數中更新了Delegate控件中的數據顯示,具體代碼如下:
void ComboDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const {QString str =index.model()->data(index).toString();QComboBox *box = static_cast<QComboBox*>(editor);int i=box->findText(str);box->setCurrentIndex(i); }setModelData()函數中更新了Model中的數據,具體代碼如下:
void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {QComboBox *box = static_cast<QComboBox*>(editor);QString str = box->currentText();model->setData(index,str); }updateEditorGeometry()函數的具體代碼如下:
void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const {editor->setGeometry(option.rect); }在“main.cpp”文件中添加以下內容:
#include "combodelegate.h"在語句tableView.setModel(&model)的后面添加以下代碼:
ComboDelegate comboDelegate; tableView.setItemDelegateForColumn(2,&comboDelegate);此時運行程序,雙擊第1行第3列,顯示如圖所示的下拉列表:
下面使用QSpinBox控件實現對收入的輸入編輯,調用自定義的Delegate來實現。
SpinDelegate類的實現與ComboDelegate類的實現類似。
(1)頭文件“spindelegate.h”中的具體代碼如下:
(2)源文件“spindelegate.cpp”中的具體代碼如下:
#include "spindelegate.h" #include <QSpinBox> SpinDelegate::SpinDelegate(QObject *parent): QItemDelegate(parent) { }createEditor()函數的具體實現代碼如下:
QWidget *SpinDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/) const {QSpinBox *editor = new QSpinBox(parent);editor->setRange(0,10000);editor->installEventFilter(const_cast<SpinDelegate*>(this));return editor; }setEditorData()函數的具體實現代碼如下:
void SpinDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const {int value =index.model()->data(index).toInt();QSpinBox *box = static_cast<QSpinBox*>(editor);box->setValue(value); }setModelData()函數的具體實現代碼如下:
void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,const QModelIndex &index) const {QSpinBox *box = static_cast<QSpinBox*>(editor);int value = box->value();model->setData(index,value); }updateEditorGeometry()函數的具體實現代碼如下:
void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const {editor->setGeometry(option.rect); }(3)在“main.cpp”文件中添加代碼如下:
#include "spindelegate.h"在語句tableView.setModel(&model)的后面添加內容如下:
SpinDelegate spinDelegate; tableView.setItemDelegateForColumn(3,&spinDelegate);(4)此時運行程序,雙擊第1行第4列后的效果如下圖所示。
總結
以上是生活随笔為你收集整理的QT学习:代理(Delegate)练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QT学习:视图(View)练习
- 下一篇: QT学习:读写文本文件