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)设定文本的时候,会出现一闪而过的情形,所以还是得自定义鼠标事件。

只要你愿意,代理任你设置:

Qt之QComboBox(基本应用、代理设置)(转)的更多相关文章

  1. Qt之QComboBox定制(二)

    上一篇文章Qt之QComboBox定制讲到了qt实现自定义的下拉框,该篇文章主要实现了列表式的下拉框,这一节我还将继续讲解QComboBox的定制,而这一节我将会讲述更高级的用法,不仅仅是下拉列表框, ...

  2. QComboBox列表项高度设置

    QComboBox列表项高度设置步骤: 1. 设置代理 QStyledItemDelegate *delegate = new QStyledItemDelegate(this); ui->co ...

  3. JMeter学习-024-JMeter 命令行(非GUI)模式详解(二)-执行代理设置

    闲话少述,接 上文 继续... 5.设置代理 jmeter -n -t JMeter分布式测试示例.jmx -H 20.9.215.90 -P 9999 -l report\01-result.csv ...

  4. android sdk manager 代理设置(送给牛逼的)

    解决android sdk更新慢的问题(公司竟然把sdk更新给墙了). 第一步:如下图 第二部:进入代理设置页面,进行设置.如下图  

  5. sdk更新代理设置

    sdk更新代理设置 http://www.cnblogs.com/zhoujg/p/4560998.html

  6. JAVA HTTP请求 常用的代理设置

    由于公司上网实行代理机制, 而最近一段时间又在研究Web上的OpenApi. 没办法一定要使用代理,我之前有文章介绍了httpclient的代理使用方式, 这里介绍基本java的代理使用方式. 最常使 ...

  7. http错误和异常处理,认证和代理设置

    http错误: import urllib.requestreq = urllib.request.Request('http://www.python.org/fish.html')try:urll ...

  8. Windows Server 2012远程刷新客户端组策略,IE代理设置

    Windows Server 2012远程刷新客户端组策略: 1.PowerShell命令对单台计算机进行刷新: Invoke-GPUpdate -RandomDelayInMinutes 0 -Co ...

  9. 魅族MX2代理设置

    魅族MX2买了快2年了,今天才知道有这个功能,唉 连接一个无线网络,比如我的centos 长按网络名字 选代理设置,设置自己的代理,再也不用SS 或 VPN 的android端了,老是提示ROOT权限 ...

  10. Nginx的安装及反向代理设置

    因为项目的缘故,接触到了Nginx的安装和反向代理设置,和大家分享下. 一.Nginx的下载.安装cd /homewget http://nginx.org/download/nginx-1.0.5. ...

随机推荐

  1. SPI总线(同步)

    一.SPI总线简介 串行外围设备接口SPI(serial peripheral interface)总线技术是Motorola公司推出的一种同步串行接口.SPI 用 于CPU与各种外围器件进行全双工. ...

  2. 【ubuntu】屏幕超时关闭后不能唤醒

    根据 http://blog.csdn.net/longshenlmj/article/details/18081167修改了"启动laptop_mode模式"gedit 的文件 ...

  3. JAVA中怎么处理高并发的情况

    一.背景综述 并发就是可以使用多个线程或进程,同时处理(就是并发)不同的操作. 高并发的时候就是有很多用户在访问,导致系统数据不正确.糗事数据的现象.对于一些大型网站,比如门户网站,在面对大量用户访问 ...

  4. python学习------dictionary和set

    一.dictionary 1.字典的形式:a={‘key’:value,‘key1’:value,................} 2.字典的的key不能重复,是一个不可变对象 3.字典的的查找和添 ...

  5. 在POM 4中,<dependency>中还引入了<scope>可以使用5个值

     在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * compile,缺省值,适用于所有 ...

  6. Jmeter 录制脚本

    Jmeter中有2种方法可以录制脚本.  不过我个人非常不推荐录制脚本,录制的脚本混乱,需要再次加工才能使用. 像我这么精通HTTP协议的人. 一直都是使用Fiddler来抓包,然后自己写脚本. 无论 ...

  7. Linux字符设备

    一.linux系统将设备分为3类:字符设备.块设备.网络设备. 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流的设备,常见的 ...

  8. python讲一个列表写入excel表中

    连接为http://blog.csdn.net/a491057947/article/details/47614263 http://www.crifan.com/export_data_to_exc ...

  9. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  10. mac 下安装jmeter

    1.http://jmeter.apache.org/download_jmeter.cgi 下载jmeter 2.解压包 3.进入解压目录/bin/ 4.sh jmeter