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

运行图

相关推荐
264玫瑰资源库2 小时前
问道数码兽 怀旧剧情回合手游源码搭建教程(反查重优化版)
java·开发语言·前端·游戏
普if加的帕2 小时前
java Springboot使用扣子Coze实现实时音频对话智能客服
java·开发语言·人工智能·spring boot·实时音视频·智能客服
安冬的码畜日常3 小时前
【AI 加持下的 Python 编程实战 2_10】DIY 拓展:从扫雷小游戏开发再探问题分解与 AI 代码调试能力(中)
开发语言·前端·人工智能·ai·扫雷游戏·ai辅助编程·辅助编程
小刘同学++3 小时前
Qt 处理 XML 数据
xml·qt
Quz3 小时前
使用Qt Quick Controls创建自定义日历组件
qt·ui·交互
朝阳5813 小时前
Rust项目GPG签名配置指南
开发语言·后端·rust
朝阳5813 小时前
Rust实现高性能目录扫描工具ll的技术解析
开发语言·后端·rust
程高兴3 小时前
基于Matlab的车牌识别系统
开发语言·matlab
牛马baby4 小时前
Java高频面试之并发编程-07
java·开发语言·面试
CodeWithMe4 小时前
【C++】STL之deque
开发语言·c++