资源简介
效果看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
相关资源
- 酒店管理系统基于Qt Creator5)
- vtk QT做的三维地质可视化系统2of2
- Qt局域网聊天软件
- Qt Creator opengl实现四元数鼠标控制轨迹
- QT局域网聊天系统(基于QT5.修改过)
- qt-电子点菜系统
- C 餐厅叫号系统(QT平)
- QT 实现文件下载
- qt图像处理
- QT,JPEG解码源代码(已完成)
- Qt 播放音频文件
- Qt 读取16进制的data文件
- MQTT+串口(usart)透传
- 易语言QQTEA算法源码
- VC工程转Qt工程文件的工具
- MQTT推送Demo
- 基于Linux、QT的视频监控系统的设计与
- Qt 百度地图 定位
- QT酷炫界面开发指南《QmlBook》
- Ubuntu下操作Excel,qt代码
- Qt图片浏览器 --基于Qt的Graphics View f
- qtnribbon2破解
- Qt软件开发 完整项目代码
- MQTT_3.1protocol_Specific中文版
- 在QT中使用RTP进行视频的采集和传输
- Mini6410 Qt4和Qtopia编程开发指南
- Qt实现Code39条形码
- qt_ffmpeg_mp4_export_and_import.zip
- QT5.5入门与项目实战
- Huffman Compress 霍夫曼编码 压缩 解压缩
评论
共有 条评论