博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Qt颜色下拉框
阅读量:7224 次
发布时间:2019-06-29

本文共 2150 字,大约阅读时间需要 7 分钟。

  上周为了用Qt写一个类似颜色下拉框的东西,查阅了网上的多数相关资料,依然没有我想要的。终于在周四的时候下定决心重写QCombobox类来实现功能,现在把它贴出来,望看到的人,批评指正。废话不多说,先上图:

       

                                       图片1-1

  点击下拉之后,出现的是下拉表格,里面都是button,我用了最简单的setstylesheet直接设置背景,点击颜色按钮之后,我展示的效果是在编辑框位置有颜色label。因为没有创建这些按钮的索引,所以我是直接简单粗暴的用的按钮的cliced()信号,颜色的话我是重写了QPushButton类,发送的是clicked(QString),将颜色直接传过去的。other按钮是可以弹出QColorDialog,Qt自带的就不多说了。效果如图:

        图片1-2

                                 图片1-3

关键代码如下:

1 MyCombobox::MyCombobox(QWidget *parent) : QComboBox(parent) 2 { 3     QTableWidget * tableWidget = new QTableWidget(3, 8); 4     tableWidget->verticalHeader()->setVisible(false); 5     tableWidget->horizontalHeader()->setVisible(false); 6     tableWidget->setShowGrid(false); 7     tableWidget->setSpan(2,0,1,8); 8     int k = 0; 9     for (int i = 0; i < 3; ++i)10     {11         if(i==0||i==1){12             for (int j = 0; j < 8; ++j)13                 {14                     tableWidget->setColumnWidth(j, 32);15                     tableWidget->setRowHeight(j, 24);16                     QStringList colorList = QColor::colorNames();\17                     MyPushButton * itemWidget = new MyPushButton(colorList[k],this);18                     itemWidget->setStyleSheet("QPushButton {
">";}");19 k++;20 tableWidget->setCellWidget(i, j, itemWidget);21 connect(itemWidget,SIGNAL(clicked(QString)),this,SLOT(change(QString)));22 }23 }else{24 other =new QPushButton(this);25 other->setText("other");26 tableWidget->setCellWidget(2, 0, other);27 }28 }29 connect(other,SIGNAL(clicked()),this,SLOT(otherColor()));30 this->setModel(tableWidget->model());31 this->setView(tableWidget);32 }33 void MyCombobox::change(QString colorname)34 {35 QLabel *showColor = new QLabel(this);36 showColor->setStyleSheet("QLabel {
">";}");37 showColor->setGeometry(5,5,240,20);38 showColor->setVisible(true);39 }40 void MyCombobox::otherColor()41 {42 QColor mycolor = QColorDialog::getColor(Qt::white, this);43 this->change(mycolor.name());44 }

  刚接触Qt,写的不好,如果各位有好的想法,我们可以多多沟通,比心

转载于:https://www.cnblogs.com/wyyde/p/7965970.html

你可能感兴趣的文章
浅谈iOS中MVVM的架构设计
查看>>
node.js 中模块的循环调用问题详解
查看>>
ActiveReports 报表应用教程 (6)---分组报表
查看>>
OLEDB操作Excel
查看>>
struts2的json-default和struts-default的区别
查看>>
java中<> 的用法
查看>>
IIS 下配置无后缀的URL ReWrite
查看>>
对Asp.net Mvc 和 jQuery UI使用者的一些忠告
查看>>
Silverlight开发历程—动画(实现跑马灯效果)
查看>>
怎么说???
查看>>
[原]Windows批处理命令学习一
查看>>
AaronYang风格 C语言挑讲[一][基本入门]
查看>>
【oneday_onepage】——The Secret Of Steve<1>
查看>>
javascript基于原型的语言的特点
查看>>
我的爱情1
查看>>
BFS广搜题目(转载)
查看>>
什么是协变量
查看>>
关于Cocos2d-x中地图轮播的实现
查看>>
解读ASP.NET 5 & MVC6系列(11):Routing路由
查看>>
Android Studio Gradle:Resolvedependencies':app:_debugCompile' 问题解决纪录
查看>>