在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;
}

运行后的效果如下图:

相关推荐
开发者工具分享22 分钟前
如何应对敏捷转型中的团队阻力
开发语言
gregmankiw28 分钟前
C#调用Rust动态链接库DLL的案例
开发语言·rust·c#
roman_日积跬步-终至千里43 分钟前
【Go语言基础【20】】Go的包与工程
开发语言·后端·golang
秦少游在淮海1 小时前
C++ - string 的使用 #auto #范围for #访问及遍历操作 #容量操作 #修改操作 #其他操作 #非成员函数
开发语言·c++·stl·string·范围for·auto·string 的使用
AAA废品回收站陈师傅1 小时前
68常用控件_QGroupBox的使用
qt
const5441 小时前
cpp自学 day2(—>运算符)
开发语言·c++
心扬1 小时前
python生成器
开发语言·python
明月醉窗台1 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
阿蒙Amon1 小时前
06. C#入门系列【自定义类型】:从青铜到王者的进阶之路
开发语言·c#
虾球xz1 小时前
CppCon 2015 学习:CLANG/C2 for Windows
开发语言·c++·windows·学习