Qt/C++:程序运行时不显示图标

发布于 2024-10-01 07:32:58 字数 308 浏览 1 评论 0原文

我在 Qt Designer 的主窗口(使用 Qt Creator IDE)中向我的 QToolBar 添加了一个 QAction ,并为该操作提供了一个图标(通过“选择文件”完成)选择与我的项目和源代码位于同一目录中的 .png)。该图标在 Qt Designer 的工具栏中显示正常,但在项目运行时不显示。我在 Windows 上选择标题栏图标时也遇到过类似的麻烦。我以前从未在 Qt 中使用过图形,我需要做一些特别的事情吗?

截图:

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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最初的梦 2024-10-08 07:32:58

我发现自己做了所有正确的事情,添加了一个 qrc 文件并将我的图标放在那里。当我运行程序时没有任何进展。
结果我忘记运行 qmake

  • 右键单击项目名称并选择“运行 qmake”。或者转到“构建”>“运行 qmake”。

每次更改 .pro 文件中的某些内容时,您都需要再次运行 qmake。创建资源文件会隐式地将参数添加到 .pro 文件中,因此您需要这样做。

希望它能帮助其他人。

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.

瑾夏年华 2024-10-08 07:32:58

你制作了QRC文件吗(即Qt相当于资源文件?)
如果没有,那就可以解释您所看到的情况。图标将显示在创建器中,但不会显示在最终编译的可执行文件中。看看这个:

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

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

爱冒险 2024-10-08 07:32:58

另外,在影子构建的情况下,不要忘记在应用程序可执行文件上使用 windeployqt.exe。之后,几个文件夹将添加到您的构建目录中。特别是两个重要的文件夹:iconenginesimageformats,其中包含加载和绘制图标所需的几个 dll。

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.

乞讨 2024-10-08 07:32:58

您是否尝试过打开项目文件?例如,我的项目文件名为 menu.pro,它包含以下内容:

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

最后一行 (RESOURCES = Images.qrc) 是您需要放入项目中的内容文件:当然,使用您用于资源文件的名称,在我的例子中,它是Images.qrc

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.

伊面 2024-10-08 07:32:58

我也面临这样的问题。就我而言,我关闭了 qt Creator 并删除了构建文件夹(类似于 build-projectname.. )。然后重新启动qt Creator。然后神奇地我的图标出现在正在运行的应用程序上。

当 qt 表现不佳时,我经常使用这个技巧。

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.

別甾虛僞 2024-10-08 07:32:58
  1. 添加图标时使用“选择资源”选项而不是“选择
    文件”选项。
  2. 右键单击项目名称,然后单击“运行”
    qmake”。
  1. While adding icons use "Choose Resource" option instead of "Choose
    File" option.
  2. Right click your project's name and click "Run
    qmake".
虐人心 2024-10-08 07:32:58

另请确保不要忘记使用 Q_INIT_RESOURCE(MyIconResource); 在代码中(例如在 main(...) 中)包含 qrc 文件
这也可能导致最终的 exe 中缺少图标。

Also make sure you don't forget to include the qrc file in the code (e.g. in main(...)) with Q_INIT_RESOURCE(MyIconResource);
This can also lead to missing icons in the final exe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文