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();
}

运行图

相关推荐
cen__y42 分钟前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第65题】【JVM篇】第25题:谈谈对 OOM 的认识
java·开发语言·jvm
社交怪人1 小时前
【算平均分】信息学奥赛一本通C语言解法(题号2071)
c语言·开发语言
郭涤生2 小时前
不同主机之间网络通信-以太网连接复习
开发语言·rk3588
山居秋暝LS2 小时前
【无标题】RTX00安装paddle OCR,win11不能装最新的,也不能用GPU
开发语言·r语言
卢锡荣2 小时前
单芯通吃,盲插标杆 —— 乐得瑞 LDR6020,Type‑C 全场景互联 “智慧芯”
c语言·开发语言·计算机外设
Xin_ye100862 小时前
C# 零基础到精通教程 - 第七章:面向对象编程(入门)——类与对象
开发语言·c#
AI科技星3 小时前
《数学公理体系·第三部·数术几何》(2026 年版)
c语言·开发语言·线性代数·算法·矩阵·量子计算·agi
审判长烧鸡3 小时前
【Go工具】go-playground是什么组织?官方的?
开发语言·安全·go
kkeeper~3 小时前
0基础C语言积跬步之字符函数与字符串函数(上)
c语言·开发语言