关键字: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获取URL

    //获取完整url (协议名+域名+站点名+文件名+参数) string fullUrl = Request.Url.ToString(); //获取客户端请求的URL信息(不包括主机和端口) str ...

  2. java io文件学习笔记

    File f = new file("D:"+File.separator+"test.txt"); File.separator跨系统文件分隔符 f.crea ...

  3. Android调用相册截取图片遇到的问题

    1.在Android中可以使用如下的方式来调用相册,选择图片进行裁剪使用,昨天在开发的时候遇到一个问题 private void cropBigImageUri(Uri uri, int output ...

  4. Curl上传文件

    curl -v -XPOST -H /thumbnail

  5. mysql root给其它用户授权问题

    今天登录mysql,给其它用户授权遇到问题 mysql> grant all privileges on testdb.* to 'dbuser'@'10.4.14.14' identified ...

  6. VB.net 字符串 分割 及 重新倒序组装

    ''' <summary> ''' split with ">>>>" , and inverted order ''' </summar ...

  7. LeetCode 213

    House Robber II Note: This is an extension of House Robber. After robbing those houses on that stree ...

  8. hihocoder 1049 后序遍历

    #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常 ...

  9. ActiveMQ(5.10.0) - Connection Configuration URI

    An Apache ActiveMQ connection can be configured by explicitly setting properties on the ActiveMQConn ...

  10. Redis - HyperLogLogs

    A HyperLogLog is a probabilistic data structure used in order to count unique things (technically th ...