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

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

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

cpp 复制代码
#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;
}

运行后的效果如下图:

相关推荐
CodeCraft Studio26 分钟前
借助Aspose.HTML控件,在 Python 中将 HTML 转换为 Markdown
开发语言·python·html·markdown·aspose·html转markdown·asposel.html
QQ_43766431427 分钟前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
aramae28 分钟前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
封奚泽优1 小时前
使用Python实现单词记忆软件
开发语言·python·random·qpushbutton·qtwidgets·qtcore·qtgui
liulilittle2 小时前
C++/CLI与标准C++的语法差异(一)
开发语言·c++·.net·cli·clr·托管·原生
daixin88482 小时前
什么是缓存雪崩?缓存击穿?缓存穿透?分别如何解决?什么是缓存预热?
java·开发语言·redis·缓存
小狄同学呀2 小时前
VS插件报错,g++却完美编译?API调用错因分析
c++
程序员编程指南2 小时前
Qt 数据库连接池实现与管理
c语言·数据库·c++·qt·oracle
你我约定有三2 小时前
RabbitMQ--消息丢失问题及解决
java·开发语言·分布式·后端·rabbitmq·ruby
小乖兽技术2 小时前
C#与C++交互开发系列(二十四):WinForms 应用中嵌入C++ 原生窗体
c++·c#·交互