自定义ComboBox下拉框(带关闭按钮)

class ComboBox : public QComboBox
{
	Q_OBJECT
public:
	ComboBox(QWidget *parent = 0);
	~ComboBox();

public:
	void hidePopup();
	void showPopup();
	void increaseItem(QString text);//添加下拉项
	void removeItem(QString account);//删除下拉项,返回该项的位置
	int getListWidgetCount();//获取下拉项的个数
	bool findItem(QString account);

signals:
	void SignalShowAccount(QString account);
	void SignalRemoveItem(int i);
private slots :
	void SlotShowAccount(QString);
	void SlotRemoveAccount(QString);

private:
	QListWidget *m_pListWidget;
};

ComboBox::ComboBox(QWidget *parent)
	: QComboBox(parent)
{
	m_pListWidget = new QListWidget(this);
	this->setModel(m_pListWidget->model());
	this->setView(m_pListWidget);
}

ComboBox::~ComboBox()
{

}

// 解决垂直滚动条出现,多次popup时item错乱的问题  (重要)
void ComboBox::hidePopup()
{
	m_pListWidget->scrollToTop();
	QComboBox::hidePopup();
}

void ComboBox::showPopup()
{
	QComboBox::showPopup();
	m_pListWidget->scrollToTop();
}

void ComboBox::increaseItem(QString text)
{
	AccountItem *account_item = new AccountItem(this);
	account_item->setAccountNumber(text);
	connect(account_item, SIGNAL(SignalShowAccount(QString)), this, SLOT(SlotShowAccount(QString)));
	connect(account_item, SIGNAL(SignalRemoveAccount(QString)), this, SLOT(SlotRemoveAccount(QString)));
	QListWidgetItem *list_item = new QListWidgetItem(m_pListWidget);
	m_pListWidget->setItemWidget(list_item, account_item);
}

void ComboBox::removeItem(QString account)
{
	int list_count = getListWidgetCount();
	for (int i = 0; i < list_count; i++)
	{
		QListWidgetItem *item = m_pListWidget->item(i);
		AccountItem *account_item = (AccountItem *)(m_pListWidget->itemWidget(item));
		QString account_number = account_item->getAccountNumber();
		if (account == account_number)
		{
			m_pListWidget->takeItem(i);
			delete item;
			emit SignalRemoveItem(i);
		}
	}
}

int ComboBox::getListWidgetCount()
{
	return m_pListWidget->count();
}

void ComboBox::SlotShowAccount(QString account)
{
	setEditText(account);
	hidePopup();
	emit SignalShowAccount(account.trimmed());
}

void ComboBox::SlotRemoveAccount(QString account)
{
	hidePopup();
	QMessageBox::StandardButton rb = QMessageBox::question(NULL, QStringLiteral("删除"), QStringLiteral("确定要删除该地区吗?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
	if (rb == QMessageBox::Yes)
	{
		int list_count = getListWidgetCount();
		for (int i = 0; i < list_count; i++)
		{
			QListWidgetItem *item = m_pListWidget->item(i);
			AccountItem *account_item = (AccountItem *)(m_pListWidget->itemWidget(item));
			QString account_number = account_item->getAccountNumber();
			if (account == account_number)
			{
				m_pListWidget->takeItem(i);
				delete item;
				emit SignalRemoveItem(i);
				break;
			}
		}
	}
}

bool  ComboBox::findItem(QString account)
{
	int list_count = getListWidgetCount();
	for (int i = 0; i < list_count; i++)
	{
		QListWidgetItem *item = m_pListWidget->item(i);
		AccountItem *account_item = (AccountItem *)(m_pListWidget->itemWidget(item));
		QString account_number = account_item->getAccountNumber();
		if (account == account_number)
		{
			return true;
		}
	}
	return false;
}


//自定义的下拉项
class AccountItem : public QWidget
{
	Q_OBJECT
public:
	AccountItem(QWidget *parent);
	~AccountItem();

public:
	void setAccountNumber(QString account_text);
	QString getAccountNumber();
private:
	void mousePressEvent(QMouseEvent *event);
signals:
	void SignalRemoveAccount(QString);
	void SignalShowAccount(QString);

	private slots:
	void removeAccount();

private:
	bool m_Mouse_press;
    QLabel * m_pAccount_number;
	QPushButton * m_pDelede_button;

};

AccountItem::AccountItem(QWidget *parent)
	: QWidget(parent)
{
	m_Mouse_press = false;
	m_pAccount_number = new QLabel();
	m_pDelede_button = new QPushButton();

	m_pDelede_button->setIcon(QIcon(":/Close"));
	m_pDelede_button->setFixedSize(20, 20);
	m_pDelede_button->setStyleSheet("background:transparent;");
	connect(m_pDelede_button, SIGNAL(clicked()), this, SLOT(removeAccount()));

	QHBoxLayout *main_layout = new QHBoxLayout();
	main_layout->addWidget(m_pAccount_number);
	main_layout->addStretch();
	main_layout->addWidget(m_pDelede_button);
	main_layout->setContentsMargins(5, 5, 5, 5);
	main_layout->setSpacing(5);
	this->setLayout(main_layout);
}

AccountItem::~AccountItem()
{

}

void AccountItem::setAccountNumber(QString account_text)
{
	m_pAccount_number->setText(account_text);
	m_pAccount_number->setAlignment(Qt::AlignCenter);
}

QString AccountItem::getAccountNumber()
{
	QString account_number_text = m_pAccount_number->text();

	return account_number_text;
}

void AccountItem::removeAccount()
{
	QString account_number_text = m_pAccount_number->text();
	emit SignalRemoveAccount(account_number_text);
}

void AccountItem::mousePressEvent(QMouseEvent *event)
{
	emit SignalShowAccount(m_pAccount_number->text());
}


//调用
{
        m_pAddressBox = new ComboBox(this);
	m_pAddressBox->setEditable(true);
	m_pAddressBox->setMaxVisibleItems(6);
	m_pAddressBox->setStyleSheet("QComboBox QAbstractItemView::item{height:30px;width: 1px;} QComboBox{width: 200px}");
       
        m_pAddressBox->increaseItem(str);  //加入下拉项(str:文本)   
}

你可能感兴趣的:(QT)