当前位置:网站首页>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样式表参考
边栏推荐
- Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
- Opencvsharp (C openCV) shape detection and recognition (with source code)
- 【LeetCode】5. Valid Palindrome·有效回文
- 保研笔记二 软件工程与计算卷二(13-16章)
- In C#, why can't I modify the member of a value type instance in a foreach loop?
- Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
- 帶外和帶內的區別
- The use of El cascader and the solution of error reporting
- Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
- CIS基准测试工具kube-bench使用
猜你喜欢

Initialize your vector & initializer with a list_ List introduction

Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste

MySQL replace primary key delete primary key add primary key

Pyqt control part (I)

【LeetCode】5. Valid Palindrome·有效回文

用列錶初始化你的vector&&initializer_list簡介

无刷驱动设计——浅谈MOS驱动电路

Comparison of parameters between TVs tube and zener diode

成为程序员的你,后悔了吗?

Bao Yan notes II software engineering and calculation volume II (Chapter 13-16)
随机推荐
进击的技术er——自动化
JVM的简介
Use CAS instead of synchronized
Do you regret becoming a programmer?
CIS基准测试工具kube-bench使用
4 points tell you the advantages of the combination of real-time chat and chat robots
保研笔记一 软件工程与计算卷二(1-7章)
In C#, why can't I modify the member of a value type instance in a foreach loop?
15 MySQL-存储过程与函数
424. 替换后的最长重复字符 ●●
How to get all the values stored in localstorage
Biased sample variance, unbiased sample variance
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
2022.6.20-6.26 AI industry weekly (issue 103): new little life
Latex multiple linebreaks
Online yaml to CSV tool
Spreadjs 15.1 CN and spreadjs 15.1 en
Introduction to JVM
PV静态创建和动态创建
yate. conf