您的位置:首页 > 编程语言 > Qt开发

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>

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: