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-10-11 18:31:12