資源簡介
QT中delegate自定義委托的小例子,有注釋說明和源碼
簡單基礎部件的委托可以繼承QItemDelegate,并使用這些函數的默認實現(xiàn),委托編輯器可以通過使用小工具來管理編輯過程或直接處理事件來實現(xiàn)。
使用Delegate的原因:Qt中當用到QTreeView和QTableView等用于顯示item的視圖時,你要編輯一個item用到的編輯工具可能是除了默認文字編輯lineEdit以外的工具,例如button,spinBox,甚至Slider,ProgressBar,也有可能是自定義的widget。所以Qt提供了一個委托類,用來處理View中的數據展示方式

代碼片段和文件信息
#pragma?execution_character_set(“utf-8“)
#include?“comboxdelegate.h“
#include?
ComBoxDelegate::ComBoxDelegate(Qobject?*parent)
????:QItemDelegate(parent)
{???????
}
//創(chuàng)建委托控件
QWidget?*ComBoxDelegate::createEditor(QWidget?*parent?const?QstyleOptionViewItem?&option?const?QModelIndex?&index)?const
{
????Q_UNUSED(index);
????Q_UNUSED(option);
????QComboBox?*box?=?new?QComboBox(parent);
????//設置下拉列表的選線內容
????QStringList?strList;
????strList<
????box->addItems(strList);
????return?box;
}
//設置控件數據
void?ComBoxDelegate::setEditorData(QWidget?*editorconst?QModelIndex?&index)?const
{
????//獲取單元格內容
????QString?str?=?index.data().toString();
????QComboBox?*box?=?static_cast(editor);
????int?currentIndex?=?box->findText(str);
????if(currentIndex?>=?0)
????{
????????box->setCurrentIndex(currentIndex);
????}
}
//設置模型數據
void?ComBoxDelegate::setModelData(QWidget?*editorQAbstractItemModel?*modelconst?QModelIndex?&index)?const
{
????//獲取編輯控件的內容
????QString?str?=?static_cast(editor)->currentText();
????//設置模型數據
????model->setData(indexstr);
}
//設置控件位置
void?ComBoxDelegate::updateEditorGeometry(QWidget?*editorconst?QstyleOption?&optionconst?QModelIndex?&index)
{
????Q_UNUSED(index);
????//更新控件位置大小
????editor->setGeometry(option.rect);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1560??2020-07-08?11:27??CustomDelegate\comboxdelegate.cpp
?????文件????????796??2020-03-20?16:08??CustomDelegate\comboxdelegate.h
?????文件????????545??2020-03-22?15:23??CustomDelegate\CustomDelegate.pro
?????文件???????2217??2020-03-20?15:25??CustomDelegate\datetimeeditdelegate.cpp
?????文件???????1018??2020-03-22?14:45??CustomDelegate\datetimeeditdelegate.h
?????文件????????114??2020-03-20?10:16??CustomDelegate\dialog.cpp
?????文件????????195??2020-03-20?10:16??CustomDelegate\dialog.h
?????文件???????2752??2020-03-22?23:21??CustomDelegate\main.cpp
?????文件?????????87??2020-03-20?10:18??CustomDelegate\res.qrc
?????文件???????1327??2020-03-23?12:55??CustomDelegate\spinboxdelegate.cpp
?????文件????????803??2020-03-22?22:54??CustomDelegate\spinboxdelegate.h
?????文件????????106??2015-03-26?13:50??CustomDelegate\test.txt
?????目錄??????????0??2020-09-18?16:19??CustomDelegate
-----------?---------??----------?-----??----
????????????????11520????????????????????13
評論
共有 條評論