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)设定文本的时候,会出现一闪而过的情形,所以还是得自定义鼠标事件。
只要你愿意,代理任你设置: