关键字:QT,QComboBox,QLineEdit,QListView,QPushButton,QMenu,QWidgetAction,setStyleSheet

OS:Windows 7

方法一:QComboBox+QLineEdit+QListView

相关问题链接:QComboBox: Can we make the entire combobox clickable, not just the dropdown button (arrow) itself?

为了使整个combobox都是可点击的,所以加个QTComboBoxButton类继承QLineEdit,在mousePressEvent里面showPopup。

class QTComboBoxButton : public QLineEdit
{
Q_OBJECT
public:
QTComboBoxButton(QWidget *parent = );
~QTComboBoxButton(); protected:
void mousePressEvent(QMouseEvent *);
}; QTComboBoxButton::QTComboBoxButton(QWidget *parent /* = 0 */) :
QLineEdit(parent)
{
} QTComboBoxButton::~QTComboBoxButton()
{
} void QTComboBoxButton::mousePressEvent(QMouseEvent * e)
{
QComboBox* combo = dynamic_cast<QComboBox*>(parent());
if(combo)
combo->showPopup();
}

QComboBox+QLineEdit+QListView的使用如下:

QComboBox* myCombo = new QComboBox;
myCombo->setView(new QListView());
myCombo->setEditable(true);
myCombo->setLineEdit(new QTComboBoxButton(m_pAnswer));
myCombo->lineEdit()->setReadOnly(true);
myCombo->addItem("Option1");
myCombo->addItem("Option2");
myCombo->addItem("Option3");
myCombo->setMaxVisibleItems(myCombo->count());
myCombo->lineEdit()->setText("Please select");
QString arrowImagePath = "c:\arrow.png";
myCombo->setStyleSheet("QComboBox {font-family: \"Arial\"; font-size: 13px; padding: 3px 0x 3px 5px;}"
"QComboBox::drop-down {subcontrol-origin: padding; subcontrol-position: top right; width: 30 px; border: 0px;}"
"QComboBox::down-arrow {image: url("+ arrowImagePath +");}");
myCombo->view()->setStyleSheet("QListView {font-family: \"Arial\"; font-size: 13px; outline: 0px;}"
"QListView::item {padding: 3px 0x 3px 5px; border-width: 0px;}"
"QListView::item:selected {background-color: rgb(74, 144, 226);}");

UI效果如下图:

方法二:QPushButton+QMenu+QWidgetAction

相关问题链接:QMenu: How to customize the menu items of QMenu

定制类QTDropDownButton

class QTDropDownButton : public QPushButton
{
Q_OBJECT
public:
QTDropDownButton(QString text, QWidget *parent = nullptr); void addItem(QString text); protected slots:
void menuAboutToShow();
void menuTriggered(QAction *action); private:
QMenu* menu_;
}; QTDropDownButton::QTDropDownButton(QString text, QWidget *parent) :
QPushButton(text, parent)
{
setFlat(true);
menu_ = new QMenu();
setMenu(menu_); connect(menu_, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
connect(menu_, SIGNAL(triggered(QAction*)), this, SLOT(menuTriggered(QAction*))); setStyleSheet("QPushButton {text-align: left; font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; padding: 5px;}"
"QPushButton::menu-indicator {subcontrol-origin: padding; subcontrol-position: right center; right:5px;}");
menu_->setStyleSheet("QLabel {font-family: Arial; font-size: 13pt; padding: 5px 0 5px 0; background-color: transparent;}"
"QLabel:hover {background-color: rgb(51, 153, 255);}");
} void QTDropDownButton::addItem(QString text)
{
if(!menu_)
return; QWidgetAction* wa1 = new QWidgetAction(menu_);
QLabel* l1 = new QLabel(text);
wa1->setDefaultWidget(l1);
menu_->addAction(wa1);
} void QTDropDownButton::menuAboutToShow()
{
if(menu_)
{
menu_->setFixedWidth(this->width());
}
} void QTDropDownButton::menuTriggered(QAction *action)
{
if(!action)
return; QWidgetAction* qwa = dynamic_cast<QWidgetAction*>(action);
if(!qwa)
return; QLabel* ql = dynamic_cast<QLabel*>(qwa->defaultWidget());
if(!ql)
return; setText(ql->text());
}

使用QTDropDownButton

QTDropDownButton* ddb = new QTDropDownButton("Please select");
ddb->addItem("Option1");
ddb->addItem("Option2");

QT定制有标题的扁平化下拉框控件的更多相关文章

  1. FineReport——JS二次开发(隐藏下拉框控件的倒三角)

    在对FR控件进行二次开发的过程中,需要自定义样式,比如下拉框控件带有自动检索的功能,但是又希望它的显示样式如同文本框一样,这时就需要隐藏多余的部分. 在对在线文档的查阅中可以发现很多选择器适用于多种控 ...

  2. DevExpress的下拉框控件ComboxBoxEdit怎样绑定键值对选项

    场景 DevExpress的下拉框控件ComboBoxEdit控件的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1028 ...

  3. DevExpress的下拉框控件ComboBoxEdit控件的使用

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  4. DevExpress的下拉框控件LookUpEdit的使用、添加item选项值、修改默认显示值

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  5. [原创]自己动手实现React-Native下拉框控件

    因项目需要,自己动手实现了一个下拉框组件,最近得空将控件独立出来开源上传到了Github和npm. Github地址(求Star 求Star 求Star 

  6. 用MVC的辅助方法自定义了两个控件:“可编辑的下拉框控件”和“文本框日历控件”

    接触MVC也没多长时间,一开始学的时候绝得MVC结构比较清晰.后来入了门具体操作下来感觉MVC控件怎么这么少还不可以像ASP.net form那样拖拽.这样设计界面来,想我种以前没学过JS,Jquer ...

  7. spinner 下拉框控件

    spinnerMode=dropdown时,为下拉模式spinnerMode=dialog时,会在界面中间弹出Android:popupBackground=”#f0000000”,可以去除spinn ...

  8. QTP测试.NET程序的时候,ComboBox下拉框控件选择后,运行时对象不可见解决方案

    解决方法: 录制时,选择下拉框数据的时候,不要鼠标单击选择,而是要用ENTER(回车键)来选择,才能完成选择,这样录制就OK了.

  9. DropShadowEffect导致下拉框控件抖动

    <!--<Border.Effect> <DropShadowEffect Direction="180" BlurRadius="1" ...

随机推荐

  1. Asp.Net 之 通过调用 WScript.Shell 启动本地 exe 程序时产生“ automation服务器不能创建对象 ”的错误

    我们经常需要通过生成 ActiveXObject("WScript.Shell"); 来调用某一exe文件. 设置网页打印的页眉页脚为空: var HKEY_Root,HKEY_P ...

  2. C# 之 未能映射路径

    在开发中遇到此问题,如下原码,报错:未能映射路径: string filename = Server.MapPath("/logs/log.txt"); 解决方法,从根目录开始: ...

  3. Java中jar命令详解

    做项目的时候我们肯定接触过很多jar包,那么jar包是什么呢?笔者了解到jar是java archive file 的简写.从名字就可以知道,它的应用与Java息息相关.下面就详细介绍如何自己生成ja ...

  4. lisener在web.xml中设置

    /* * servlet监听器开发步骤: * 1.写一个类实现XXXListener接口(6个=3个容器+3个对容器中属性进行操作) * 2.在web.xml中配置<listener> - ...

  5. jar包冲突解决方法

    import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.Enumerati ...

  6. [改善Java代码]非稳定排序推荐使用List

    我们知道Set与List的最大区别就是Set中的元素不可以重复(这个重复指的equals方法的返回值相等),其他方面则没有太大的区别了,在Set的实现类中有一个比较常用的类需要了解一下:TreeSet ...

  7. hdu 1518 深搜

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  8. ListView使用自定义适配器的情况下实现适配器的文本和图标控件点击事件执行Activity界面中的方法

    ListView使用的是自定义适配器,列表项的布局文件中含有文本和图标,实现文本区域和图标区域的点击事件. 实现思路:在自定义适配器MyArrayAdapter 类型中自定义接口和接口方法,分别设置文 ...

  9. Servlet & JSP - Servlet API Overview

    Servlet & Generic & HttpServlet 类图 Servlet 的生命周期 init.service 和 destroy 是 servlet 的生命周期方法,它们 ...

  10. Linux - 文件的压缩与归档

    文件压缩 常用的压缩命令有 gzip.bzip2 等. gzip 命令 命令格式 gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ...  ] 命令参数 -c ...