当前位置:网站首页>Qt QPushButton详解
Qt QPushButton详解
2022-07-05 23:34:00 【Mr.codeee】
1.QPushButton概述
QAbstractButton 类是所有按钮控件类的基类,包含很多通用的按钮功能。
QPushButton继承自QAbstractButton,它可以放置文字、图片等。
它是最常用的小部件。按(点击)按钮来命令计算机执行某些操作或回答问题。典型的按钮有“确定”、“应用”、“取消”、“关闭”、“是”、“否”和“帮助”。
2.常用方法
QPushButton 类提供了 3 个构造函数,分别是:
QPushButton(QWidget *parent = Q_NULLPTR)
QPushButton(const QString &text, QWidget *parent = Q_NULLPTR)
QPushButton(const QIcon &icon, const QString &text, QWidget *parent = Q_NULLPTR)parent 参数用于指定父窗口;text 参数用于设置按钮上要显示的文字;icon 参数用于设置按钮上要显示的图标。
其他常用方法:
| void setText(const QString &text) | 设置显示的文字 |
| void setIcon(const QIcon &icon) | 设置图标 |
| void setShortcut(const QKeySequence &key) | 设置快捷键 |
| void setCheckable(bool) | 设置是否能够选中 |
| void setChecked(bool) | 设置是否选中 |
| void setFont(const QFont &) | 设置字体 |
| void setEnabled(bool) | 设置是否能按下 |
| void setAutoDefault(bool) | 当用户按下 Enter 回车键时,是否触发点击按钮的事件。 |
| void setAutoExclusive(bool) | 此属性保存是否启用了自动独占 |
常用信号:
有鼠标点击、按下、抬起信号。
void clicked(bool checked = false)
void pressed()
void released()
3.示例
3.1设置文字、图片。
QFont font;
font.setBold(true); //设置粗体
ui->pushButton->setFont(font);
QPixmap pixmap("test.png");
QIcon icon(pixmap);
ui->pushButton_2->setIcon(icon);设置文字和图片一起出现
可以看到任何时候只有一个高亮。

ui->pushButton->setCheckable(true);
ui->pushButton_2->setCheckable(true);
ui->pushButton_3->setCheckable(true);
ui->pushButton_4->setCheckable(true);
ui->pushButton->setAutoExclusive(true);
ui->pushButton_2->setAutoExclusive(true);
ui->pushButton_3->setAutoExclusive(true);
ui->pushButton_4->setAutoExclusive(true);3.3按钮三态设置
按钮有三种状态,鼠标 悬停、按下、默认。

QString a1 = "\
QPushButton#pushButton{\
border-image: url(:/1.png);\
}\
QPushButton#pushButton:hover{\
border-image: url(:/2.png);\
}"\
"QPushButton#pushButton:pressed{\
border-image: url(:/3.png);\
}";
ui->pushButton->setStyleSheet(a1);3.4其他样式表设置

设置圆角,边框,字体颜色,字体大小等属性。
QString a2 = "QPushButton {\
border: 2px solid #8f8f91;\
border-radius: 6px;\
background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #f6f7fa, stop: 1 #dadbde);\
font-size: 24px;\
font-family: Microsoft YaHei;\
color: #ff0000;\
min-width: 80px;\
}";
ui->pushButton->setStyleSheet(a2);3.5QSS样式表参考
边栏推荐
- MySQL (1) -- related concepts, SQL classification, and simple operations
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
- TS type declaration
- TVS管 与 稳压二极管参数对比
- SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
- 《牛客刷verilog》Part III Verilog企业真题
- GFS分布式文件系統
- Difference between out of band and in band
- Why use weak pointers for delegation- Why use weak pointer for delegation?
猜你喜欢

Research notes I software engineering and calculation volume II (Chapter 1-7)

C reflection and type

Scala concurrent programming (II) akka

20220703 周赛:知道秘密的人数-动规(题解)

总结了 800多个 Kubectl 别名,再也不怕记不住命令了!

698. 划分为k个相等的子集 ●●

Neural structured learning 4 antagonistic learning for image classification

698. Divided into k equal subsets ●●

进击的技术er——自动化

rsync远程同步
随机推荐
[Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
White hat talks about web security after reading 2
asp. Net pop-up layer instance
Switching power supply buck circuit CCM and DCM working mode
Comparison between webgl and webgpu [3] - vertex buffer
Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
Dynamic planning: robbing families and houses
C reflection and type
Cwaitabletimer timer, used to create timer object access
21. PWM application programming
用列表初始化你的vector&&initializer_list简介
Open source CRM customer relationship system management system source code, free sharing
2022.6.20-6.26 AI industry weekly (issue 103): new little life
orgchart. JS organization chart, presenting structural data in an elegant way
5. Logistic regression
Code farmers to improve productivity
STM32__06—单通道ADC
哪些偏门项目可以做到?自媒体做到月赚一万以上很难吗?
C# 文件与文件夹操作
Introduction to JVM