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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QTableViewQt框架的一个类,用于展示表格数据。要使用QTableView,需要先包含头文件#include <QTableView>和#include <QStandardItemModel>。然后创建一个QTableView对象和QStandardItemModel,并使用QTableView的setModel()函数将视图和模型对象进行绑定。 以下是一个继承QTableView的示例代码: ```cpp // .h #include <QTableView> #include <QStandardItemModel> class DBTableView : public QTableView { public: explicit DBTableView(QWidget* _parent_widget = nullptr); ~DBTableView() override; private: QStandardItemModel* db_table_model_; }; // .cpp DBTableView::DBTableView(QWidget* _parent_widget) : QTableView(_parent_widget) { db_table_model_ = new QStandardItemModel(); setModel(db_table_model_); } DBTableView::~DBTableView() {} ``` 要填充表格数据,可以使用QStandardItemModel作为表格的数据模型。每一行每一的数据可以通过操作模型来设置。 以下是一个使用QStandardItemModel的示例代码: ```cpp // .h #include <QStandardItemModel> #include <QWidget> #include <QTableView> #include <QStandardItem> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QEvent> #include <QMenu> #include <QDebug> class TableViewWidget : public QWidget { Q_OBJECT public: TableViewWidget(QWidget* parent = nullptr); ~TableViewWidget(); private: void initTableView(); private: QTableView* m_pMyTableView; QStandardItemModel* m_model; QPushButton* m_pBtnRemove; QPushButton* m_pBtnAdd; }; // .cpp TableViewWidget::TableViewWidget(QWidget* parent) : QWidget(parent) { initTableView(); } TableViewWidget::~TableViewWidget() { delete m_model; } void TableViewWidget::initTableView() { m_model = new QStandardItemModel(this); m_pMyTableView = new QTableView(this); m_pMyTableView->setModel(m_model); // 设置其他属性和布局 // 添加到布局 QVBoxLayout* layout = new QVBoxLayout(this); layout->addWidget(m_pMyTableView); layout->addWidget(m_pBtnRemove); layout->addWidget(m_pBtnAdd); setLayout(layout); } bool TableViewWidget::eventFilter(QObject* object, QEvent* event) { // 事件过滤器的处理逻辑 // ... return false; } ``` 在创建完QTableView对象后,可以设置使用自定义菜单策略,并进行信号槽关联。下面是一个示例: ```cpp void TestTable::tableViewMenu(const QPoint& _pos) { // 响应数据处理 } void TestTable::initUI() { // ... connect(table_view, &QTableView::customContextMenuRequested, this, &TestTabel::tableViewMenu); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值