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

embedded/2024/9/24 14:42:25/

内置图片遍历-Lambda按钮

  • 知识点
  • widget.h
  • widget.cpp
  • main.cpp
  • 运行图

知识点

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


connect(btn, &QToolButton::clicked, this, [this, btn,index]() {  onToolButtonClicked(btn)}); // Lambda表达式中调用成员函数,并传递btn  
槽函数中使用
onToolButtonClickedvoid Widget::onToolButtonClicked(QToolButton *button)
{bool ok;int index = button->text().toInt(&ok);if (ok) {// 显示序号,这里以QMessageBox为例qDebug() <<index;}
}

Qt遍历枚举容器

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

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>#include <QLabel>
#include <QComboBox>
#include <QToolButton>class Widget : public QWidget
{Q_OBJECTpublic: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

#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

#include "widget.h"#include <QApplication>
#include <QDebug>
int main(int argc, char *argv[])
{QApplication a(argc, argv);#ifdef QT_NO_DEBUGqDebug() << "Release mode:";
#elseqDebug() << "Debug mode:";
#endifWidget w;w.show();return a.exec();
}

运行图

在这里插入图片描述


http://www.ppmy.cn/embedded/100839.html

相关文章

VS实用调试技巧

目录 1. 什么是bug 2. 什么是调试(debug) 3. Debug和Release 4. VS调试快捷键 4.1 环境准备 4.2 快捷键调试 5. 监视和内存观察 5.1 监视 5.2 内存 6. 调试举例 7. 编程常见错误归类 7.1 编译型错误 7.2 链接型错误 ​7.3 运行时错误 1. 什么是bug bug本意是&qu…

sheng的学习笔记-AI-半监督学习

AI目录&#xff1a;sheng的学习笔记-AI目录-CSDN博客 基础知识 什么是半监督学习 我们在丰收季节来到瓜田&#xff0c;满地都是西瓜&#xff0c;瓜农抱来三四个瓜说这都是好瓜&#xff0c;然后再指着地里的五六个瓜说这些还不好&#xff0c;还需再生长若干天。基于这些信息&a…

windows删除不了的一些长名字文件,为什么python可以删除?

感谢阅读 windows报错截图windows最大文件路径长度限制为什么基于windows系统运行的python可以完成删除文件名259字符的文件&#xff1f;文件系统的存储方式操作系统和文件系统的关系总结 windows报错截图 windows最大文件路径长度限制 但真的是260字符吗&#xff1f;早期windo…

-bash: whichwhere: command not found

一、由于环境变量没有添加、添加即可 查看mysqldump位置 [rootVM-20-10-centos data]# sudo find / -name mysqldump /usr/local/mysql-5.7.44/bin/mysqldump vim /etc/profile export MYSQL_HOME/usr/local/mysql-5.7.44 export PATH.:${MYSQL_HOME}/bin:$PATHsource /etc/p…

记一个启动有ranger的hiveserver2报错

1&#xff0c;启动hiveserver2报错 2024-08-20T22:21:17,399 INFO [main] thrift.ThriftCLIService: Starting ThriftBinaryCLIService on port 10000 with 5...500 worker threads 2024-08-20T22:21:17,400 INFO [main] service.AbstractService: Service:HiveServer2 is …

flink读写案例合集

文章目录 前言一、flink 写kafka1.第一种使用FlinkKafkaProducer API2.第二种使用自定义序列化器3.第三种使用FlinkKafkaProducer011 API4.使用Kafka的Avro序列化 (没有使用过,感觉比较复杂)5.第五种使用 (强烈推荐使用)二、Flink读kafka三、Flink写其他外部系统前言 提示:这…

【设计模式】组合模式和(宏)命令模式

组合模式 组合模式在对象间形成树形结构&#xff1b;组合模式中基本对象和组合对象被一致对待&#xff1a;无须关心对象有多少层&#xff0c;调用时只需在根部进行调用。 它在我们树型结构的问题中&#xff0c;模糊了简单元素和复杂元素的概念&#xff0c;客户程序可以向处理…

[C语言]一、C语言基础(函数)

G:\Cpp\C语言精讲 6. 函数 6.1函数的基本使用 6.1.1 为什么需要函数 《街霸》游戏中&#xff0c;每次人物出拳、出脚或跳跃等动作都需要编写50-80行的代码&#xff0c;在每次出拳、出脚或跳跃的地方都需要重复地编写这50-80行代码&#xff0c;这样程序会变得很臃肿&#xff…