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

news2024/9/23 3:31:52

目录

常用控件按钮

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{

    }
}

编译执行结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1080046.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2023年中国自动化微生物样本处理系统竞争现状及行业市场规模分析[图]

微生物检测能够对感染性疾病的病原体或者代谢物进行检测分析&#xff0c;是IVD的细分领域之一。2022年中国体外诊断市场规模1424亿元。 2015-2022年中国体外诊断市场规模 资料来源&#xff1a;共研产业咨询&#xff08;共研网&#xff09; 微生物检测由于样本类型多样&#xf…

HttpServletResponse对象

1.介绍 在Servlet API中&#xff0c;定义了一个HttpServletResponse接口&#xff0c;它继承自ServletResponse接口&#xff0c;专门用来封装HTTP响应消息。由于HTTP响应消息分为状态行、响应消息头、消息体三部分&#xff0c;因此&#xff0c;在HttpServletResponse接口中定义…

Netty RPC 实现

1 概念 RPC&#xff0c;即 Remote Procedure Call&#xff08;远程过程调用&#xff09;&#xff0c;调用远程计算机上的服务&#xff0c;就像调用本地服务一样。RPC 可以很好的解耦系统&#xff0c;如 WebService 就是一种基于 Http 协议的 RPC。这个 RPC 整体框架如下&#…

优盘中毒了怎么办?资料如何恢复

在现代社会中&#xff0c;优盘成为我们日常生活与工作中必备的便携式存储设备。然而&#xff0c;正是由于其便携性&#xff0c;优盘也成为病毒感染的主要目标之一。本篇文章将帮助读者了解如何应对优盘中毒的情况&#xff0c;以及如何恢复因病毒感染丢失的资料。 ▶优盘为什么…

【Java】 DirectByteBuffer堆外内存回收

PhantomReference虚引用 在分析堆外内存回收之前&#xff0c;先了解下PhantomReference虚引用。 PhantomReference需要与ReferenceQueue引用队列结合使用&#xff0c;在GC进行垃圾回收的时候&#xff0c;如果发现一个对象只有虚引用在引用它&#xff0c;则认为该对象需要被回…

nvm的简介、安装、使用(简单明了)

一、nvm是什么&#xff1f; nvm是一个node的版本管理工具&#xff0c;可以简单操作node版本的切换、安装、查看。。。等等&#xff0c;与npm不同的是&#xff0c;npm是依赖包的管理工具。 二、nvm的安装。 1、windows包下载地址&#xff1a; 2、点击如下文件进行安装&#…

配置接口策略路由

【微|信|公|众|号&#xff1a;厦门微思网络】 【微思网络www.xmws.cn&#xff0c;成立于2002年&#xff0c;专业培训21年&#xff0c;思科、华为、红帽、ORACLE、VMware等厂商认证及考试&#xff0c;以及其他认证PMP、CISP、ITIL等】 组网需求 如图1所示&#xff0c;缺省情况下…

【TensorFlow2 之013】TensorFlow-Lite

一、说明 在这篇文章中&#xff0c;我们将展示如何构建计算机视觉模型并准备将其部署在移动和嵌入式设备上。有了这些知识&#xff0c;您就可以真正将脚本部署到日常使用或移动应用程序中。 教程概述&#xff1a; 介绍在 TensorFlow 中构建模型将模型转换为 TensorFlow Lite训练…

第九章-线程

初始时&#xff0c;CPU的执行流为进程&#xff1b;当产生了线程概念后&#xff0c;CPU执行流变为了线程&#xff0c;大大增大了一个周期以内进程的执行速度。 线程产生的作用就是为了提速&#xff0c;利用线程提速&#xff0c;原理就是实现多个执行流的伪并行&#xff0c;让处…

vue3前端开发系列 - electron开发桌面程序(2023-10月最新版)

文章目录 1. 说明2. 创建项目3. 创建文件夹electron3.1 编写脚本electron.js3.2 编写脚本proload.js 4. 修改package.json4.1 删除type4.2 修改scripts4.3 完整的配置如下 5. 修改App.vue6. 修改vite.config.ts7. 启动8. 打包安装9. 项目公开地址 1. 说明 本次安装使用的环境版…

提取log文件中的数据,画图

要提取的log格式如下&#xff1a; 代码如下&#xff1a; import reimport matplotlib.pyplot as plt import numpy as npimport argparse from os import path from re import searchclass DataExtractor(object): DataExtrator class def __init__(self, infile, keyword, out…

电脑上播放4K视频需要具备哪些条件?

在电视上播放 4K&#xff08; 4096 2160 像素&#xff09;视频是很简单的&#xff0c;但在电脑设备上播放 4K 视频并不容易。相反&#xff0c;它们有自己必须满足的硬件要求。 如果不满足要求&#xff0c;在电脑上打开 4K 分辨率文件或大型视频文件会导致卡顿、音频滞后以及更…

ROS中的命名空间

ROS中的节点、参数、话题和服务统称为计算图源&#xff0c;其命名方式采用灵活的分层结构&#xff0c;便于在复杂的系统中集成和复用。以下是一些命名的示例&#xff1a; /foo /stanford/robot/name /wg/node1计算图源命名是ROS封装的一种重要机制。每个资源都定义在一个命名空…

微信小程序wxml使用过滤器

微信小程序wxml使用过滤器 1. 新建wxs2. 引用和使用 如何在微信小程序wxml使用过滤器&#xff1f; 犹如Angular使用pipe管道这样子方便&#xff0c;用的最多就是时间格式化。 下面是实现时间格式化的方法和步骤&#xff1a; 1. 新建wxs 插入代码&#xff1a; /*** 管道过滤工…

泡泡玛特,难成“迪士尼”

作者 | 艺馨 豆乳拿铁 排版 | Cathy 监制 | Yoda 出品 | 不二研究 新增长难寻&#xff0c;新故事难讲。泡泡玛特(06682.HK)业绩增长承压的困局&#xff0c;都写在最新的半年报里。 曾经潮玩领域的王者、“潮玩第一股”泡泡玛特&#xff0c;主题城市乐园于9月26日在北京朝阳…

centos下安装配置redis7

1、找个目录下载安装包 sudo wget https://download.redis.io/release/redis-7.0.0.tar.gz 2、将tar.gz包解压至指定目录下 sudo mkdir /home/redis sudo tar -zxvf redis-7.0.0.tar.gz -C /home/redis 3、安装gcc-c yum install gcc-c 4、切换到redis-7.0.0目录下 5、修改…

2023年中国医学影像信息系统市场规模、竞争格局及行业趋势分析[图]

医学影像信息系统简称PACS&#xff0c;与临床信息系统、放射学信息系统、医院信息系统、实验室信息系统同属医院信息系统。医学影像信息系统是处理各种医学影像信息的采集、存储、报告、输出、管理、查询的计算机应用程序。主要包括&#xff1a;预约管理、数据接收、影像处理、…

[读博随笔] 系统安全和论文写作的那些事——不忘初心,江湖再见

很难想象读博这四年的时光意味着什么&#xff0c;是对妻子和儿子深切的思念。我在珞珈山下挑灯夜读&#xff0c;你在贵阳家中独自照顾幼子。怕的不是孑然一身&#xff0c;而是明明已经习惯两个人&#xff0c;又必须各自前行&#xff0c;像单打独斗的勇士。想到千里之外还有一个…

【软考设计师】【计算机系统】E01 计算机硬件组成与CPU

【计算机系统】E01 计算机硬件组成与CPU 硬件组成概述中央处理单元 CPUCPU 组成运算器控制器寄存器组 多核 CPU 硬件组成概述 运算器&#xff1a; 数据加工处理部件&#xff0c;用于完成计算机的各种算术和逻辑运算。控制器&#xff1a; 顾名思义&#xff0c;控制整个CPU的工作…

2023年中国牙线市场规模、竞争现状及行业需求前景分析[图]

牙线是由合成纤维或其他材料制成&#xff0c;或添加香料、色素、活性成分等&#xff0c;用来清洁牙齿邻面附着物的线。能够有效包裹牙齿&#xff0c;对于清洁平面/凸起牙面和牙齿邻接面的牙菌斑效果很好&#xff0c;还可以实现对于牙缝间食物/异物的剔除&#xff0c;有效清洁口…