資源簡介
效果看https://blog.csdn.net/Sakuya__/article/details/88895498。使用自定義委托實現在TableView中添加Combobox、Spainbox、CheckBox控件。

代碼片段和文件信息
#include?“controldelegate.h“
//spinbox控件部分
SpinboxDelegate::SpinboxDelegate(int?column)
{
????????mColumn?=?column;
}
//返回修改數據的組件,為指定的列或者行創建部件
QWidget?*SpinboxDelegate::createEditor(QWidget?*parent?const?QstyleOptionViewItem?&optionconst?QModelIndex?&index)?const
{
????if(index.column()?==?3)
????{
????????QSpinBox?*editor?=?new?QSpinBox(parent);
????????editor->setRange(0??1000);
????????connect(editorSIGNAL(editingFinished())SLOT(commitAndCloseEditor()));
????????return?editor;
????}
????else?if(?index.column()?==?4)
????{
????????QSpinBox?*editor?=?new?QSpinBox(parent);
????????editor->setRange(01000);
????????connect(editorSIGNAL(editingFinished())SLOT(commitAndCloseEditor()));
????????return?editor;
????}
????else
????{
????????return?QItemDelegate::createEditor(parentoptionindex);
????}
}
//根據editor?的數據更新model的數據
void?SpinboxDelegate::setModelData(QWidget?*editor?QAbstractItemModel?*model?const?QModelIndex?&index)?const
{
????QSpinBox?*spinBox?=?static_cast(editor);
????spinBox->interpretText();
????int?value?=?spinBox->value();
????model->setData(index?value?Qt::EditRole);
}
//顯示格式控制
void?SpinboxDelegate::paint(QPainter?*painter?const?QstyleOptionViewItem?&option?const?QModelIndex?&index)?const
{
????if(index.column()?==?3)?{
????????int?warehouseAmount?=?index.model()->data(indexQt::DisplayRole).toInt();
????????QString?text?=?QString(“%1“).arg(warehouseAmount?3?10?QChar(‘?‘));
????????QstyleOptionViewItem?myOption?=?option;
????????//設置顯示在item的中間
????????myOption.displayAlignment?=?Qt::AlignHCenter?|?Qt::AlignVCenter;
????????drawDisplay(painter?myOption?myOption.rect?text);
????????drawFocus(painter?myOption?myOption.rect);
????}
????else?if(index.column()?==?4)?{
????????int?amount?=?index.model()->data(indexQt::DisplayRole).toInt();
????????QString?text?=?QString(“%1“).arg(amount?3?10?QChar(‘?‘));
????????QstyleOptionViewItem?myOption?=?option;
????????myOption.displayAlignment?=?Qt::AlignHCenter?|?Qt::AlignVCenter;
????????drawDisplay(painter?myOption?myOption.rect?text);
????????drawFocus(painter?myOption?myOption.rect);
????}
}
void?SpinboxDelegate::commitAndCloseEditor()
{
????QSpinBox?*editor?=?qobject_cast(sender());
????emit?commitData(editor);????????????????????//當編輯器小部件完成數據編輯并希望將其寫入模型時,必須發出此信號
????emit?closeEditor(editor);???????????????????//當用戶使用指定的編輯器完成對項目的編輯時,將發出此信號
}
//checkbox部分
static?QRect?CheckBoxRect(const?QstyleOptionViewItem?&viewItemstyleOptions)/*const*/
{
????//繪制按鈕所需要的參數
????QstyleOptionButton?checkBoxstyleOption;
????//按照給定的風格參數?返回元素子區域
????QRect?checkBoxRect?=?QApplication::style()->subElementRect(?Qstyle::SE_CheckBoxIndicator?&checkBoxstyleOption);
????//返回QCheckBox坐標
????QPoint?checkBoxPoint(viewItemstyleOptions.rect.x()?+?viewItemstyleOptions.rect.width()?/?2?-?checkBoxRect.width()?/?2
?????????????????????????viewItemstyleOptions.rect.y()?+?viewItemstyle
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7059??2019-03-29?15:35??CustomDelegate\controldelegate.cpp
?????文件???????2210??2019-03-29?15:36??CustomDelegate\controldelegate.h
?????文件???????1143??2019-03-28?11:04??CustomDelegate\CustomDelegate.pro
?????文件???????4286??2019-01-28?16:25??CustomDelegate\img\heart.ico
?????文件????????101??2019-03-28?10:22??CustomDelegate\img.qrc
?????文件????????183??2019-03-28?10:20??CustomDelegate\main.cpp
?????文件???????3170??2019-03-29?15:28??CustomDelegate\mainwindow.cpp
?????文件????????810??2019-03-29?11:43??CustomDelegate\mainwindow.h
?????文件???????1177??2019-03-29?11:43??CustomDelegate\mainwindow.ui
?????目錄??????????0??2019-03-28?10:21??CustomDelegate\img
?????目錄??????????0??2019-03-29?19:35??CustomDelegate
-----------?---------??----------?-----??----
????????????????20139????????????????????11
- 上一篇:czoomview Demo
- 下一篇:用戶名和密碼爆破字典.rar
評論
共有 條評論