资源简介
效果看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的简单视频播放器
- UR优傲机器人说明书
- tcpNum.zip
- QT项目code.rar
- qt做一个带有日历和钟表的界面
- 树莓派教程--Qt工程中使用DHT11传感器
- QT实现学生管理系统123783
- qt虚拟键盘26键和数字键
- 基于Qt界面显示的温湿度检测
- qt-opensource-linux-x64-5.7.0.run.txt
- 利用Qt实现的N皇后算法
- beatMouse.zip
- Qt 使用QPainter类在子控件上画图
- 基于Qt绘制的时间轴demo
- linux下QT游戏五子棋源代码
- QT中实现8个基本对话框
- QT自绘滑条控件Slider,可设置横向和纵
- A星算法代码QT运行
- QT带有滑动效果的图片浏览器
- 基于Qt的图像采集系统
- qt的小波分解程序
- 人脸识别在arm板上的实现
- qt日历控件
- Qt中用qwt实现动态绘制二维曲线
- Qt 模拟软键盘
- QT音乐播放器120395
- Qt界面多线程opencv调用摄像头
- QWebEngineView实现聊天窗
- QT 小游戏 加农炮 源码 +教程刚刚把源
- qt可以使用的AES加密的类
评论
共有 条评论