qt 带复选框的下拉列表

参考 Qt之QComboBox定制
在这里插入图片描述

核心思想:

void QComboBox::setView(QAbstractItemView *itemView)
void QComboBox::setModel(QAbstractItemModel *model)

下拉列表拉出来的那个界面可以是 QAbstractItemView ,比如QListWidget 。只要在构造函数中:

QComboBox->setModel(pListWidget->model());
QComboBox->setView(pListWidget);

设置好其他的操作就和 QListWidget 一样了。

所以 我们想要在下拉列表里加入复选框,只需要设置

	QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
	pListWidget->addItem(pItem);
	QCheckBox *pCheckBox = new QCheckBox(this); 
	pCheckBox->setText(value);
	pListWidget->addItem(pItem);
	pListWidget->setItemWidget(pItem, pCheckBox);
	connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(Slot_StateChanged(int)));

即可。

同理,下拉列表还可以是表格形式

源码

#pragma once
#include <qcombobox.h>
#include<QListWidget>
#include<QLineEdit>

class CheckComboBox:
	public QComboBox
{
    
    
	Q_OBJECT
public:
	CheckComboBox(QWidget * parent = nullptr);
	~CheckComboBox();
	void setValues(QStringList values);
	void push_back(QString value);
	QString text();//返回用户选择的值
protected slots:
	void Slot_StateChanged(int);
private:
	QListWidget *pListWidget = nullptr;
	QLineEdit *pLineEdit = nullptr;
	BASEINFO m_strInfo;
};


#include "CheckComboBox.h"
#include "QCheckBox"
CheckComboBox::CheckComboBox(QWidget * parent ) :QComboBox(parent)
{
    
    
	pListWidget = new QListWidget(this);
	pLineEdit = new QLineEdit(this);
	this->setModel(pListWidget->model());
	this->setView(pListWidget);
	this->setLineEdit(pLineEdit);
	pLineEdit->setReadOnly(true);
}
CheckComboBox::~CheckComboBox()
{
    
    
	if (pListWidget)
	{
    
    
		delete pListWidget;
		pListWidget = nullptr;
	}
	if (pLineEdit)
	{
    
    
		delete pLineEdit;
		pLineEdit = nullptr;
	}
}

void CheckComboBox::setValues(QStringList values)
{
    
    
	for (auto value : values)
	{
    
    
		push_back(value);
	}
}
void CheckComboBox::push_back(QString value)
{
    
    
	QListWidgetItem *pItem = new QListWidgetItem(pListWidget);
	pListWidget->addItem(pItem);
	QCheckBox *pCheckBox = new QCheckBox(this); 
	pCheckBox->setText(value);
	pListWidget->addItem(pItem);
	pListWidget->setItemWidget(pItem, pCheckBox);
	connect(pCheckBox, SIGNAL(stateChanged(int)), this, SLOT(Slot_StateChanged(int)));
	pCheckBox->setChecked(true);
}

void CheckComboBox::Slot_StateChanged(int)
{
    
    
	QString  strSelectedData;
	int nCount = pListWidget->count();
	for (int i = 0; i < nCount; ++i)
	{
    
    
		QListWidgetItem *pItem = pListWidget->item(i);
		QWidget *pWidget = pListWidget->itemWidget(pItem);
		QCheckBox *pCheckBox = (QCheckBox *)pWidget;
		if (pCheckBox->isChecked())
		{
    
    
			QString strText = pCheckBox->text();
			strSelectedData.append(strText).append(";");
		}
	}
	//避免最后一个item添加";"
	if (strSelectedData.endsWith(";"))
		strSelectedData.remove(strSelectedData.count() - 1, 1);
	if (!strSelectedData.isEmpty())
	{
    
    
		pLineEdit->setText(strSelectedData);
		pLineEdit->setToolTip(strSelectedData);
	}
 
 
}
QString CheckComboBox::text()
{
    
    
	return pLineEdit->text();
}

 

猜你喜欢

转载自blog.csdn.net/fuyouzhiyi/article/details/127802286