内置图片遍历-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();
}