Qt 常用控件按钮Button 案例分析-1

简介: Qt 常用控件按钮Button 案例分析

常用控件按钮

  • Push Button: 命令按钮。
  • Tool Button:工具按钮。
  • Radio Button:单选按钮
  • Check Box: 复选框按钮
  • Command Link Button: 命今链接按钮
  • Dialog Button Box : 按钮盒


1.QPushButton

QPushButton 是一种 Qt 应用程序中常用的小部件,它是一个可以呈现文本或图像的简单按钮。用户可以单击这个按钮来触发一个动作或事件。PushButton 可以设置文本,图像或两者的组合。它还支持一些属性,如禁用状态,默认状态和自动默认状态等。PushButton 还可以设置样式表,使其外观看起来与应用程序的整体外观相匹配。可以通过连接到特定的槽函数来处理按钮的点击事件,从而实现自定义操作。QPushButton 继承自QAbstractButton ,还具有QToolButton 和 QCommandLinkButton等可选风格。


案例分析:


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QPushButton>
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QPushButton *pb1,*pb2;
private slots:
    void pushbutton1_clicked();
    void pushbutton2_clicked();
 
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
 
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
 
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle("QPushbutton");
 
    // 设置窗口运行位置 
    this->setGeometry(300,150,500,300);
        
    // 实例化两个命令按钮对象
    pb1 = new QPushButton("命令按钮1",this);
    pb2 = new QPushButton("命令按钮2",this);
 
    // 设置两个QPushButton对象的坐标位置
    pb1->setGeometry(20,20,150,50);
    pb2->setGeometry(20,90,150,50);
    
    // 与信号槽函数连接
    connect(pb1,SIGNAL(clicked()),this,SLOT(pushbutton1_clicked()));
    connect(pb2,SIGNAL(clicked()),this,SLOT(pushbutton2_clicked()));
}
 
MainWindow::~MainWindow()
{
}
 
// 声明对象pb1 pb2的槽函数
void MainWindow::pushbutton1_clicked()
{
    this->setStyleSheet("QMainWindow {background-color:rgba(255,255,0,100%);}"); // 按钮1按下后变黄色
}
 
void MainWindow::pushbutton2_clicked()
{
    this->setStyleSheet("QMainWindow {background-color:rgba(255,0,0,100%);}"); // 按钮2按下后变红色
}

编译执行结果:

2.QToolButton

QToolButton是一种特殊的QPushButton,通常用于工具栏和工具箱中。它可以显示图标和文本,可以在单击时显示选择菜单,并支持不同的显示模式,如文本下拉、图标下拉和菜单模式。


QToolButton的特点包括:


  1. 支持常见的按钮样式,如FlatButton、RaisedButton和ToolButton样式。
  2. 可以显式设置按钮的大小和图标的大小,并支持自动调整大小,以适应不同的按钮内容。
  3. 可以设置一个或多个快捷键,方便用户使用。
  4. 可以设置提示工具提示,以提供有关按钮功能的信息。
  5. 支持菜单模式,可以在单击时显示下拉菜单,提供更多选项。


案例分析:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
#include <QToolBar> // 引入QToolBar类
#include <QToolButton> // 引入QToolButton类
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QToolBar *tbar;
    QToolButton *tbutton;
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
 
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
 
#include <QApplication>
#include <QStyle>
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    //1: 设置窗口标题
    this->setWindowTitle("QToolButton");
    
    //2: 设置窗口运行位置
    this->setGeometry(300,150,500,300);
 
    //3: 将QToolBar对象进行实例化
    tbar = new QToolBar(this);
    tbar->setGeometry(150,75,200,150);
 
    //4: 将QStyle类对象进行实例化,主要目的设置风格,图标是系统自带
    QStyle *sty = QApplication::style();
    QIcon ico = sty->standardIcon(QStyle::SP_TitleBarContextHelpButton);
 
    //5: 将QToolButton对象进行实例化
    tbutton = new QToolButton();
    tbutton->setIcon(ico);
 
    //6: 设置将要显示文本
    tbutton->setText("系统帮助提示");
 
    //7: 调用setToolButtonStyle函数设置tbutton样式,设置文本在图标下方
    tbutton->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
 
    //8: 将tbutton添加到tbar里面
    tbar->addWidget(tbutton);
}
 
MainWindow::~MainWindow()
{
}
 

执行编译结果:

3.QRadioButton

QRadioButton是QT框架提供的一种单选按钮控件,用于在多个选项中选择一项。该控件在界面设计中非常常用,如选择性别、选择颜色、选择字体等。它通常与QButtonGroup结合使用,以便在一组中只能选择一个选项。除了基本功能外,还可以通过设置属性和信号槽等方式对控件进行样式和行为定制。


案例分析:


mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
#include <QRadioButton>
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
 
private:
    QRadioButton *radb1,*radb2;
};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"
 
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(300,150,500,300);
 
    this->setStyleSheet("QMainWindow {background-color:rgba(255,0,0,100%)}");
 
    radb1 = new QRadioButton(this);
    radb2 = new QRadioButton(this);
 
    radb1->setGeometry(20,20,150,40);
    radb2->setGeometry(20,80,150,40);
 
    radb1->setText("选择按钮1");
    radb2->setText("选择按钮2");
 
    radb1->setChecked(true);
    radb2->setChecked(false);
}
 
MainWindow::~MainWindow()
{
}
 

编写执行结果:


Qt 常用控件按钮Button 案例分析-2

https://developer.aliyun.com/article/1507893

相关文章
|
5天前
|
安全 BI 数据库
数据库大作业——基于qt开发的图书管理系统 (一)环境的配置与项目需求的分析
数据库大作业——基于qt开发的图书管理系统 (一)环境的配置与项目需求的分析
|
1月前
|
容器 内存技术
Qt中常用容器组控件介绍和实操-1
Qt中常用容器组控件介绍和实操
|
1月前
|
数据安全/隐私保护 图形学
Qt 输入组控件(Input Widgets)& 显示组控件(Display Widgets)详解
Qt 输入组控件(Input Widgets)& 显示组控件(Display Widgets)详解
|
1月前
|
API 容器
Qt中常用容器组控件介绍和实操-2
Qt中常用容器组控件介绍和实操
|
1月前
|
算法 编译器 Linux
【Qt4 部署】ARM系统上使用Qt 4 进行开发的QWS 等环境变量部署
【Qt4 部署】ARM系统上使用Qt 4 进行开发的QWS 等环境变量部署
43 0
|
5天前
|
关系型数据库 MySQL 项目管理
数据库大作业——基于qt开发的图书管理系统(四)项目目录的整理与绘制登录页面
数据库大作业——基于qt开发的图书管理系统(四)项目目录的整理与绘制登录页面
|
5天前
|
SQL 关系型数据库 MySQL
数据库大作业——基于qt开发的图书管理系统(三)Qt连接Mysql数据库
数据库大作业——基于qt开发的图书管理系统(三)Qt连接Mysql数据库
|
5天前
|
SQL 数据可视化 关系型数据库
数据库大作业——基于qt开发的图书管理系统(二) 相关表结构的设计
数据库大作业——基于qt开发的图书管理系统(二) 相关表结构的设计
|
1月前
|
区块链
【qt】最快的开发界面效率——混合编程3
【qt】最快的开发界面效率——混合编程
41 1