在Qt工具箱中有按钮组(Buttons),按钮组包含Push Buttton(按钮),Tool Button(工具按钮),Radio Button(单选按钮),Check Box(复选框),Command Link Button(命令链接按钮),Dialog Button Box(对话框按钮盒)。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/6f0363a7f92842859b24a76a942aa002.png)
Push Buttton主要用于响应用户的点击操作,用于Push Button使用场景较多,本文主要演示关于Push Button的相关内容。主要包括以下几项:
(1)背景颜色变化。使用PushButton充当圆形灯,可点击改变颜色。应用场景如:红绿灯,设备灯等;
(2)文本颜色变化。使用PushButton充当警告指示,可点击改变颜色。应用场景如:告警,警告,提示等;
(3)按钮状体变化。按钮正常、悬停、按压三种状态的颜色变化。制作测试按钮,展示按钮响应效果。
(4)颜色选择按钮。点击按钮弹出颜色对话框,改变按钮背景色;
(5)下拉菜单按钮。点击按钮弹出下拉菜单,选择菜单项;
(6)退出按钮。点击退出按钮,程序退出。
1.功能实现
(1)创建PushButtonDlg窗口,增加圆灯按钮、字体按钮、彩色按钮、选择颜色按钮、弹出下拉菜单和退出等资源。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7219351a963a4e3ea9a33781bbec7b36.png
(2)PushButtonDlg .h内容
#ifndef PUSHBUTTONDLG_H
#define PUSHBUTTONDLG_H
#include <QWidget>
#include <QMenu>
namespace Ui {
class PushButtonDlg;
}
class PushButtonDlg : public QWidget
{
Q_OBJECT
public:
explicit PushButtonDlg(QWidget *parent = 0);
~PushButtonDlg();
private slots:
void on_pushButton_choose_clicked();
void on_pushButton_RedLamp_clicked();
void on_pushButton_GreenLamp_clicked();
void on_pushButton_YellowLamp_clicked();
void on_pushButton_RedFont_clicked();
void on_pushButton_GreenFont_clicked();
void on_pushButton_YellowFont_clicked();
void on_pushButton_BlueFont_clicked();
void on_pushButton_BlueLamp_clicked();
void on_pushButton_Menu_clicked();
void on_pushButton_Quit_clicked();
private:
Ui::PushButtonDlg *ui;
};
#endif // PUSHBUTTONDLG_H
1.1背景颜色变化
以蓝色圆灯、红色圆灯、绿色圆灯、黄色圆灯演示背景颜色变化。
![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/ffe5aea0a1244b2b9b5f677a009530cd.png
void PushButtonDlg::on_pushButton_BlueLamp_clicked()
{
if(ui->pushButton_BlueLamp->GetBackgroundColor() == QColor(0,0,0))
ui->pushButton_BlueLamp->SetPushButtonQss(30,5,14,"#0000FF","#FFFFFF","#0000FF","#FFFFFF","#0000FF","#FFFFFF");
else
ui->pushButton_BlueLamp->SetPushButtonQss(30,5,14,"#000000","#FFFFFF","#000000","#FFFFFF","#000000","#FFFFFF");
}
void PushButtonDlg::on_pushButton_RedLamp_clicked()
{
if(ui->pushButton_RedLamp->GetBackgroundColor() == QColor(0,0,0))
ui->pushButton_RedLamp->SetPushButtonQss(30,5,14,"#ff0000","#FFFFFF","#ff0000","#FFFFFF","#ff0000","#FFFFFF");
else
ui->pushButton_RedLamp->SetPushButtonQss(30,5,14,"#000000","#FFFFFF","#000000","#FFFFFF","#000000","#FFFFFF");
}
void PushButtonDlg::on_pushButton_GreenLamp_clicked()
{
if(ui->pushButton_GreenLamp->GetBackgroundColor() == QColor(0,0,0))
ui->pushButton_GreenLamp->SetPushButtonQss(30,5,14,"#00FF00","#FFFFFF","#00FF00","#FFFFFF","#00FF00","#FFFFFF");
else
ui->pushButton_GreenLamp->SetPushButtonQss(30,5,14,"#000000","#FFFFFF","#000000","#FFFFFF","#000000","#FFFFFF");
}
void PushButtonDlg::on_pushButton_YellowLamp_clicked()
{
if(ui->pushButton_YellowLamp->GetBackgroundColor() == QColor(0,0,0))
ui->pushButton_YellowLamp->SetPushButtonQss(30,5,14,"#FFFF00","#FFFFFF","#FFFF00","#FFFFFF","#FFFF00","#FFFFFF");
else
ui->pushButton_YellowLamp->SetPushButtonQss(30,5,14,"#000000","#FFFFFF","#000000","#FFFFFF","#000000","#FFFFFF");
}
1.2文本颜色变化
以红色、蓝色、黄色、绿色字体按钮展示文本颜色变化。
1.3按钮状态变化
以彩色按钮1、2、3、4展示按钮状态变化
1.4颜色选择按钮
以颜色选择按钮展示颜色选择功能。