简体   繁体   English

如何在纯 C++ 环境中使用 qt 中的图像?

[英]How can I use image in qt with pure c++ environment?

I'm writing a GUI without QT IDE(in clion).I want use image(QICON) in my GUI.I've done some research,QT offical website told me that "The resource system is based on tight cooperation between qmake, rcc (Qt's resource compiler), and QFile.".I don't want to use qmake,.qrc file.我正在编写一个没有QT IDE(在clion中)的GUI。我想在我的GUI中使用图像(QICON)。我做了一些研究,QT官方网站告诉我“资源系统基于qmake之间的紧密合作, rcc(Qt的资源编译器)和QFile。”。我不想使用qmake,.qrc文件。

I can do this with absolute path,or QIcon(QDir::currentPath().But when I change workpath,this method work uncorrectly(image is not shown in GUI).我可以使用绝对路径或 QIcon(QDir::currentPath() 来执行此操作。但是当我更改工作路径时,此方法工作不正确(图像未显示在 GUI 中)。

So,how can I use image file correctly with out qmake,.qrc?那么,如何在没有 qmake、.qrc 的情况下正确使用图像文件?

If you don't want to deal with Qt resource system, you might convert your images to the XPM format , which is a textual representation of an image, and embed it into your source code.如果您不想处理 Qt 资源系统,您可以将图像转换为XPM 格式,这是图像的文本表示,并将其嵌入到源代码中。 There are plenty of online converters you can use.您可以使用很多在线转换器。

For example, you can store the converted image as a static char * array in the source code and use it in the GUI, as follows:例如,您可以在源代码中将转换后的图像存储为static char *数组并在 GUI 中使用,如下所示:

/* An image in XPM */
static char *imageTxt[] = {
    /* columns rows colors chars-per-pixel */
    "300 292 64 1 ",
    "  c black",
    etc.
[..]

QLabel l;
l.setPixmap( QPixmap(imageTxt) );
l.show();

Basically, this is what Qt's resource system does automatically.基本上,这就是 Qt 的资源系统自动执行的操作。

Given that you are not distributing the image(s) in question through the Qt resource system (as in, packaging the image(s) into the application binary), I assume that you are distributing the image(s) alongside the applciation binary.鉴于您没有通过 Qt 资源系统分发有问题的图像(例如,将图像打包到应用程序二进制文件中),我假设您将图像与应用程序二进制文件一起分发。

In this case,QCoreApplication::applicationDirPath() would most likely suit your needs.在这种情况下,QCoreApplication::applicationDirPath()很可能满足您的需求。

There are ways to do it without using the Qt resource system as other answers have said.正如其他答案所说,有一些方法可以在不使用 Qt 资源系统的情况下做到这一点。 However there is also a way to use the Qt resource system with CMake and by extension CLion.然而,还有一种方法可以将 Qt 资源系统与 CMake 以及扩展 CLion 一起使用。

CMake has an option called AUTORCC , which you can toggle on just like AUTOMOC and it will process your.qrc file properly. CMake 有一个名为AUTORCC选项,您可以像AUTOMOC一样打开它,它会正确处理 your.qrc 文件。 This is actually what QtCreator uses if you choose the CMake option.如果您选择 CMake 选项,这实际上是 QtCreator 使用的。

All you have to do is turn on AUTORCC and add your qrc file to your sources, for example:您所要做的就是打开AUTORCC并将您的 qrc 文件添加到您的源中,例如:

set(AUTORCC ON)
...
add_target(my_target my_qrc_file.qrc ...)

You can then access things in the resource system normally (eg path for your image would be :/my_image ).然后,您可以正常访问资源系统中的内容(例如,您的图像路径为:/my_image )。

Use relative path.使用相对路径。 Make sure the image file is in the same dir with your program, or the image file is in a subdir of your program's dir.确保图像文件与您的程序位于同一目录中,或者图像文件位于程序目录的子目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM