Use custom delegate components in QTableWidget

Steps for usage

1. Custom delegate components

2. Set the delegate to QTableWidget

Custom delegate component

1. Inherited fromQItemDelegate class

2. Rewrite the following function

  • virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const//Create delegated display control
  • virtual void setEditorData(QWidget *editor, const QModelIndex &index) const//Set the data in the control to QTableWidget (here, take QTableWidget as an example)
  • virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const//Set the data in the control to QTableWidget
class FSpinBoxDelegate : public QItemDelegate
{
public:
	explicit FSpinBoxDelegate(QObject *parent = nullptr);
	~FSpinBoxDelegate();
protected:
	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;
};

FSpinBoxDelegate::FSpinBoxDelegate(QObject *parent /*= nullptr*/)
	:QItemDelegate(parent)
{

}

FSpinBoxDelegate::~FSpinBoxDelegate()
{

}

QWidget* FSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
	QSpinBox *spinBox = new QSpinBox(parent);
	return spinBox;
}

void FSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
	QSpinBox * spinBox = dynamic_cast<QSpinBox*>(editor);
	if (spinBox)
	{
		spinBox->setValue(index.data().toInt());
	}
}

void FSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	QSpinBox * spinBox = dynamic_cast<QSpinBox*>(editor);
	if (spinBox)
	{
		model->setData(index, spinBox->value());
	}
}

Use in QTableWidget

class TableWidgetTest : public QTableWidget
{
	Q_OBJECT

public:
	TableWidgetTest(QWidget *parent = Q_NULLPTR);
	~TableWidgetTest();
};

TableWidgetTest::TableWidgetTest(QWidget *parent)
	: QTableWidget(parent)
{
	this->setColumnCount(3);
	this->setRowCount(5);
	for (int row = 0;row<5;++row)
	{
		for (int col = 0;col<3;++col)
		{
			QTableWidgetItem *item = new QTableWidgetItem;
			item->setText(QString("%1").arg(row * 5 + col));
			this->setItem(row, col, item);
		}
	}
	FSpinBoxDelegate* dg = new FSpinBoxDelegate(this);
	this->setItemDelegateForColumn(2, dg);

/*	FSpinBoxDelegate stackDg;
	this->setItemDelegateForColumn(2, &stackDg);//表格的第二列 设置委托
	//此种设置方式会导致崩溃
	*/
}

TableWidgetTest::~TableWidgetTest()
{

}

Problem record

Some friends have encountered double-clicking the table before but the problem of delegated controls does not appear. Now give the following three directions to help you troubleshoot (here, take QTableWidget as an example)

  1. Is the number of columns set for delegation correct?
  2. Can the QTableWidget itself be edited
  3. Can the QTableWidgetItem itself be edited

Description

1 . For example table only three. But the number of columns set by the commission is set to the wrong column (for example, the fourth column, the fifth column, etc.)

2. See if QTableWidget calls setEditTriggers(QAbstractItemView::NoEditTriggers);  resulting in QTableWidget unable to edit.

3. See if QTableWidgetItem has set the attribute setFlags(Qt::NoItemFlags) that does not allow editing  ; the  result is that QTableWidget can allow editing, but QTableWidgetItem itself cannot

Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/114768756