关键字: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. Fence Repair

    有n(n>=1&&n<=20000)个木棒.现在要将这些木棒还原为一根.每次只能将两根连接成一根.费用为这两根的长度.求还原的最小费用. 输入:n,接下来n个正整数,代表长 ...

  2. c#导出Excel 使用EXCEL进程

    private void exportExcel(string filename, string path,string title, List<ArchivedWcsTask> wcst ...

  3. EXTJS 常用控件的使用

    重要按钮配置项 handler: renderTo: 取得控件及其值 var memo = form.findById('memo');//取得输入控件 alert(memo.getValue()); ...

  4. [改善Java代码]断言绝对不是鸡肋

    建议19: 断言绝对不是鸡肋 在防御式编程中经常会用断言(Assertion)对参数和环境做出判断,避免程序因不当的输入或错误的环境而产生逻辑异常,断言在很多语言中都存在,C.C++.Python都有 ...

  5. 【分割圆】Uva 10213 - How Many Pieces of Land ?

    一个椭圆上有N个点,将这n个点两两相连,问最多能将这个椭圆分成多少片. 理清思路,慢慢推. 首先我们要想到欧拉公式:V+E-F=2 其中V为图上的顶点数,E为边数,F为平面数. 计算时的可以枚举点,从 ...

  6. 【转】创业C2C(Copy To China):停车位共享APP,用户、市政能够买账?

    如果周六中午想开车到旧金山的Mission吃顿早午餐,笔者劝您还是省省吧.因为不光是到了吃饭的地儿排队得耗上一个小时,就是满大街的兜圈子找停车位都能折腾死人.那个时候您或许就明白了,其实最苦的并不是买 ...

  7. 去蓝港在线面试Unity3D的笔试题。难吗?知道答案的在评论里写出来分享

    前一阵子去蓝港面试unity3d程序,在前台登记以后被领到一个吧台前面填2张个人信息表,之后有人送来笔试题,做了1个小时,感觉挺难的.之后被带到下面面试,面试的是一个年龄稍大的(可能是项目经理或者技术 ...

  8. 转:SqlServer2008误操作数据(delete或者update)后恢复数据

    Sqlserver2008误操作数据(delete或者update)后恢复数据(转) 实际工作中,有时会直接在数据库中操作数据,比如对数据进行delete或者update操作,当进行这些操作的时候,如 ...

  9. Oracle 一些操作

    Achivelog ============================ alter system set db_recovery_file_dest='F:\ORACLE\recovery_ar ...

  10. c#winform音乐制作软件

    C#音乐播放器 这个音乐播放器是winform 功能有:(括号中是值得提高的部分) 1:登陆提示(查询数据库) 2:皮肤换肤的功能(能右键或者通过按键就能实现) 3:图片的轮换(图片的地址从数据库中抽 ...