QT 实现QTableView中,双击弹出下拉框

QTableView显示数据时,双击表格默认情况下是显示Spin控件,但很多情况下需要显示ComboBox,这需要为TableView增加ComBox代理

Combox代理头文件代码:

#pragma once
// ComboBoxDelegate.h
#include 
class ComboDelegate : public QItemDelegate
{
	Q_OBJECT

public:
	ComboDelegate(const QStringList &items, 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;
	int nOpIndex = 0;
signals:
	void CurrentIndexChangeSignal(int iRow, int iColumn) const;  //定义消息必须有const
private slots :
private:	
	QStringList myItems;
};

Combox代理CPP文件代码:

#include "ComboDelegate.h"
#include 
#include 

ComboDelegate::ComboDelegate(const QStringList &items, QObject *parent) :
QItemDelegate(parent)
{
	myItems = items;
}

QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
	QComboBox *editor = new QComboBox(parent);
	editor->addItems(myItems);
	editor->setEditable(false);
	return editor;
}

void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
	QString value = index.model()->data(index, Qt::EditRole).toString();
	QComboBox *comboBox = static_cast(editor);
	int icurIndex = comboBox->findText(value);
	comboBox->setCurrentIndex(icurIndex);
}

void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
	QComboBox *comboBox = static_cast(editor);
	QString value = comboBox->currentText();
	model->setData(index, value, Qt::EditRole);
	emit CurrentIndexChangeSignal(index.row(), index.column());   //发送信号,实现其它联动更新
}

void ComboDelegate::updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
	editor->setGeometry(option.rect);
	QComboBox *comboBox = static_cast(editor);
	comboBox->showPopup(); // 直接弹出下拉框,没有这一句双击后需要再点一次Combox才会弹出下拉框
}

之后,在TableView所在界面类中,为TableView增加QCombox代理

QtTable::QtTable(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	//pModel = new CustomTableModel;
	QStandardItemModel* pModel = new QStandardItemModel(10, 2);
	pModel->setHeaderData(0, Qt::Horizontal, QString("Index"));
	pModel->setHeaderData(1, Qt::Horizontal, QString("Data"));
	for (int row = 0; row < 1000; ++row)
	{
		for (int column = 0; column < 2; ++column)
		{
			QModelIndex index = pModel->index(row, column, QModelIndex());
			pModel->setData(index, QVariant((row + 1) * (column + 1)));
		}
	}
	ui.tableView->setModel(pModel);
	ui.tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	//ui.tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
	QStringList strList;
	strList << "1" << "2" << "3" << "4";
	strList << "5" << "6" << "7" << "8";
	ComboDelegate *pComboBox = new ComboDelegate(strList);    // strList中包含ComboBox中供选择的数据
	ui.tableView->setItemDelegateForColumn(1,pComboBox);
	connect(ui.tableView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(OnTableClicked(const QModelIndex &)));
	connect(pComboBox, SIGNAL(CurrentIndexChangeSignal(int, int)), this, SLOT(OnIndexChangeSlot(int, int)));
}

之后双击表格第2列,就会弹出QComboBox下拉框。源代码下载链接:https://download.csdn.net/download/leither/12504551

参考链接:https://blog.csdn.net/chenyijun/article/details/80915794

https://blog.csdn.net/u014378771/article/details/89914811

https://blog.csdn.net/xiexingshishu/article/details/44886563

 

你可能感兴趣的:(QT 实现QTableView中,双击弹出下拉框)