Qt/C++: Icons not showing up when program is run

2020-06-02 05:06发布

I've added a QAction to my QToolBar in my MainWindow in Qt Designer (using the Qt Creator IDE) and given that Action an icon (done by "Choose File" and selecting my .png located in the same directory as my project and source code). The icon shows up fine in the toolbar in Qt Designer, but does not show when the project is running. I've had similar trouble when choosing the title bar icon on windows. I've never used graphics in Qt before, is there something special I need to do?

Screenshot:

标签: qt icons toolbar
6条回答
Bombasti
2楼-- · 2020-06-02 05:41

Also in case of shadow build don't forget to use windeployqt.exe on your application executable file. After that several folders will be added to your build directory. Particulary two important folders: iconengines and imageformats which contain several dlls needed to load and draw the icon.

查看更多
Rolldiameter
3楼-- · 2020-06-02 05:51

Have you tried opening up the Project file. For example, my project file is named menu.pro and it contains the following:

TEMPLATE = app
QT = gui core
CONFIG += qt debug warn_on console
DESTDIR = bin
OBJECTS_DIR = build
MOC_DIR = build
UI_DIR = build
FORMS = ui/mainwindow.ui
HEADERS = src/mainwindowimpl.h
SOURCES = src/mainwindowimpl.cpp src/main.cpp
RESOURCES = Images.qrc

The last line (RESOURCES = Images.qrc) is what you need to put in your project file: of course, use the name that you used for your resources file, in my case, it is Images.qrc.

查看更多
神经病院院长
4楼-- · 2020-06-02 05:52

I found myself doing all the right stuff, adding a qrc file and placing my icons there. When I run the program no deal. Turn out I was forgetting to run qmake:

  • Right click your project's name and select "Run qmake". Or go to Build>Run qmake.

Everytime you change something in the .pro file you need to run qmake again. Creating a resource file implicitly adds argunments to the .pro file, so you need to do it.

Hope it helps other people out there.

查看更多
▲ chillily
5楼-- · 2020-06-02 05:54

Did you make a QRC file (that is, Qt's equivalent of a resource file?) If not, that would explain what you're seeing. The icons will show up in the creator, but not in the final compiled executable. Have a look at this:

http://doc.qt.io/qt-5/resources.html

查看更多
ゆ 、 Hurt°
6楼-- · 2020-06-02 06:01

I also faced such a problem. In my case, I closed qt creator and deleted the build folder (something like build-projectname.. ). Then restarted qt creator. Then magically my icon showed up on running application.

I often use this trick when qt doesn't behave well.

查看更多
放我归山
7楼-- · 2020-06-02 06:07
  1. While adding icons use "Choose Resource" option instead of "Choose File" option.
  2. Right click your project's name and click "Run qmake".
查看更多
登录 后发表回答