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的更多相关文章

  1. mysql实现简单的增删改查,放入xmapp自带数据库中

    1.mysql概念:SQL-Structured Query Language,是一种特殊的语言,专用于操作关系型数据库服务器中的数据,所有的SQL语句分为四类: (1)DDL(2)DQL(3)DML ...

  2. tuple放入dict中

    tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...

  3. PyQt(Python+Qt)学习随笔:QTableView中数据行高和列宽的调整方法

    老猿Python博文目录 老猿Python博客地址 一.概述 在QTableView中,除了采取缺省的间隔显示行和列的数据外,还可以通过带调整数据的行高和列宽. 二.列宽调整方法 调整数据行列宽的方法 ...

  4. Qt自定义委托在QTableView中绘制控件、图片、文字(内容比较全)

    自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...

  5. Qt自定义委托在QTableView中绘制控件、图片、文字

    自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...

  6. 'QObject& QObject::operator=(const QObject&)' is private——无法将自定义的QObject子类放入Qt容器(container)中

    先贴出问题的代码: #include<QCoreApplication> classMyObject:publicQObject { public: MyObject(QObject*pa ...

  7. qt QTableView中嵌入复选框CheckBox 的四种方法总结

    第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件 ...

  8. C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。

    //将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...

  9. 在Excel中把横行与竖列进行置换、打勾号

    在Excel中把横行与竖列进行置换:复制要置换的单元,在新的单元上右键->选择性复制,会出现对话框,选中“置换”,即可在Excel中打勾号,左手按住ALT不放,右手在小键盘也就是右边的数字键盘依 ...

随机推荐

  1. FileStream 的FileShare一点小认识

    C#读写文本文件一般都是用StreamWriter来实现(读书的时候就这样用,毕业后这几年基本也是这样干的),通常代码如下: using (StreamWriter sw = new StreamWr ...

  2. 用css3实现风车效果

    前面讲过css3可以替代很多js实现的效果,其实很多时候纯css3甚至可以替代图片,直接用css3就可以画出一些简单的图片.虽然css3画出来的图片效果可能不如直接用图片的好,实现起来也比较复杂,最麻 ...

  3. a,b盘去哪儿?

    我们知道,电脑的硬盘从c盘开始了,那我们不仅要问了,a,b盘符去哪儿了? 打开Windows系统的电脑,看到的第一个硬盘分区是C盘,请问A盘和B盘在哪里?所谓的硬盘分区号就是盘符,也就是驱动器号,是微 ...

  4. BiLSTM-CRF模型中CRF层的解读

    转自: https://createmomo.github.io/ BiLSTM-CRF模型中CRF层的解读: 文章链接: 标题:CRF Layer on the Top of BiLSTM - 1  ...

  5. 文本分类需要CNN?No!fastText完美解决你的需求(后篇)

    http://blog.csdn.net/weixin_36604953/article/details/78324834 想必通过前一篇的介绍,各位小主已经对word2vec以及CBOW和Skip- ...

  6. Kafka:ZK+Kafka+Spark Streaming集群环境搭建(八)安装zookeeper-3.4.12

    如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...

  7. Java-Shiro(三):Shiro与Spring MVC集成

    新建Java Daynamic Web项目 导入Spring.SpringMVC依赖包: 导入Spring & Spring MVC包(导入如下所有开发包): Spring AOP依赖扩展包: ...

  8. 转: git使用时让http记住帐号密码

    见 http://git.mydoc.io/?t=154710 https 方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受 https 带来的极速 按照以下设置记住密 ...

  9. "Ext 4.1 Grid 'el.dom' 为空或不是对象"问题的解决

    我在使用Ext 4.1 做Grid,IE下冒出这么个错误,导致表格完全显示不出来,换另外一个IE浏览器,有没有问题,呵呵,百思不得其解啊... 后来得出答案,即在grid相关代码周围套上Ext.onR ...

  10. 【S6】当心C++编译器最烦人的分析机制

    1.考虑一个包含int的文件,复制到list,如下: ifstream dataFile("ints.bat"); list<int> data(istream_ite ...