Qt内置图标
- 前言
- 简例
- 示例
前言
Qt内置图标封装在QStyle中,大概七十多个图标,可以直接拿来用。图标的大小:我认为 size = 30 还是可以的.
简例
SP_TitleBarMenuButton,
SP_TitleBarMinButton,
SP_TitleBarMaxButton,
SP_TitleBarCloseButton,
SP_MessageBoxInformation,
SP_MessageBoxWarning,
SP_MessageBoxCritical,
SP_MessageBoxQuestion,
...
//下面这样取出来使用就行
QPixmap pixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMenuButton);
ui->label->setPixmap(pixmap);
示例
- main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
- mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
private:
Ui::MainWindow *ui;
int min_i;
int max_i;
int current_i;
int size ;
};
#endif // MAINWINDOW_H
- mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QStyle>
#include <QDebug>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//下面这样取出来使用就行
//QPixmap pixmap = this->style()->standardPixmap(QStyle::SP_TitleBarMenuButton);
// 遍历 StandardPixmap 枚举
min_i = QStyle::SP_TitleBarMenuButton;
max_i = QStyle::SP_LineEditClearButton;
current_i = min_i;
size = 24;
QStyle::StandardPixmap StandardPixmap = static_cast<QStyle::StandardPixmap>(current_i);
QPixmap pixmap = this->style()->standardPixmap(StandardPixmap);
QSize desiredSize(size, size); // 设置您需要的尺寸
pixmap = pixmap.scaled(desiredSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if(++current_i > max_i)
{
QMessageBox::information(0,"Tips","from min_i begin");
current_i = min_i;
}
QStyle::StandardPixmap StandardPixmap = static_cast<QStyle::StandardPixmap>(current_i);
QPixmap pixmap = this->style()->standardPixmap(StandardPixmap);
QSize desiredSize(size, size); // 设置您需要的尺寸
pixmap = pixmap.scaled(desiredSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
}
void MainWindow::on_pushButton_2_clicked()
{
size++;
QStyle::StandardPixmap StandardPixmap = static_cast<QStyle::StandardPixmap>(current_i);
QPixmap pixmap = this->style()->standardPixmap(StandardPixmap);
QSize desiredSize(size, size); // 设置您需要的尺寸
pixmap = pixmap.scaled(desiredSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
qDebug()<<"size:"<<size;
}
void MainWindow::on_pushButton_3_clicked()
{
if(--size < 15)
{
size++;
}
QStyle::StandardPixmap StandardPixmap = static_cast<QStyle::StandardPixmap>(current_i);
QPixmap pixmap = this->style()->standardPixmap(StandardPixmap);
QSize desiredSize(size, size); // 设置您需要的尺寸
pixmap = pixmap.scaled(desiredSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
qDebug()<<"size:"<<size;
}