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不放,右手在小键盘也就是右边的数字键盘依 ...
随机推荐
- Asp.Net MVC2.0 Url 路由入门---实例篇 【转】
本篇主要讲述Routing组件的作用,以及举几个实例来学习Asp.Net MVC2.0 Url路由技术. 接着上一篇开始讲,我们在Global.asax中注册一条路由后,我们的请求是怎么转到相应的Vi ...
- 华清远见Linux设备驱动(每章小结)
1. linux设备驱动是以内核模块的方式而存在的,在具体的驱动开发中将驱动编译为模块具有很到的工程意义.因为如果将正在开发中的驱动编译如内核,而开发过程中会不断修改驱动代码,则需要不断的编译和重启 ...
- 用EntityFramework6完成增删查改和事务【转】
http://www.cnblogs.com/wujingtao/p/5407821.html 上一节我们已经学习了如何使用EF连接数据库,并简单演示了一下如何使用EF6对数据库进行操作,这一节我来详 ...
- javascript 的面向对象特性参考
最近在看用javascript+css实现rich client.javascript 也是一个蛮有意思的语言.特别是其面向对象的实现和其他“标准”的OO launguage有很大的不同.但是,都是动 ...
- 在Asp.Net中使用SmtpMail发送邮件的方法
在ASP中,就可以通过调用CDONTS组件发送简单邮件,在ASP.Net中,自然也可以.不同的是,.Net Framework中,将这一组件封装到了System.Web.Mail命名空间中. 一个典型 ...
- Tensorflow动态seq2seq使用总结(r1.3)
https://www.jianshu.com/p/c0c5f1bdbb88 动机 其实差不多半年之前就想吐槽Tensorflow的seq2seq了(后面博主去干了些别的事情),官方的代码已经抛弃原来 ...
- intel 汇编中断解释
汇编中的10H中断是由BIOS对显示器和屏幕所提供的服务程序.使用int 10h服务程序时,必须先指定ah寄存器为以下显示服务编号之一,以指定需要调用的功用. 显示服务 (Video Service: ...
- free的说明
http://www.cnblogs.com/peida/archive/2012/12/25/2831814.html
- CAD中批量打印
同事在网上找各种软件来实现CAD图的批量打印,总是问题多多.于是,我想到一个更方便的解决方法,即只要我将一个打印出来,然后就可以用批量处理来实现. 1.在CAD中输入plot命令(或快捷键Ctrl+P ...
- performSelector 多个参数
[self performSelector:@selector(callFooWithArray) withObject:[NSArray arrayWithObjects:@"first& ...