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. Go语言之进阶篇TCP相互通信

    1.TCP相互通信 服务端示例: tcp_server.go package main import ( "fmt" "net" ) func main() { ...

  2. Python实现爬虫设置代理IP和伪装成浏览器的方法(转载)

    https://www.jb51.net/article/139587.htm chrome_options = webdriver.ChromeOptions() chrome_options.ad ...

  3. 中文分词 coreseek安装笔记

    #!/bin/bash # create by lhb # date 2013-11-26 # coreseek install script apt-get install make gcc g++ ...

  4. Eclipse QuickSear的插件的说明

    https://spring.io/blog/2013/07/11/eclipse-quick-search Eclipse QuickSear的插件的说明

  5. Cognos11中Dashboard和HTML页面的简单集成

    一.需求 之前很多第三方的程序都是通脱URL的形式可以和cognos Report进行集成,在我前几天的博文<Cognos11中通过URL访问report的设置>一篇中也提到了普通repo ...

  6. Maven镜像收集

    本贴主要收集国内国外速度比较快的maven镜像,OSC的MAVEN已经关闭了 0.阿里Maven镜像 setting.xml https://github.com/ae6623/Zebra/blob/ ...

  7. “Uncaught TypeError: Cannot call method 'createChild' of undefined" 问题的解决

    Uncaught TypeError: Cannot call method 'createChild' of undefined 我在使用Ext 4.1.1做grid.Panel,然后chrome爆 ...

  8. 【Python】使用torrentParser1.02对单文件torrent的分析结果

    C:\Users\horn1\Desktop\python\41-torrentParser>python torrentParser.py 文件名=./5.torrent 文件结构: anno ...

  9. ActiveMQ持久化方式

    ActiveMQ持久化方式 发表于8个月前(2014-09-04 15:55)   阅读(686) | 评论(0) 17人收藏此文章, 我要收藏 赞1 慕课网,程序员升职加薪神器,点击免费学习 摘要  ...

  10. Solidworks如何把装配体做成单个零件

    先把装配体另存为IGS格式   再把IGS打开之后另存为SLDPRT格式(打开IGS需要比较长的时间,中途会弹出提示,而且IGS文件体积也比较大)   另存为SLDPRT之后体积就很小了       ...