Qt中的QTableView 中的列放入Widget
QTableView是Qt中Model View理念的框架,View只展现数据,所以通过互交修改编辑数据,需要用到委托这个概念Delegate。
所以基本思路是继承QItemDelegate这个类,然后overried里面的方法,然后通过QTableView的成员函数setItemDelegateForColumn就可以了。
以下代码是在某列中添加QComboBox:
///////////////////////////EmployeePrivilegeComboxEditor.h///////////
#include <QItemDelegate>
#include <QStringList> class EmployeePrivilegeComboxEditor : public QItemDelegate {
Q_OBJECT public:
explicit EmployeePrivilegeComboxEditor(QObject* parent = Q_NULLPTR);
~EmployeePrivilegeComboxEditor(); public:
virtual QWidget* createEditor(QWidget* parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const override; virtual void setEditorData(
QWidget* editor, const QModelIndex& index) const override; virtual void setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const override; private:
QStringList items_;
}; ///////////////////////////EmployeePrivilegeComboxEditor.cpp/////////// #include "EmployeePrivilegeComboxEditor.h" #include <QComboBox> EmployeePrivilegeComboxEditor::EmployeePrivilegeComboxEditor(QObject* parent)
: QItemDelegate(parent)
{
items_ << QStringLiteral("普通用户") << QStringLiteral("管理员")
<< QStringLiteral("超级管理员");
} EmployeePrivilegeComboxEditor::~EmployeePrivilegeComboxEditor() {} QWidget* EmployeePrivilegeComboxEditor::createEditor(QWidget* parent,
const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QComboBox* editor = new QComboBox(parent);
editor->addItems(items_);
return editor;
} void EmployeePrivilegeComboxEditor::setEditorData(
QWidget* editor, const QModelIndex& index) const
{
QString text = index.model()->data(index, Qt::EditRole).toString();
QComboBox* comboBox = dynamic_cast<QComboBox*>(editor);
if (comboBox) {
int tindex = comboBox->findText(text);
comboBox->setCurrentIndex(tindex);
}
} void EmployeePrivilegeComboxEditor::setModelData(
QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
{
QComboBox* comboBox = dynamic_cast<QComboBox*>(editor);
if (comboBox) {
QString text = comboBox->currentText();
model->setData(index, text, Qt::EditRole);
}
} //////////////////////////Usage///////////////////////////
ui->employeeTable->setItemDelegateForColumn(
, new EmployeePrivilegeComboxEditor(this));
以下代码是在单元格列中放置QPushButton:
///////////////////////DownloadUpdateButton.h//////////////////////
#pragma once #include <QItemDelegate>
#include <QString>
#include <QMap> class QStyleOptionButton; class DownloadUpdateButton : public QItemDelegate
{
Q_OBJECT public:
explicit DownloadUpdateButton(QObject *parent = Q_NULLPTR);
~DownloadUpdateButton();
public:
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const override;
bool editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index) override;
private slots: private:
QString downloadStyle_;
QString updateStyle_;
private:
QMap<QModelIndex, QStyleOptionButton*> m_btns;
}; ///////////////////DownloadUpdateButton.cpp/////////////////////////// #include "DownloadUpdateButton.h" #include <QStyleOptionButton>
#include <QMessageBox>
#include <QPainter>
#include <QApplication>
#include <QDesktopWidget>
#include <QMouseEvent> DownloadUpdateButton::DownloadUpdateButton(QObject *parent)
: QItemDelegate(parent)
{
downloadStyle_ = "";
updateStyle_ = ""; } DownloadUpdateButton::~DownloadUpdateButton()
{
} void DownloadUpdateButton::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionButton* button = m_btns.value(index);
if (!button) {
button = new QStyleOptionButton();
int x, y, width, height;
x = option.rect.left() + option.rect.width()/;
y = option.rect.top() + ;
width = ;
height = ;
button->rect = option.rect.adjusted(, , -, -) /*QRect(x,y,width,height)*/;
button->text = "X";
button->state |= QStyle::State_Enabled; (const_cast<DownloadUpdateButton *>(this))->m_btns.insert(index, button);
}
painter->save(); if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight()); }
painter->restore();
QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter);
} bool DownloadUpdateButton::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
int x, y, width, height;
x = option.rect.left() + option.rect.width() / ;
y = option.rect.top() + ;
width = ;
height = ; QRect btnRect(x, y, width, height); if (event->type() == QEvent::MouseButtonPress) { QMouseEvent* e = (QMouseEvent*)event; if (option.rect.adjusted(, , -, -)/*(btnRect*/.contains(e->x(), e->y()) && m_btns.contains(index)) {
m_btns.value(index)->state |= QStyle::State_Sunken;
}
}
if (event->type() == QEvent::MouseButtonRelease) {
QMouseEvent* e = (QMouseEvent*)event; if (option.rect.adjusted(, , -, -)/*btnRect*/.contains(e->x(), e->y()) && m_btns.contains(index)) {
m_btns.value(index)->state &= (~QStyle::State_Sunken); QDialog *d = new QDialog(); d->setGeometry(, , , );
d->move(QApplication::desktop()->screenGeometry().center() - d->rect().center());
d->show();
}
}
return true;
} ///////////////////////Usage//////////////////////////
ui->appTable->setItemDelegateForColumn(
, new DownloadUpdateButton(this)); //给第5列添加一个按钮
rerferences:
https://www.cnblogs.com/li-peng/p/3961843.html
https://www.cnblogs.com/li-peng/p/4029885.html
http://www.qtcn.org/bbs/simple/?t60567.html
https://stackoverflow.com/questions/11777637/adding-button-to-qtableview
Qt中的QTableView 中的列放入Widget的更多相关文章
- mysql实现简单的增删改查,放入xmapp自带数据库中
1.mysql概念:SQL-Structured Query Language,是一种特殊的语言,专用于操作关系型数据库服务器中的数据,所有的SQL语句分为四类: (1)DDL(2)DQL(3)DML ...
- tuple放入dict中
tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...
- PyQt(Python+Qt)学习随笔:QTableView中数据行高和列宽的调整方法
老猿Python博文目录 老猿Python博客地址 一.概述 在QTableView中,除了采取缺省的间隔显示行和列的数据外,还可以通过带调整数据的行高和列宽. 二.列宽调整方法 调整数据行列宽的方法 ...
- Qt自定义委托在QTableView中绘制控件、图片、文字(内容比较全)
自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...
- Qt自定义委托在QTableView中绘制控件、图片、文字
自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...
- 'QObject& QObject::operator=(const QObject&)' is private——无法将自定义的QObject子类放入Qt容器(container)中
先贴出问题的代码: #include<QCoreApplication> classMyObject:publicQObject { public: MyObject(QObject*pa ...
- qt QTableView中嵌入复选框CheckBox 的四种方法总结
第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...
- C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。
//将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...
- 在Excel中把横行与竖列进行置换、打勾号
在Excel中把横行与竖列进行置换:复制要置换的单元,在新的单元上右键->选择性复制,会出现对话框,选中“置换”,即可在Excel中打勾号,左手按住ALT不放,右手在小键盘也就是右边的数字键盘依 ...
随机推荐
- osg for android学习之一:windows下编译(亲测通过)【转】
1. 首先需要一个OSG for android的环境 (1)NDK 现在Eclipse 对NDK已经相当友好了,已经不需要另外cygwin的参与,具体可以参考 Android NDK开发篇(一):新 ...
- 构造读写IRP(转)
DDK示例中的代码. NTSTATUSFltReadSectors( IN PDEVICE_OBJECT DeviceObject, OUT PVOID Buffer, IN ULONG Len ...
- JavaScript-手机中访问页面判断
最近在做微信服务号开发,其中遇到一个问题是微信服务号查看的个人的消息,如果点击在浏览器中查看(iOS中是在Safari中打开)应该是跳转到登录页面,因为页面需要从后台获取,因为需要服务端判断,如果是存 ...
- 层叠顺序与堆栈上下文、font-family字体定义顺序的
1.层叠顺序与堆栈上下文 z-index 看上去其实很简单,根据 z-index 的高低决定层叠的优先级,实则深入进去,会发现内有乾坤. 问题背景:拥有共同父容器的两个 DIV 重叠在一起,是 dis ...
- 【Scala】Scala-循环与遍历
Scala-循环与遍历 scala for 1000_百度搜索 Scala 2.8的for表达式:性能与运行顺序的改进 - 51CTO.COM scala List集合的用法 - CSDN博客
- 滴滴大数据算法大赛Di-Tech2016参赛总结
https://www.jianshu.com/p/4140be00d4e3 题目描述 建模方法 特征工程 我的几次提升方法 从其他队伍那里学习到的提升方法 总结和感想 神经网络方法的一点思考 大数据 ...
- Spring Boot集成持久化Quartz定时任务管理和界面展示
本文是对之前的一篇文章Spring+SpringMVC+mybatis+Quartz整合代码部分做的一个修改和补充, 其中最大的变化就是后台框架变成了Spring Boot. 本工程所用到的技术或工具 ...
- spring /spring boot中mock
1 Mockito简介 1.1 Mockito是什么 Mockito是一个简单的流行的Mock框架.它允许你创建和配置mock对象.使用Mockito可以明显的简化对外部依赖的测试类的开发.一般使 ...
- Android自己定义View之仪表盘
新建项目,新建DashBoardView继承自View实现OnGlobalLayoutListener接口,并重写OnDraw方法. 使用OnGlobalLayoutListener接口须要重写onG ...
- FastDFS_v4.06+nginx-1.4.2配置详解
径不带group名(storage只有一个group的情况),如/M00/00/00/xxx: location /M00 { ngx_fastdfs_module; ...