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. ...
随机推荐
- Lucene之删除索引
1.前言 之前的博客<Lucene全文检索之HelloWorld>已经简单介绍了Lucene的索引生成和检索.本文着重介绍Lucene的索引删除. 2.应用场景: 索引建立完成后,因为有些 ...
- 自己写一个jqery的拖拽插件
说实话,jQuery比原生的js好用多了,本来想用原生写的,也写出来的,仅仅是,感觉不像插件,所以用jQuery实现了一版. 实现的功能:能够指定拖拽的边界,在拖拽过程中,能够触发几个自己定义事件 先 ...
- Hibernate核心接口
1.Configuration接口 Configuration负责管理Hibernate的配置信息. 2,SessionFactory接口 SessionFactory负责创建Session实例,能够 ...
- Visual Studio Code中文文档
Visual Studio Code中文文档 Visual Studio Code是一个轻量级但是十分强大的源代码编辑器,重要的是它在Windows, OS X 和Linux操作系统的桌面上均可运行. ...
- 积累的VC编程小技巧之打印相关
1.修改打印预览的ToolBar 为AFX_IDD_PREVIEW_TOOLBAR这个ID创建一个DialogBar.则系统就会用新创建的DialogBar代替系统默认的那个 2.关于打印 1.要打印 ...
- 带有机器人框架的.NET自己主动化測试
Clayton Neal在软件測试和质量保证方面有超过13年的经验,当中有八年的Windows, web,和移动应用程序的測试自己主动化经验.他在測试领域的全部等级都工作过.近期他在Bloomberg ...
- ThinkPhp学习11
原文:ThinkPhp学习11 一.模板的使用 (重点) a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主题文件夹/]和模块名同名的文件夹[Index]/和方法名同名的文件[i ...
- 程序猿的量化交易之路(29)--Cointrader之Tick实体(16)
转载需注明出处:http://blog.csdn.net/minimicall,http://cloudtrade.top Tick:什么是Tick,在交易平台中很常见,事实上就 单笔交易时某仅仅证券 ...
- 《转》Python多线程学习
原地址:http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944771.html 一.Python中的线程使用: Python中使用线程有两种方式 ...
- AWS(0) - Amazon Web Services
Computer EC2 – Virtual Servers in the Cloud EC2 Container Service – Run and Manage Docker Containers ...