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

目录

常用控件按钮

1.QPushButton

2.QToolButton

3.QRadioButton

4.QCheckBox

5.QCommandLinkButton

6.QDialogButtonBox


常用控件按钮

  • 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()
{
}

编写执行结果:

4.QCheckBox

QCheckBox是一种Qt框架中的部件(widgets),它允许用户选择或取消选择某个选项。它通常用于设置对话框或首选项面板中。QCheckBox组件可以显示标签和选项框,您可以在其上单击以选中或取消选中选项。

QCheckBox类提供了一些方法和信号来处理选项的状态,例如isChecked()方法用于检查选项是否被选中,setCheckState(state)方法用于设置选项的状态等。

QCheckBox的主要特点包括:

  1. 可以显示文本或非文本(如图标)标签
  2. 可以设置三种状态:选中、未选中和半选中
  3. 可以通过绑定到信号和槽来监听选项状态的更改
  4. 可以与其他Qt部件集成,例如QGroupBox、QButtonGroup等

案例分析:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCheckBox>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QCheckBox *cb;
private slots:
    void checkboxstate(int);
};
#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->setWindowTitle("QCheckBox");
    
    // 设置窗口运行位置
    this->setGeometry(400,300,500,300);

    // 设置窗口颜色
    this->setStyleSheet("QMainWindow {background-color:rgba(255,100,0,100%);}");

    cb = new QCheckBox(this);
    cb->setGeometry(30,50,250,50);

    cb->setCheckState(Qt::Checked);
    cb->setText("初始化状态为:Checked状态");

    cb->setTristate();

    connect(cb,SIGNAL(stateChanged(int)),this,SLOT(checkboxstate(int)));

}

MainWindow::~MainWindow()
{
}

void MainWindow::checkboxstate(int state)
{
    switch(state){
    case Qt::Checked:
        cb->setText("选中状态OK");
        break;

    case Qt::Unchecked:
        cb->setText("未选中状态NO");
        break;

    case Qt::PartiallyChecked:
        cb->setText("半选中状态OK");
        break;

    default:
        break;

    }
}

编译执行结果:

5.QCommandLinkButton

QCommandLinkButton是Qt中的一种按钮控件,它是QPushButton的子类。它可以显示一段文字和一个图标,并且支持为按钮设置一个快捷键。它通常用于显示执行某个命令或者打开某个对话框的操作,因为它具有更丰富的内容展示和更加直观的操作反馈。

QCommandLinkButton的特点包括:

  1. 显示丰富,既可以显示文字又可以显示图标;
  2. 支持设置按钮的快捷键;
  3. 支持设置按钮所代表的命令或操作;
  4. 拥有默认按钮的特点,可以自动设置为按下回车键的响应按钮;
  5. 显示效果类似于Windows Vista及更高版本中的控件,并符合Windows用户界面设计规范。

案例分析:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QCommandLinkButton>


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QCommandLinkButton *clb;
private slots:
    void clbClicked();
};
#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 <QDesktopServices> // 引入桌面服务
#include <QUrl> // 引入URL 统一资源定位符(Uniform Resource Locator)”简称为URL

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setWindowTitle("QCommandLinkButton");
    // 设置窗口运行位置
    this->setGeometry(400,300,500,300);


    clb = new QCommandLinkButton("testclb","clicked testclb",this);
    clb->setGeometry(50,100,250,80);

    connect(clb,SIGNAL(clicked()),this,SLOT(clbClicked()));

}

MainWindow::~MainWindow()
{
}

void MainWindow::clbClicked()
{
    // 调用系统服务打开操作
    QDesktopServices::openUrl(QUrl("https://i.csdn.net/#/user-center/profile?spm=1000.2115.3001.5111"));
}

编译执行结果:

6.QDialogButtonBox

QDialogButtonBox类是Qt框架中的一个常用类,它是一个用于显示对话框按钮的集合的小部件。QDialogButtonBox可以使用默认的标准按钮或自定义按钮。标准按钮包括:Ok、Cancel、Save、Discard、Apply、Reset、Close、Yes、No、Abort、Retry和Ignore。QDialogButtonBox还可以使用addButton()方法添加自定义按钮,或者使用removeButton()方法删除按钮。

QDialogButtonBox类通常用于对话框窗口,提供样式统一的标准按钮,方便用户进行交互操作。可以通过其signals和slot机制方便地获取用户交互操作的结果。

案例分析:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDialogButtonBox>
#include <QPushButton>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private:
    QDialogButtonBox *dbb;
    QPushButton *pb;
private slots:
    void dbbpbClicked(QAbstractButton *);
};
#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 <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setWindowTitle("QDialogButtonBox");
    // 设置窗口运行位置
    this->setGeometry(0,0,800,600);

    dbb=new QDialogButtonBox(this);
    dbb->setGeometry(300,200,200,30);

    dbb->addButton(QDialogButtonBox::Cancel);
    dbb->button(QDialogButtonBox::Cancel)->setText("取消");

    pb = new QPushButton("自定义",this);

    dbb->addButton(pb,QDialogButtonBox::ActionRole);

    connect(dbb,SIGNAL(clicked(QAbstractButton *)),this,SLOT(dbbpbClicked(QAbstractButton *)));
}

MainWindow::~MainWindow()
{
}

void MainWindow::dbbpbClicked(QAbstractButton *bt)
{
    if(bt == dbb->button(QDialogButtonBox::Cancel)){
        qDebug()<<"你已经点击【取消】按钮"<<endl;
      }
    else if(bt==pb){
        qDebug()<<"你已经点击【自定义】按钮"<<endl;
    }
    else{

    }
}

编译执行结果:

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱嵌入式的小佳同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值