qt-内置图片遍历-Lambda按钮

内置图片遍历-Lambda按钮

知识点

使用新的connect语法连接信号和槽 --Lambda 使用

cpp 复制代码
connect(btn, &QToolButton::clicked, this, [this, btn,index]() {  
    onToolButtonClicked(btn)}); // Lambda表达式中调用成员函数,并传递btn  
槽函数中使用
onToolButtonClicked


void Widget::onToolButtonClicked(QToolButton *button)
{
    bool ok;
    int index = button->text().toInt(&ok);
    if (ok) {
        // 显示序号,这里以QMessageBox为例
        qDebug() <<index;
    }
}

Qt遍历枚举容器

cpp 复制代码
   auto me = QMetaEnum::fromType<QStyle::StandardPixmap>();
    for(int i = 0; i < me.keyCount(); i++) {
        qDebug() << me.key(i) << me.value(i);
    }

widget.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include <QLabel>
#include <QComboBox>
#include <QToolButton>

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    void init();
private slots:
    void changeStyle(const QString &text);
    void onToolButtonClicked(QToolButton *button);
private:
    QComboBox *comboBox;
    QLabel *labelStyle;
};
#endif // WIDGET_H

widget.cpp

cpp 复制代码
#include "widget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QComboBox>
#include <QToolButton>
#include <QApplication>
#include <QStyleFactory>
#include <QDebug>
#include <QObject>
#include <QMetaEnum>
#include <QLabel>
#include <QMessageBox>


const int ROW = 8;
const int COL = 10;

Widget::Widget(QWidget *parent)
    : QWidget(parent),
    comboBox(nullptr),
    labelStyle(nullptr)
{
    init();
}

Widget::~Widget()
{
}

void Widget::init()
{
    labelStyle = new QLabel(QStringLiteral("风格:"));
    comboBox = new QComboBox;
    connect(comboBox,  QOverload<const QString &>::of(&QComboBox::currentIndexChanged), this, &Widget::changeStyle);
    comboBox->addItems(QStyleFactory::keys());

    auto hLayout = new QHBoxLayout;
    hLayout->addWidget(labelStyle);
    hLayout->addWidget(comboBox);
    hLayout->setStretch(1, 1);

    auto gridLayout = new QGridLayout;
    auto me = QMetaEnum::fromType<QStyle::StandardPixmap>();
    for(int i = 0; i < me.keyCount(); i++) {
        qDebug() << me.key(i) << me.value(i);
    }
    int index = 0;
    for (int i = 0; i < ROW; i++) {
        for (int j = 0; j < COL; j++) {
            if (index < me.keyCount()) {
                auto btn = new QToolButton;
                btn->setToolButtonStyle(Qt::ToolButtonFollowStyle);
                btn->setText(QStringLiteral("%1").arg(index));
                btn->setIcon(QApplication::style()->standardIcon(QStyle::StandardPixmap(index)));
                btn->setIconSize(QSize(40, 40));
                //添加事件
                connect(btn, &QToolButton::clicked, this, [this, btn, index]() {
                    onToolButtonClicked(btn);
                });
                gridLayout->addWidget(btn, i, j, Qt::AlignCenter);
                index++;
            }
        }
    }
   
    auto vLayout = new QVBoxLayout;
    vLayout->addLayout(hLayout);
    vLayout->addLayout(gridLayout);

    this->setLayout(vLayout);
    this->setWindowTitle(QStringLiteral("Qt内置标准图标Demo"));
}

void Widget::changeStyle(const QString &text)
{
    QApplication::setStyle(text);
}

void Widget::onToolButtonClicked(QToolButton *button)
{
    bool ok;
    int index = button->text().toInt(&ok);
    if (ok) {
        // 显示序号,这里以QMessageBox为例
        qDebug() <<index;
          QMessageBox::information(this, "序号", QString("你点击了序号 %1").arg(index));
    }
}

main.cpp

cpp 复制代码
#include "widget.h"

#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

#ifdef QT_NO_DEBUG
    qDebug() << "Release mode:";
#else
    qDebug() << "Debug mode:";
#endif
    Widget w;
    w.show();
    return a.exec();
}

运行图

相关推荐
kruptos5 小时前
分布式系统怎么“选主“?Raft 共识一次讲清
开发语言·分布式·php·共识算法
吴声子夜歌5 小时前
ApacheCommons——commons-cli(命令行参数解析)
java·开发语言·apache
benchmark_cc6 小时前
REST API 和 Python SDK 应该怎么选?量化交易数据接口选型实战
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
王的宝库6 小时前
Go 项目结构:从单文件到标准工程布局
开发语言·后端·golang
Tairitsu_H6 小时前
[C++] 深入理解红黑树:封装set与map
开发语言·c++·set·map·红黑树·模拟实现
泡海椒6 小时前
JQuick-Curl 性能分析:并发场景下的性能表现与调优,第三方接口调用不只要快写也要稳跑
java·开发语言·okhttp
小智老师PMP6 小时前
2026深度解析|PMP第八版与NPDP核心侧重点本质区别(管理类证书怎么选)
开发语言·分布式·算法·职场和发展·产品经理
_林枭_7 小时前
ZW3D二次开发_原生表单_如何设置表单左上角图标
qt·zw3d
江畔柳前堤8 小时前
前台·中台·后台:2026年AI原生时代的架构全景图
开发语言·人工智能·算法·机器学习·架构·scala·ai-native
Figo_Cheung8 小时前
Figo基于RNC理论的宇宙演化第七纪元猜想
开发语言·php