QComboBox下拉列表比較常用,用戶可以通過選擇不同的選項來實現不同的操作,如何實現自己的下拉列表呢?
很多人在問QComboBox如何設置選項的高度、代理等一些問題!今天就在此分享一下自己的一些小心得。。。
一、基本應用
QComboBox *network_type = new QComboBox();
1、設置樣式:
邊框色、選項高度、下拉按鈕圖標
network_type->setStyleSheet("QComboBox{border:1px solid gray;}"
"QComboBox QAbstractItemView::item{height:20px;}" //下拉選項高度
"QComboBox::down-arrow{image:url(:/icon/arrowdown);}" //下拉箭頭
"QComboBox::drop-down{border:0px;}"); //下拉按鈕
network_type->setView(new QListView());
2、添加選項
typedef enum
{
PROXY_NONE, //沒有代理
PROXY_BROWSER, //瀏覽器代理
PROXY_HTTP, //HTTP代理
PROXY_SOCKS4, //SOCK4代理
PROXY_SOCK5, //SOCK5代理
}Proxy_Types;
network_type->addItem("none", PROXY_NONE);
network_type->addItem("browser", PROXY_BROWSER);
network_type->addItem("http", PROXY_HTTP);
network_type->addItem("socks4", PROXY_SOCKS4);
network_type->addItem("socks5", PROXY_SOCK5);
network_type->setItemText(0, tr("no proxy"));
network_type->setItemText(1, tr("use browser"));
network_type->setItemText(2, tr("http"));
network_type->setItemText(3, tr("socks4"));
network_type->setItemText(4, tr("socks5"));
3、獲取所選項
Proxy_Types proxy_type = (Proxy_Types)(network_type->currentIndex());
4、點擊不同選項執行的事件
connect(network_type, SIGNAL(currentIndexChanged(int)), this, SLOT(proxyChange(int)));
效果如下:
二、設置代理
好了,此代理非彼代理也,綜上所說的代理為QComboBox的選項,這里要說明的是QComboBox的代理組件!
先看此圖:
可以看出下拉選項中不僅包含有文本信息,而且含包含有相應的組件!其實這是模擬的一個用戶選擇輸入框,用戶不僅可以輸入帳號,而且可以選擇想要登錄的帳號,並且可進行帳號的刪除!
代理選項包含一個用戶帳號文本和一個刪除按鈕,mouseReleaseEvent函數主要獲取此代理的文本,用於顯示在QComboBox中,刪除按鈕執行的是獲取代理的文本,根據不同的文本刪除對應的帳號信息!
(1)設定代理組成
AccountItem::AccountItem(QWidget *parent)
: QWidget(parent)
{
mouse_press = false;
account_number = new QLabel();
delede_button = new QPushButton();
QPixmap pixmap(":/loginDialog/delete");
delede_button->setIcon(pixmap);
delede_button->setIconSize(pixmap.size());
delede_button->setStyleSheet("background:transparent;");
connect(delede_button, SIGNAL(clicked()), this, SLOT(removeAccount()));
QHBoxLayout *main_layout = new QHBoxLayout();
main_layout->addWidget(account_number);
main_layout->addStretch();
main_layout->addWidget(delede_button);
main_layout->setContentsMargins(5, 5, 5, 5);
main_layout->setSpacing(5);
this->setLayout(main_layout);
}
AccountItem::~AccountItem()
{
}
void AccountItem::setAccountNumber(QString account_text)
{
account_number->setText(account_text);
}
QString AccountItem::getAccountNumber()
{
QString account_number_text = account_number->text();
return account_number_text;
}
void AccountItem::removeAccount()
{
QString account_number_text = account_number->text();
emit removeAccount(account_number_text);
}
void AccountItem::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
mouse_press = true;
}
}
void AccountItem::mouseReleaseEvent(QMouseEvent *event)
{
if(mouse_press)
{
emit showAccount(account_number->text());
mouse_press = false;
}
}
(2)添加代理至QComboBox:
account_combo_box = new QComboBox();
list_widget = new QListWidget();
account_combo_box->setModel(list_widget->model());
account_combo_box->setView(list_widget);
account_combo_box->setEditable(true); //設置QComboBox可編輯
for(int i=0; i<3; i++)
{
AccountItem *account_item = new AccountItem();
account_item->setAccountNumber(QString("safe_") + QString::number(i, 10) + QString("@sina.com"));
connect(account_item, SIGNAL(showAccount(QString)), this, SLOT(showAccount(QString)));
connect(account_item, SIGNAL(removeAccount(QString)), this, SLOT(removeAccount(QString)));
QListWidgetItem *list_item = new QListWidgetItem(list_widget);
list_widget->setItemWidget(list_item, account_item);
}
(3)實現代理選項進行的操作
//將選項文本顯示在QComboBox當中
void LoginDialog::showAccount(QString account)
{
account_combo_box->setEditText(account);
account_combo_box->hidePopup();
}
//刪除帳號時,彈出提示框,與用戶進行交互,告知是否確定要刪除此帳號的所有信息!
void LoginDialog::removeAccount(QString account)
{
account_combo_box->hidePopup();
msg_box->setInfo(tr("remove account"), tr("are you sure to remove account?"), QPixmap(":/loginDialog/attention"), false);
if(msg_box->exec() == QDialog::Accepted)
{
int list_count = list_widget->count();
for(int i=0; i
{
QListWidgetItem *item = list_widget->item(i);
AccountItem *account_item = (AccountItem *)(list_widget->itemWidget(item));
QString account_number = account_item->getAccountNumber();
if(account == account_number)
{
list_widget->takeItem(i);
delete item;
break;
}
}
}
}
當然,可以試試list_widget的itemClicked()與itemPressed()信號,在指定account_combo_box->setEditText(account)設定文本的時候,會出現一閃而過的情形,所以還是得自定義鼠標事件。
只要你願意,代理任你設置: