Qt中的QTableView 中的列放入Widget

QTableView是Qt中Model View理念的框架,View只展现数据,所以通过互交修改编辑数据,需要用到委托这个概念Delegate。

所以基本思路是继承QItemDelegate这个类,然后overried里面的方法,然后通过QTableView的成员函数setItemDelegateForColumn就可以了。

以下代码是在某列中添加QComboBox:

 1 ///////////////////////////EmployeePrivilegeComboxEditor.h///////////
 2 #include <QItemDelegate>
 3 #include <QStringList>
 4
 5 class EmployeePrivilegeComboxEditor : public QItemDelegate {
 6     Q_OBJECT
 7
 8 public:
 9     explicit EmployeePrivilegeComboxEditor(QObject* parent = Q_NULLPTR);
10     ~EmployeePrivilegeComboxEditor();
11
12 public:
13     virtual QWidget* createEditor(QWidget* parent,
14         const QStyleOptionViewItem& option,
15         const QModelIndex& index) const override;
16
17     virtual void setEditorData(
18         QWidget* editor, const QModelIndex& index) const override;
19
20     virtual void setModelData(QWidget* editor, QAbstractItemModel* model,
21         const QModelIndex& index) const override;
22
23 private:
24     QStringList items_;
25 };
26
27 ///////////////////////////EmployeePrivilegeComboxEditor.cpp///////////
28
29 #include "EmployeePrivilegeComboxEditor.h"
30
31 #include <QComboBox>
32
33 EmployeePrivilegeComboxEditor::EmployeePrivilegeComboxEditor(QObject* parent)
34     : QItemDelegate(parent)
35 {
36     items_ << QStringLiteral("普通用户") << QStringLiteral("管理员")
37            << QStringLiteral("超级管理员");
38 }
39
40 EmployeePrivilegeComboxEditor::~EmployeePrivilegeComboxEditor() {}
41
42 QWidget* EmployeePrivilegeComboxEditor::createEditor(QWidget* parent,
43     const QStyleOptionViewItem& option, const QModelIndex& index) const
44 {
45     QComboBox* editor = new QComboBox(parent);
46     editor->addItems(items_);
47     return editor;
48 }
49
50 void EmployeePrivilegeComboxEditor::setEditorData(
51     QWidget* editor, const QModelIndex& index) const
52 {
53     QString text        = index.model()->data(index, Qt::EditRole).toString();
54     QComboBox* comboBox = dynamic_cast<QComboBox*>(editor);
55     if (comboBox) {
56         int tindex = comboBox->findText(text);
57         comboBox->setCurrentIndex(tindex);
58     }
59 }
60
61 void EmployeePrivilegeComboxEditor::setModelData(
62     QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const
63 {
64     QComboBox* comboBox = dynamic_cast<QComboBox*>(editor);
65     if (comboBox) {
66         QString text = comboBox->currentText();
67         model->setData(index, text, Qt::EditRole);
68     }
69 }
70
71 //////////////////////////Usage///////////////////////////
72 ui->employeeTable->setItemDelegateForColumn(
73         3, new EmployeePrivilegeComboxEditor(this)); 

以下代码是在单元格列中放置QPushButton:

  1 ///////////////////////DownloadUpdateButton.h//////////////////////
  2 #pragma once
  3
  4 #include <QItemDelegate>
  5 #include <QString>
  6 #include <QMap>
  7
  8 class QStyleOptionButton;
  9
 10 class DownloadUpdateButton : public QItemDelegate
 11 {
 12     Q_OBJECT
 13
 14 public:
 15     explicit DownloadUpdateButton(QObject *parent = Q_NULLPTR);
 16     ~DownloadUpdateButton();
 17 public:
 18     void paint(QPainter *painter, const QStyleOptionViewItem &option,
 19         const QModelIndex &index) const override;
 20     bool editorEvent(QEvent *event, QAbstractItemModel *model,
 21         const QStyleOptionViewItem &option, const QModelIndex &index) override;
 22 private slots:
 23
 24 private:
 25     QString downloadStyle_;
 26     QString updateStyle_;
 27 private:
 28     QMap<QModelIndex, QStyleOptionButton*> m_btns;
 29 };
 30
 31 ///////////////////DownloadUpdateButton.cpp///////////////////////////
 32
 33 #include "DownloadUpdateButton.h"
 34
 35 #include <QStyleOptionButton>
 36 #include <QMessageBox>
 37 #include <QPainter>
 38 #include <QApplication>
 39 #include <QDesktopWidget>
 40 #include <QMouseEvent>
 41
 42 DownloadUpdateButton::DownloadUpdateButton(QObject *parent)
 43     : QItemDelegate(parent)
 44 {
 45     downloadStyle_ = "";
 46     updateStyle_ = "";
 47
 48 }
 49
 50 DownloadUpdateButton::~DownloadUpdateButton()
 51 {
 52 }
 53
 54 void DownloadUpdateButton::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
 55 {
 56     QStyleOptionButton* button = m_btns.value(index);
 57     if (!button) {
 58         button = new QStyleOptionButton();
 59         int x, y, width, height;
 60         x = option.rect.left() + option.rect.width()/2;
 61         y = option.rect.top() + 5;
 62         width = 20;
 63         height = 20;
 64         button->rect = option.rect.adjusted(4, 4, -4, -4) /*QRect(x,y,width,height)*/;
 65         button->text = "X";
 66         button->state |= QStyle::State_Enabled;
 67
 68         (const_cast<DownloadUpdateButton *>(this))->m_btns.insert(index, button);
 69     }
 70     painter->save();
 71
 72     if (option.state & QStyle::State_Selected) {
 73         painter->fillRect(option.rect, option.palette.highlight());
 74
 75     }
 76     painter->restore();
 77     QApplication::style()->drawControl(QStyle::CE_PushButton, button, painter);
 78 }
 79
 80 bool DownloadUpdateButton::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
 81 {
 82     int x, y, width, height;
 83     x = option.rect.left() + option.rect.width() / 2;
 84     y = option.rect.top() + 5;
 85     width = 20;
 86     height = 20;
 87
 88     QRect btnRect(x, y, width, height);
 89
 90     if (event->type() == QEvent::MouseButtonPress) {
 91
 92         QMouseEvent* e = (QMouseEvent*)event;
 93
 94         if (option.rect.adjusted(4, 4, -4, -4)/*(btnRect*/.contains(e->x(), e->y()) && m_btns.contains(index)) {
 95             m_btns.value(index)->state |= QStyle::State_Sunken;
 96         }
 97     }
 98     if (event->type() == QEvent::MouseButtonRelease) {
 99         QMouseEvent* e = (QMouseEvent*)event;
100
101         if (option.rect.adjusted(4, 4, -4, -4)/*btnRect*/.contains(e->x(), e->y()) && m_btns.contains(index)) {
102             m_btns.value(index)->state &= (~QStyle::State_Sunken);
103
104             QDialog *d = new QDialog();
105
106             d->setGeometry(0, 0, 200, 200);
107             d->move(QApplication::desktop()->screenGeometry().center() - d->rect().center());
108             d->show();
109         }
110     }
111     return true;
112 }
113
114 ///////////////////////Usage//////////////////////////
115  ui->appTable->setItemDelegateForColumn(
116         5, 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

时间: 2024-08-09 08:08:05

Qt中的QTableView 中的列放入Widget的相关文章

在Photoshop中实现将Roughness贴图放入Metalic贴图的Alpha通道中

一万年过去了,我终于回来写博客啦 最近在看傅老师的Unity教程,看到第10p的时候,有一个地方讲的是Material中可设置的一个贴图中,有一个叫做Roughness(记录Material粗糙度的贴图),它在Unity中没有特定的选项去设置这张贴图,唯一接近的就是Smoothness属性.但Smoothness属性又不能设置贴图. 这是因为在Unity中,当Material的Shader为Standard的时候,它会吧Metalic和Roughness这两种贴图合成一个文件使用,据说这种设计是

qt 下tablewidget 中要单独使一列不能编辑怎么办?

qt 下tablewidget 中要单独使一列不能编辑怎么办???最好能用一个button 控件控制 2012-03-31 19:57南山杨木 | 分类:其他编程语言 | 浏览2658次 分享到: 2012-04-01 16:29 提问者采纳 热心网友 修改该列item的flags. 例如: #include <QApplication>#include <QTableWidget>#include <QTableWidgetItem>int main(int argc

&#39;QObject&amp; QObject::operator=(const QObject&amp;)&#39; is private——无法将自定义的QObject子类放入Qt容器(container)中

先贴出问题的代码: 1 #include<QCoreApplication> 2 classMyObject:publicQObject 3 { 4 public: 5 MyObject(QObject*parent =0): 6 QObject(parent) 7 { 8 } 9 private: 10 int m_id; 11 }; 12 int main(int argc,char*argv[]) 13 { 14 QCoreApplication a(argc, argv); 15 QL

读取mysql中的特定列值放入页面的下拉框中

1.使用的技术:JSP,Spring JDBC(Mapper) 2.代码 2.1 接口 public interface IMeetingRoomDao { public List<Mrcap> selectCap(); public List<Mrfloor> selectFloor(); } 2.2 实现类 @Override public List<Mrcap> selectCap() { List<Mrcap> caplist = new Array

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

搜索了一下,QTableView中嵌入复选框CheckBox方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简单,通常用这种方法. 第三种只适合静态显示静态数据用 第四种比较适合扩展,它除了可以嵌入复选框,还可以通过paint()绘制其它控件,图片等自定义风格. 第一种方法是:编辑委托法 这种方法直接利用委托中重载createEditor(),激活QCheckBox,这个缺点是必须双击/选中,才能显示CheckBox控件.一般不满足我们实际中的直接显示的需要.可以

QTreeView/QTableView中利用QStandardItem实现复选框三种形态变化

版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QTreeView/QTableView中利用QStandardItem实现复选框三种形态变化     本文地址:http://techieliang.com/2017/12/729/ 文章目录 1. 介绍 2. 源码  2.1. using_checkbox_item.h  2.2. using_checkbox_item.cpp 3. 说明 1. 介绍 复选框有三种形态:全选对勾.全不选

将time的结果放入到文件中

缘起 今天看到一个问题,如何把time的执行结果放到文件中,心想直接time sh sleep.sh >output.log不就可以了么.其实大部分返回标准输出的命令都可以这么干,列如:ls >output.log 就会把ls的执行结果放入到output.log中. 那么为什么time命令就不行了呢? 因为:time命令返回的是标准错误输出! 实验准备: 1.有一个sleep.sh脚本,脚本内容如下:2.执行结果如下:3.通过time命令获取脚本执行时间 无效的例子1: time sh sle

输入两个整数,放入到a与b变量中去,如果a&gt;b就将a与b中的值进行交换,否则就不交换。

import java.util.Scanner; /** * @author 蓝色以太 输入两个整数,放入到a与b变量中去,如果a>b就将a与b中的值进行交换,否则就不交换. */ public class Change { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入两个整数:"); int a = sc.nextInt

在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句

在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句 突然看到这个问题,脑袋一蒙,不知道啥意思,后来想想,试图把select里的选项放到后面,问题自然解决! 下面这个就是报“orderdate select shipcountry,sum(shipvia) as totalvia,OrderDate as thefirsttime from orders group by shipcountry,相应的从网上看到其他的朋友也有这样的问题 比如要显示authors表中