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

运行图

相关推荐
端庄的战斗机2 小时前
javascript 设计模式(文章很长,请自备瓜子,水果和眼药水)
开发语言·javascript·设计模式
Full Stack Developme3 小时前
Java LRU 与 LFU 算法及应用
java·开发语言·算法
C语言小火车4 小时前
C++ 堆排序深度精讲:基于完全二叉树的选择排序进化,最坏情况 O(n log n) 的稳定王者
开发语言·c++·算法·排序算法·堆排序
北冥you鱼4 小时前
abigen 最佳实践:从入门到精通,高效生成 Go 语言合约绑定
开发语言·golang·区块链
ALex_zry5 小时前
C++26 std::complex 结构化绑定详解:auto [re, im] = c
c语言·开发语言·c++
她说..6 小时前
Java 默认值设置方式
java·开发语言·后端·springboot
忧郁的紫菜6 小时前
基础实现:单篇 Markdown 转 Word
开发语言·c#·word
甜美的小天鹅6 小时前
Swifter C#之inline还是不inline,这是个问题
开发语言·c#
小短腿的代码世界6 小时前
Qt Bluetooth源码深度解析:从HCI协议到跨平台API的完整架构
开发语言·qt·架构
北冥you鱼7 小时前
Go 语言读取链上数据:从基础到实战
开发语言·后端·golang