我如何获得 QTableWidget 的委托?
Posted
技术标签:
【中文标题】我如何获得 QTableWidget 的委托?【英文标题】:how can i get Delegate for QTableWidget? 【发布时间】:2016-08-26 07:31:36 【问题描述】:在 QTableWidget 上获取委托时出现问题,我正在尝试使用两个委托(comboboxdelegate、spinbox 委托)来控制具有 2 列和 1 行的 QTableWidget 中的用户输入,但是在阅读了委托类页面并查看 SpinBox 之后& comboox Delegate 示例,我以为我已经弄清楚事情是如何工作的,但显然还没有。
(对不起我的英语),任何帮助表示赞赏。
代码如下。
notepad.h 文件
#ifndef NOTEPAD_H
#define NOTEPAD_H
#include <QtGui>
class Notepad : public QMainWindow
Q_OBJECT
public:
//Notepad(QWidget *parent = 0, Qt::WFlags flags = 0);
Notepad();
void test();
QTableWidget* table ;
public slots:
void add();
void Delete();
void mouseReleaseEvent ( QMouseEvent * event );
private:
QAction *add_action;
QAction *Delete_action;
;
class SpinBoxDelegate : public QStyledItemDelegate,public Notepad
Q_OBJECT
public:
SpinBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
void setEditorData(QWidget *editor, const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
;
class ComboBoxDelegate : public QStyledItemDelegate,public Notepad
Q_OBJECT
public:
ComboBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
;
#endif // NOTEPAD_H
notepad.cpp 文件
#include "notepad.h"
#include <QMessageBox>
#include <QTableView>
#include <QMouseEvent>
#include <QSpinBox>
#include <QComboBox>
#include <QStandardItemModel>
Notepad::Notepad()
table = new QTableWidget();
test() ;
SpinBoxDelegate();
ComboBoxDelegate();
add_action = new QAction(tr("Add cell"), this);
add_action->setIcon(QIcon("add.jpg"));
Delete_action = new QAction(tr("Delete cell"), this);
Delete_action->setIcon(QIcon("delete.jpg"));
connect(Delete_action, SIGNAL(triggered()), this, SLOT(Delete()));
connect(add_action, SIGNAL(triggered()), this, SLOT(add()));
//tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
centralWidget()->setAttribute(Qt::WA_MouseTracking,true);
setMouseTracking(true);
void Notepad::test()
QTableWidgetItem* tableItem = new QTableWidgetItem();
table->setRowCount(1);
table->setColumnCount(3);
table->setItem(0,0,new QTableWidgetItem());
table->setItem(0,1,new QTableWidgetItem());
table->setItem(0,2,new QTableWidgetItem());
table->setMouseTracking(true);
table->viewport()->setMouseTracking(true);
table->installEventFilter(this);
table->viewport()->installEventFilter(this);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
table->setSelectionMode(QAbstractItemView::SingleSelection);
QStandardItemModel model((table->rowCount()),(table->columnCount()));
SpinBoxDelegate sdelegate;
ComboBoxDelegate comDel;
for (int row = 0; row < 4; ++row)
QWidget *parent;
QStyleOptionViewItem option;
for (int column = 0; column < 1; ++column)
table->setItemDelegateForColumn( column, &comDel);
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row + 1) * (column + 1)));
for (int column = 1; column < 2; ++column)
table->setItemDelegateForColumn( column, &sdelegate );
QModelIndex index = model.index(row, column, QModelIndex());
model.setData(index, QVariant((row + 1) * (column + 1)));
table->setHorizontalHeaderItem(0, new QTableWidgetItem("combobox"));
table->setHorizontalHeaderItem(1, new QTableWidgetItem("spinbox"));
table->setHorizontalHeaderItem(2, new QTableWidgetItem("lineEdit"));
tableItem->setFlags(tableItem->flags() ^ Qt::ItemIsEditable);
table->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
setCentralWidget(table);
void Notepad::mouseReleaseEvent (QMouseEvent * event )
QMessageBox* msgBox;
if(event->button() == Qt::RightButton)
QMouseEvent *mouseEvent = static_cast<QMouseEvent*> (event);
QMenu *menu = new QMenu(this);
menu->addAction(add_action);
menu->addAction(Delete_action);
menu->exec(mouseEvent->globalPos());
//msgBox->setInformativeText("u pressed right button");
void Notepad::add()
table->insertRow( 1);
setCentralWidget(table);
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
void Notepad::Delete()
table->removeRow(1);
setCentralWidget(table);
centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
setMouseTracking(true);
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */)/* const*/
QSpinBox *editor = new QSpinBox(parent);
editor->setFrame(false);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) /*const*/
int value = index.model()->data(index, Qt::EditRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) /*const*/
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) /*const*/
editor->setGeometry(option.rect);
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
QComboBox *editor = new QComboBox(parent);
QStringList list ;
list << "srikanth" << "dilip";
editor->addItems(list);
//editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));
return editor;
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(value);
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();
model->setData(index, value);
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
editor->setGeometry(option.rect);
main.cpp 文件
#include <QtGui>
#include <notepad.h>
int main(int argv, char **args)
QApplication app(argv, args);
Notepad notepad;
notepad.show();
return app.exec();
【问题讨论】:
【参考方案1】:QTableWidget 具有获取委托的功能。然后,您可以将其转换为您的自定义委托,假设您确定它实际上是这种类型。
MyCustomDelegate * theDelegate = ( MyCustomDelegate *) myTableWidget->itemDelegate();
【讨论】:
以上是关于我如何获得 QTableWidget 的委托?的主要内容,如果未能解决你的问题,请参考以下文章
QT编程,如何用代码实现 QTableWidget中滚动条的移动
如何将带有数据的标题添加到 Qt 中的 QTableWidget?
QT中的QtableWidget和QtableView使用有啥区别?