Qt中QIcon图标设置(标题、菜单栏、工具栏、状态栏图标)

卓学电子 2024-06-24 16:35:33 阅读 90

1 exe程序图标概述

在 Windows 操作系统中,程序图标一般会涉及三个地方;

(1) 可执行程序(以及对应的快捷方式)的图标

(2) 程序界面标题栏图标

(3)程序在任务栏中的图标

2 icon图标文件制作方法

参考:ICO图标在线转换_ICO图标制作工具_PNG在线转ICO - ICO吧

3 QT QIcon图标设置方法

(1)方法一

【1】在.pro中添加:

RC_ICONS = favicon.ico #facicon.ico为图标文件

【2】代码中设置

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ this->setWindowTitle("ICON SET"); QWidget* w=new QWidget; setCentralWidget(w); setWindowIcon(QIcon("favicon.ico"));//或者 setWindowIcon(QIcon(":/favicon.ico"));}

【3】图标文件放置.pro同级目录 ;也可以根据需要放置其它地方,调整调用路径

 【4】显示xxx.exe文件的图标

在项目目录下新建一个txt,并改名为 xxx.rc,我这里是main.rc;

添加如下内容 IDI_ICON1 ICON DISCARDABLE “xxx.ico” 引号里是你的图标名字,我这里是favicon.ico。

IDI_ICON1 ICON DISCARDABLE "favicon.ico"

 

添加刚才创建的main.rc ,或者直接在pro文件里添加:RC_FILE += main.rc

 【5】qmake->构建->运行结果

(2)方法二

【1】通过 Qt Creator 为项目创建一个新的资源文件,如下图所示:

【2】 右键点击创建好的资源文件,选择“Open in Editor”;

 选择添加前缀

添加现有的图标文件 ,如下图标文件名称为“favicon.ico”,如下表明该文件位于.pro项目同级目录下;

【3】 代码中设置

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent){ this->setWindowTitle("ICON SET"); QWidget* w=new QWidget; setCentralWidget(w); setWindowIcon(QIcon(":/favicon.ico"));}

  【4】qmake->构建->运行结果;(显示xxx.exe文件的图标参考方法一)

4 菜单栏图标设置

QMenuBar *menuBar = this->menuBar(); // 创建一个菜单栏 QMenu *fileMenu = menuBar->addMenu("FILE"); // 创建一个菜单 QAction *openAction = fileMenu->addAction("OPEN"); // 创建一个动作 QIcon openIcon(":/open.png"); // 假设你有一个名为"open.png"的图标文件在资源文件中 openAction->setIcon(openIcon); // 设置图标

 

5 工具栏图标设置

QToolButton *button = new QToolButton(); button->setText("LOAD"); QToolBar *toolBar = addToolBar("toolBar"); toolBar->addWidget(button); QPixmap pixmap(":/load.png"); // 假设你有一个名为"load.png"的图标文件在资源文件中 // QIcon barLoad(":/load.png");// 或者使用icon button->setIcon(pixmap); button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); // 设置文字在图标下方,也可旁边 //button->setIconSize(QSize(64, 64));

 5 状态栏图标设置

QIcon iconOpenFile(":/open.png"); // 假设你有一个名为"open.png"的图标文件在资源文件中 QStatusBar *sBar = statusBar(); //状态栏 QLabel *iconLabel = new QLabel(this); iconLabel->setText("Normal text file");// 设置提示文本 iconLabel->setPixmap(iconOpenFile.pixmap(QSize(32, 32)));//设置图标大小 QLabel *textLabel = new QLabel(this); textLabel->setText("Normal text file"); sBar->addWidget(iconLabel); // 添加图标到状态栏 sBar->addWidget(textLabel);//文本添加状态栏 sBar->addWidget(new QLabel("2", this)); //addWidget 从左往右添加 sBar->addPermanentWidget(new QLabel("3", this)); // addPermanentWidget 从右往左添加



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。