Qt内置图标速查表

文章目录

更多精彩内容
👉个人内容分类汇总 👈

1、说明🌾

在我们使用Qt开发程序时,需要美化界面总会想到贴图,显示图标,但是下载图标又很麻烦;

Qt其实已经在QStyle类中内置了一些常用的图标,可以直接使用。

Qt6.7后版本在QIcon中也内置了图标。

2、实现效果🌱

Qt6.7以下版本

Qt6.7以上版本

3、主要代码🌳

cpp 复制代码
#include "widget.h"
#include "ui_widget.h"

#include <qlabel.h>
#include <QIcon>
#include <QStyle>
#include <QMetaEnum>
#include <QtMath>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setWindowTitle("Qt内置图标速查表");

    // 将枚举的名称和数值添加进下拉列表中
    QMetaEnum m = QMetaEnum::fromType<QStyle::StandardPixmap>();
    int row = (int)qSqrt(m.keyCount());  // 计算一排多少个
    int x =0;
    int y = 0;
    for(int i = 1; i <= m.keyCount(); ++i)
    {
        QStyle::StandardPixmap icon = QStyle::StandardPixmap(i - 1);   // 遍历获取枚举
        QLabel* label = new QLabel();
        label->setPixmap(this->style()->standardPixmap(icon));
        label->setAlignment(Qt::AlignCenter);  // 图标居中
        label->setToolTip(QString("[%1,%2] ").arg(x).arg(y) + m.key(i - 1));
        label->setFrameShape(QFrame::Box);   // 设置边框
        ui->gridLayout_style->addWidget(label, x, y, 1, 1);
        x++;
        if(i > 0 && i % row == 0)
        {
            y++;
            x = 0;
        }
    }

#if (QT_VERSION > QT_VERSION_CHECK(6, 7, 0))
    int count = (int)QIcon::ThemeIcon::NThemeIcons;
    row = (int)qSqrt(count);  // 计算一排多少个
    x =0;
    y = 0;
    for(int i = 1; i < count; ++i)
    {
        QIcon icon = QIcon::fromTheme(QIcon::ThemeIcon(i - 1));
        QLabel* label = new QLabel();
        label->setPixmap(icon.pixmap(36));
        label->setAlignment(Qt::AlignCenter);  // 图标居中
        label->setToolTip(QString("[%1,%2] ").arg(x).arg(y) + icon.name());
        label->setFrameShape(QFrame::Box);   // 设置边框
        ui->gridLayout_icon->addWidget(label, x, y, 1, 1);
        x++;
        if(i > 0 && i % row == 0)
        {
            y++;
            x = 0;
        }
    }
#endif

}

Widget::~Widget()
{
    delete ui;
}
相关推荐
whoarethenext34 分钟前
初始https附带c/c++源码使用curl库调用
服务器·c++·qt·https·curl
enyp801 小时前
麒麟系统(基于Ubuntu)上使用Qt编译时遇到“type_traits文件未找到”的错误
linux·qt·ubuntu
道剑剑非道2 小时前
QT开发技术【qcustomplot 曲线与鼠标十字功能】
开发语言·qt·计算机外设
Ethon_王8 小时前
走进Qt--QWidget基类详解
c++·qt
狗蛋儿l12 小时前
qt 3d航迹图
开发语言·qt·3d
Ethon_王15 小时前
走进Qt--信号与槽机制详解与实战
c++·qt
十五年专注C++开发15 小时前
Qt中的全局函数讲解集合(全)
开发语言·c++·qt·算法
.hopeful.17 小时前
基于QT的仿QQ音乐播放器
开发语言·c++·qt
溟洵17 小时前
【C++ Qt】快速上手 显⽰类控件(Label、LCDNumber、ProcessBar、CalendarWidget)
开发语言·c++·qt