Qt学习笔记(资源文件)
2019-08-17 20:46
891 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_42754132/article/details/99697687
资源文件
菜单项的子选项在生成时不让出现中文,可以先设置成英文,然后去下边对应的text中去改成对应的中文。
对于状态栏添加不了信息,那么就用代码实现
在代码中加图标
以后再找控件通过ui
qrc文件上右键Open in Edit
成千上百中文件可能要加一个前缀,由程序员自己分类,下一步
添加文件,,选中图片
添加之前最好先编译一下。
添加前缀之后添加文件,把所有图片都加进来。
添加资源文件
首先将资源导入到项目中
右键项目 - 添加新文件 – Qt - Qt Resource File
给资源起名称 res
Qt会生成res.qrc 文件
右键res.qrc open in Editor
添加前缀
添加文件
使用 “ : + 前缀名 + 文件名 ”
可以设置别名,但是不建议,因为设置别名,原来的方式就不可用了
[code]#------------------------------------------------- # # Project created by QtCreator 2019-08-16T15:42:54 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = 02_Source TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 CONFIG += c++11 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target RESOURCES += \ res.qrc
[code]#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
[code]#include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
[code]#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //通过ui寻找控件 //设置工具项图标 //ui->actionnew->setIcon(//路径为 /分隔或 \\分隔 //QIcon("C:/users/dawnl/Desktop/Qt/06 QT/04-Day15_C++_QT/04-Day15_C++_QT/Doc/Image/Luffy.png")); //本地的文件,脱离本地就不见了,所以要添加资源文件到项目中 //右键 addnew ----> Qt ---> Qt Resource File ---> //路径不用改,起名称就行 //使用资源文件": + 前缀名 + 文件名" ui->actionnew->setIcon(QIcon(":/Image/Luffy.png")); ui->actionopen->setIcon(QIcon(":/Image/mario.gif")); } MainWindow::~MainWindow() { delete ui; }
[code]<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"/> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>23</height> </rect> </property> <widget class="QMenu" name="menu"> <property name="title"> <string>文件</string> </property> <addaction name="actionnew"/> <addaction name="separator"/> <addaction name="actionopen"/> </widget> <widget class="QMenu" name="menu_2"> <property name="title"> <string>编辑</string> </property> </widget> <addaction name="menu"/> <addaction name="menu_2"/> </widget> <widget class="QToolBar" name="mainToolBar"> <attribute name="toolBarArea"> <enum>LeftToolBarArea</enum> </attribute> <attribute name="toolBarBreak"> <bool>false</bool> </attribute> </widget> <widget class="QStatusBar" name="statusBar"/> <action name="actionnew"> <property name="text"> <string>new</string> </property> </action> <action name="actionopen"> <property name="text"> <string>open</string> </property> </action> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
相关文章推荐
- 上传速度 、已用时间、预计剩余时间 上传资源时 上传文件大小,已上传大小 进度%
- java项目下资源文件路径的获取
- Android中String资源文件的String.format方法(java)
- 小瓜牛漫谈 — 获取资源文件
- Android APK反编译得到Java源代码和资源文件
- 如何将MFC资源文件打包成dll
- duilib将资源文件压缩成zip并打包到exe的资源中
- yepnope.js 异步加载资源文件
- ajax访问不到服务端静态资源文件的处理方法
- 结合MS AJAX将资源文件编译到动态链接库
- Android中打包含有Activity以及资源文件的jar包在工程中调用
- Android开发知识:字符串资源文件format方法介绍
- SharePoint 2010:部署.resx(资源)文件到App_GlobalResources的简单方法
- 在VB项目中使用资源文件
- ASP.NET提供文件下载函数(支持大文件、续传、速度限制、资源占用小)
- 打第三方jar包,找不到资源文件问题解决方案
- app增加国际化的资源文件支持
- Maven 单测使用资源文件(词表、配置等)及资源文件损坏
- 资源地址;浏览和打开文件;GetOpenFileName;GetSaveFileName;OPENFILENAME;PtrToStringChars;.NET类库的打开文件夹操作
- eclipse 项目中搜索资源(类方法,文件名,文件中的字符串)(图解)