Qt之QComboBox(基本应用、代理设置)
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、获取选项的值
(1)
使用时,请声明Q_DECLARE_METATYPE(Proxy_Types)
int proxy = network_type->itemData(network_type->currentIndex()).value();
(2)
Proxy_Types proxy_type = (Proxy_Types)network_type->itemData(network_type->currentIndex()).toInt();
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(基本应用、代理设置)的更多相关文章
- Qt之QComboBox定制(二)
上一篇文章Qt之QComboBox定制讲到了qt实现自定义的下拉框,该篇文章主要实现了列表式的下拉框,这一节我还将继续讲解QComboBox的定制,而这一节我将会讲述更高级的用法,不仅仅是下拉列表框, ...
- QComboBox列表项高度设置
QComboBox列表项高度设置步骤: 1. 设置代理 QStyledItemDelegate *delegate = new QStyledItemDelegate(this); ui->co ...
- JMeter学习-024-JMeter 命令行(非GUI)模式详解(二)-执行代理设置
闲话少述,接 上文 继续... 5.设置代理 jmeter -n -t JMeter分布式测试示例.jmx -H 20.9.215.90 -P 9999 -l report\01-result.csv ...
- android sdk manager 代理设置(送给牛逼的)
解决android sdk更新慢的问题(公司竟然把sdk更新给墙了). 第一步:如下图 第二部:进入代理设置页面,进行设置.如下图
- sdk更新代理设置
sdk更新代理设置 http://www.cnblogs.com/zhoujg/p/4560998.html
- JAVA HTTP请求 常用的代理设置
由于公司上网实行代理机制, 而最近一段时间又在研究Web上的OpenApi. 没办法一定要使用代理,我之前有文章介绍了httpclient的代理使用方式, 这里介绍基本java的代理使用方式. 最常使 ...
- http错误和异常处理,认证和代理设置
http错误: import urllib.requestreq = urllib.request.Request('http://www.python.org/fish.html')try:urll ...
- Windows Server 2012远程刷新客户端组策略,IE代理设置
Windows Server 2012远程刷新客户端组策略: 1.PowerShell命令对单台计算机进行刷新: Invoke-GPUpdate -RandomDelayInMinutes 0 -Co ...
- 魅族MX2代理设置
魅族MX2买了快2年了,今天才知道有这个功能,唉 连接一个无线网络,比如我的centos 长按网络名字 选代理设置,设置自己的代理,再也不用SS 或 VPN 的android端了,老是提示ROOT权限 ...
- Nginx的安装及反向代理设置
因为项目的缘故,接触到了Nginx的安装和反向代理设置,和大家分享下. 一.Nginx的下载.安装cd /homewget http://nginx.org/download/nginx-1.0.5. ...
随机推荐
- Yii --Command 任务处理
1.配置,执行任务所需要的组件 任务配置文件:/protected/config/console.php 配置方法跟配置main文件差不多 <?php // This is the confi ...
- 手把手教你修改pcduino系统默认的音频输出
最近要搞个小玩意儿,要用到pcduino的音频输出,但是系统默认的是输出到hdmi的音频,我的显示器上没有喇叭,只能搞个USB声卡.但是系统默认又不是输出到USB,这里我手把手叫你怎么设置系统默认声卡 ...
- Greenplum同步到Oracle
开发提出须要从Greenplum同步到Oracle的解决方式,写了个脚本用于定时调度处理. #!/bin/sh #copy_gp_2_ora.sh if [ $# -ne 1 ]; then ...
- 搜索:POJ2251&POJ1426&POJ3087&POJ2488
图的遍历也称为搜索,就是从图中某个顶点出发,沿着一些边遍历图中所有的顶点,且每个顶点仅被访问一次,遍历可采取两种不同的方式:深度优先搜索(DFS)和广度优先搜索(BFS). 1.DFS算法思想` 从顶 ...
- c#Lamdba表达式与托付
介绍: "Lambda表达式"(lambda expression)是一个匿名函数,在C#3.0中引入了lambda表达式,它是对匿名函数的一种简化,能够包括表达式和语句,而且可用 ...
- 百度GPSutil
================================================= package com.qcar.benz.biz.common; import com.aliba ...
- 如何理解java回电话
同android中间Button的setOnClickListener这个回调案例: Button button = (Button)this.findViewById(R.id.button); b ...
- 数据结构《17》---- 自己主动补齐之《二》----Ternary Search Tree
一. 序言 上一篇文章中,给出了 trie 树的一个实现. 能够看到,trie 树有一个巨大的弊病,内存占用过大. 本文给出还有一种数据结构来解决上述问题---- Ternary Search Tre ...
- Unigine 基础入门
1. 首先要搭建好开发环境: 1)Visual Stodio 已经安装了. 2). Microsoft Windows SDK 7.1 (for Windows 7): https://www.mic ...
- PHP操作Mysql中间BLOB场
1.MySQL在BLOB字段类型 BLOB场的类型用于存储二进制数据. MySQL在.BLOB它是一种类型的一系列.含有:TinyBlob.Blob.MediumBlob.LongBlob.大小上不同 ...