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不放,右手在小键盘也就是右边的数字键盘依 ...
随机推荐
- 回顾 Exchange 2007 SCC 安装-供需要的人参考!
最近可能会涉及到一个项目的升级,客户目前是基于SCC+SCR的一种工作模式,因为之前对SCR 了解很少,所以需要搭建一个SCC+SCR 平台来做一个整体的POC,来还原整个安装和升级过程. 首先我们先 ...
- weblogic——服务器搭建与配置
本次操作的内容:weblogic服务器搭建与配置服务 本次操作是主要围绕如何搭建weblogic服务器及配置服务,总共有两大步骤,可划分为六个小步骤: 选取已有环境,准备weblogic压缩包 安装w ...
- scrapy框架系列 (1) 初识scrapy
Scrapy 框架 Scrapy是用纯Python实现一个为了爬取网站数据.提取结构性数据而编写的应用框架,用途非常广泛. 框架的力量,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页 ...
- First Missing Positive leetcode java
题目: Given an unsorted integer array, find the first missing positive integer. For example, Given [1, ...
- javaScript:让文本框内的最后一个文字的后面获得焦点
//当失去交点以后 让文本框内的文字获得焦点 并且光标移到最后一个字后面 function myfocus(myid) { if(isNav){ document.getElementById(myi ...
- 从Linux服务器下载网站文件
最近公司迁来一个新客户,该客户的网站是别的网络服务商做的,放在linux主机上,因为客户跟之前的网络服务商合作的不愉快 所以就把网站迁到我们公司,经理让我把网站文件和数据库download下来并在我们 ...
- "Value does not fall within the expected range" with managed metadata fields
From: http://geekswithblogs.net/claraoscura/archive/2011/01/21/143569.aspx The problem: I have an ...
- slf4j、jcl、jul、log4j1、log4j2、logback大总结
1 系列目录 jdk-logging.log4j.logback日志介绍及原理 commons-logging与jdk-logging.log4j1.log4j2.logback的集成原理 slf4j ...
- 求两个有序数组的中位数或者第k小元素
问题:两个已经排好序的数组,找出两个数组合并后的中位数(如果两个数组的元素数目是偶数,返回上中位数). 设两个数组分别是vec1和vec2,元素数目分别是n1.n2. 算法1:最简单的办法就是把两个数 ...
- 微信小程序 - 步骤条组件
<!-- 未激活颜色: uncolor:'#ccc' 激活 active:0 数据源 data:[{},{}] 步骤条类型:type basic detail num more --> & ...