在Qt工具栏上实现矩阵并排的按钮效果源码

devtools/2024/9/24 13:05:28/

如果这个要用MFC去实现头皮都得掉一层,建议大家以后要写GUI方面的小工具尽量转QT或其他吧,MFC真不适合搞这种花里胡哨的界面.

在Qt工具栏上实现矩阵并排的按钮效果源码如下:

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QGridLayout>
#include <QPushButton>class QxBtnMatrix : public QWidget {
public:QxBtnMatrix (QWidget *parent = nullptr) : QWidget(parent) {QGridLayout *layout = new QGridLayout(this);// 添加上排矩阵按钮for (int i = 0; i < 4; ++i){QPushButton *button = new QPushButton(QString("A%1").arg(i + 1));layout->addWidget(button, 0, i);m_lstTypeA.push_back(button);connect(button, &QPushButton::clicked, this, &QxBtnMatrix::OnClickTypeA);}// 添加下排矩阵按钮for (int i = 0; i < 4; ++i){QPushButton *button = new QPushButton(QString("B%1").arg(i + 1));layout->addWidget(button, 1, i);m_lstTypeB.push_back(button);connect(button, &QPushButton::clicked, this, &QxBtnMatrix::OnClickTypeB);}// 设置布局间距和按钮大小layout->setSpacing(2); // 设置按钮之间的间距layout->setContentsMargins(1, 1, 1, 1); // 设置边距// 调整按钮大小QList<QPushButton *> buttons = findChildren<QPushButton *>();for (QPushButton *button : buttons) {button->setFixedSize(24, 16); // 设置按钮固定大小button->setStyleSheet("QPushButton {""border: 2px solid #8f8f91;""border-radius: 6px;""min-width: 24px;""background-color: #f6f7fa;""}""QPushButton:pressed {""background-color: #dadbde;""}");}}private slots:void OnClickTypeA(){QPushButton *clickedBtn = qobject_cast<QPushButton  *>(sender());if(!clickedBtn)return;for(QPushButton *btn : m_lstTypeA){if(btn != clickedBtn){btn->setStyleSheet("QPushButton {""border: 2px solid #8f8f91;""border-radius: 6px;""min-width: 24px;""background-color: #f6f7fa;""}""QPushButton:pressed {""background-color: #dadbde;""}");}else{btn->setStyleSheet("QPushButton {""border: 2px solid #8f8f91;""border-radius: 6px;""min-width: 24px;""background-color: #ff0000;""}""QPushButton:pressed {""background-color: #ff0000;""}");}}}void OnClickTypeB(){QPushButton *clickedBtn = qobject_cast<QPushButton  *>(sender());if(!clickedBtn)return;for(QPushButton *btn : m_lstTypeB){if(btn != clickedBtn){btn->setStyleSheet("QPushButton {""border: 2px solid #8f8f91;""border-radius: 6px;""min-width: 24px;""background-color: #f6f7fa;""}""QPushButton:pressed {""background-color: #dadbde;""}");}else{btn->setStyleSheet("QPushButton {""border: 2px solid #8f8f91;""border-radius: 6px;""min-width: 24px;""background-color: #ff0000;""}""QPushButton:pressed {""background-color: #ff0000;""}");}}}public:QList<QPushButton *>    m_lstTypeA, m_lstTypeB;
};MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建按钮矩阵小部件QxBtnMatrix *btnMatrix = new QxBtnMatrix(this);// 将按钮矩阵小部件添加到工具栏中ui->toolBar->addSeparator();ui->toolBar->addWidget(btnMatrix);ui->toolBar->addSeparator();// 添加打开ui->toolBar->addAction(ui->actionOpen);
}MainWindow::~MainWindow()
{delete ui;
}

运行后的效果如下图:


http://www.ppmy.cn/devtools/39925.html

相关文章

2024年滴滴前端一二三面(汽车资产管理)

面试前&#xff0c;先找面经哥&#xff0c;点击此处查看更多面经 一面 1、聊项目 2、实现 TypeScript 的 Await 3、手写 compose 4、用 Vue 或者 React 实现一个组件&#xff0c;组件通过 checkbox 控制列表传入数据每一列的全选反选 二面 1、项目问题以及实现细节 2、小程序…

C语言⼤⼩端模式对 union 类型数据有什么影响?

一、问题 计算机都是以⼋位⼀个字节为存储单位的&#xff0c;所以⼀个 16 位的整型就存在两种可能的存储顺序&#xff1a;⼤端模式和⼩端模式。那么⼤⼩端模式对共⽤体类型中的数据存储又有什么影响呢&#xff1f; 二、解答 1.⼤⼩端模式概述 考虑⼀个 int 型整数 29&#xf…

AIGC|将GPTBots与10000+主流软件连接,实现应用场景全覆盖

一、自动化工作流的无限可能&#xff0c;由AI带来 当前市场上存在许多自动化工作流工具&#xff0c;这些工具在很大程度上提升了人们的工作效率&#xff0c;为企业节省了大量时间和人力成本。然而&#xff0c;这些工具并非万能&#xff0c;它们在实际应用中仍存在一定的局限性…

实现树莓派DS18B20读取温度(OneWire)

简介 使用的是树莓派3B, Go编程实现OneWire方式读取DS18B20温度。 接线 DS18B20 包含经典三线&#xff0c; VCC和GND自不必说&#xff0c; 主要的是DQ线&#xff0c; 需要接4.7K的上拉电阻&#xff0c; 即4.7K欧姆的电阻接到DQ和VCC&#xff0c; 否则树莓派识别不到DS18B20&am…

【生信技能树】数据挖掘全流程

R包的安装&#xff0c;每次做分析的时候先运行这段代码把R包都安装好了&#xff0c;这段代码不需要任何改动&#xff0c;每次分析直接运行。 options("repos""https://mirrors.ustc.edu.cn/CRAN/") if(!require("BiocManager")) install.packag…

招展工作的接近尾声“2024上海国际科技创新展会”即将盛大开幕

2024上海国际科技创新展会&#xff0c;即将于6月中旬在上海新国际博览中心盛大召开。随着招展工作的接近尾声&#xff0c;目前仍有少量余位可供各企业和机构预定。这一盛大的科技展会&#xff0c;将汇聚全球智能科技领域的精英&#xff0c;共同展示最新的科技成果&#xff0c;探…

x_t格式介绍

x_t格式 X_T格式&#xff0c;通常被称为Parasolid文件格式&#xff0c;是一种用于3D CAD数据交换的文件格式。Parasolid本身是一个几何建模内核&#xff0c;由Siemens PLM Software开发和维护&#xff0c;广泛应用于许多主流CAD、CAM、CAE系统中&#xff0c;如SolidWorks、Sol…

安全继电器的使用和作用

目录 一、什么是安全继电器 二、安全继电器的接线方式 三、注意事项 四、总结 一、什么是安全继电器 安全继电器是由多个继电器与硬件电路组合而成的一种模块&#xff0c;是一种电路组成单元&#xff0c;其目的是要提高安全因素。完整点说&#xff0c;应该叫成安全继电器模…