1

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.

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).

So,how can I use image file correctly with out qmake,.qrc?

Daniel Lee
  • 65
  • 1
  • 6

4 Answers4

2

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. 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:

/* 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.

vahancho
  • 20,808
  • 3
  • 47
  • 55
  • XPM is one option but (for larger images) more compact formats can be used as well. I once used [GIMP](http://www.gimp.org)s option for RGB C source image dump in my answer to [SO: Qt C++ Displaying images outside the GUI thread (Boost thread)](https://stackoverflow.com/a/47470395/7478597). With [QImage::loadFromData()](https://doc.qt.io/qt-5/qimage.html#loadFromData), a dump of any supported format may be used as well. – Scheff's Cat May 14 '20 at 10:00
  • To produce a source code out of a binary file, a simple (self-written) tool can help: [SO: How to include data object files (images, etc.) in program and access the symbols?](https://stackoverflow.com/a/47415163/7478597) – Scheff's Cat May 14 '20 at 10:06
0

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.

In this case, QCoreApplication::applicationDirPath() would most likely suit your needs.

Morten B.
  • 93
  • 8
0

There are ways to do it without using the Qt resource system as other answers have said. However there is also a way to use the Qt resource system with CMake and by extension CLion.

CMake has an option called AUTORCC, which you can toggle on just like AUTOMOC and it will process your .qrc file properly. This is actually what QtCreator uses if you choose the CMake option.

All you have to do is turn on AUTORCC and add your qrc file to your sources, for example:

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

You can then access things in the resource system normally (e.g path for your image would be :/my_image).

Object object
  • 1,939
  • 10
  • 19
0

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.