How to set application icon in a Qt-based project? - c++

How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.

For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc

Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):
1) Add the icon file to your project folder;
2) In the main function call setWindowIcon() method. For example:
QApplication a(argc, argv);
a.setWindowIcon(QIcon("./images/icon.png"));

To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.
macx: ICON = <app_icon>.icns
Note that the ICON qmake variable is only meant to target macOS.
For Windows, use
RC_ICONS = <app_icon>.ico if you're attaching a .ico file
or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)
For further reading, see Setting the Application Icon.

For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:
RC_ICONS = myappico.ico
Offical link: https://doc.qt.io/qt-5/appicon.html

Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc
1) Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:
QIcon icon(":icon/app.icon");
MainWin.setWindowIcon(icon);
2) With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.
The above method can be used in Qt4.7, Qt4.8.x.

Related

How to copy a .ui file in my qwidgets project?

I wish to try this qt example. I created a qwidgets project, copied all headers and sources from the following example to my project.
http://doc.qt.io/archives/qt-5.5/qtmultimedia-audiorecorder-example.html
What is the way to copy the .ui file of that project to my project?
What should be the extention of that file in my project?
The *.ui files are GUI designer files and define the actual GUI.
Copy it as-is. You'll need to invoke the uic (User Interface Compiler) on it to produce the ui_audiorecorder.h header file, which is included in the audiorecorder.cpp file.
Read more on the uic toolchain in the docs: Using a Designer UI File in Your Application.
You copy the .ui file into your project's folder using your favorite file manager or terminal or what have you. Suppose that the file is named foo.ui.
Then you add the file to the project by either:
Adding the following line to the .pro file:
FORMS += foo.ui
Right-clicking the root project node, selecting "Add Existing Files...", and navigating to the foo.ui file in the file dialog.
The two procedures are equivalent, the result is the same: a new FORMS item in the project file.
That's all. As soon as you save the .pro file, Qt Creator will parse it and show the foo.ui under a Forms project tree node.

How to include an icon in the title bar of window in Qt?

I want to be able to replace the existing standard icon with my own but I am unsure how to do so. I only started Qt one week ago so I don't know much about the application. In addition to this I looked at the Qt website but their method for adding an icon did not work on my computer.
Besides using a RC file (Windows) you may specify the icon directly using qmake.
win32: RC_ICONS = icon.ico
Make sure it detects the path correctly. In the provided case the icon resides in the project root directory right beside the project file.
For window icons you typically, if application wide applicable, use some method of QApplication.
...
QApplication app(argc, argv);
...
app.setWindowIcon(QIcon(":/icon");
...
which is consuming the resource file using the alias icon. Here is an example (res.qrc):
<RCC>
<qresource prefix="/">
<file alias="icon">icon.png</file>
</qresource>
</RCC>
More information on resources are here.
Perhaps try the function setWindowIcon function on the QWidget or QApplication.

QT Resource File - Images Not Showing When Running

I just upgraded to QT Creator 5.3 and created a brand new QT Widgets Application project and am using the Microsoft VC++ compiler. All I have is a resource file with "logo.png" added (which opens in QT if I double click it), and a label that I am trying to set the background image for. The problem is that no matter what I do I cannot get the image to show up when the program is running. The only way I can get it to show the image is by not using the resource file and instead map directly to the file (ex. "c:/blah/blah/logo.png")
Also, if I set the background image in the UI designer, the background shows up in the IDE but NOT when I run the program. I have tried probably 20+ variations of the code, including resource file aliases as well as adding the files to the project directly, and nothing seems to work.
I'm not sure if there is a step I am missing, or if perhaps there is something that I have to do to get the project to compile the image.
Resources.qrc
<RCC>
<qresource prefix="/">
<file>logo.png</file>
</qresource>
</RCC>
loginform.cpp
QPixmap pixmap = QPixmap (":/logo.png");
ui->label->setPixmap( QPixmap(pixmap));
Project.pro
OTHER_FILES += \
logo.png
RESOURCES += \
Resources.qrc
After countless hours of trial, error, and mostly frustration, I have been able to resolve this issue by simply running qmake.
To do this I just right clicked on the project and clicked "Run qmake" and suddenly the image errors (as well as a few others) were resolved!

Qt qrc resource file - can't load icon

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:
RESOURCES = resource.qrc
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
The problem is: the icon won't show up!
The following works:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)
Update:
I'm posting here my qrc file:
<RCC>
<qresource prefix="/">
<file>my_image.png</file>
</qresource>
</RCC>
it's pretty simple and I don't understand why at runtime icons are not showing up
I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.
Qt Creator is awesome.
Hope this helps!
#Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
In your code, you have:
QPixmap pixmap = QPixmap ("://my_image.png");
but, according to the documentation, it should be
QPixmap pixmap = QPixmap (":/my_image.png");
or you can give aliases to your resources, and use those instead.
Issue is solved is use rcc.exe
C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp
During compilation you should have images in the path.
Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.

qtcreator tray icon under window does not show up when deployed

i'am trying to deploy a QT application made in QT Creator which goes back to system tray when closed.
I have made a svg tray icon, when i run it from QT Creator, either in debug and in release mode under Window 7, the tray icon shows up, but when i copy everything to another directory to make a distributable archive from it, the tray icon does not show up anymore.
Of course i search already for solutions, but everything i have found yet, i have made.
So what i have:
trayicon.svg file in project root
qrc file created, adding trayicon.svg into the resource files root
in project .pro file: RESOURCES += resources.qrc
copied binary + necessary dll's to target directory
copied QT plugins imageformats/* to target dir imageformats
added QApplication a(argc, argv); a.addLibraryPath(a.applicationDirPath()); to main.cpp
that is everything i found so far, but still the system tray icon is not showing up
What am i missing?
(current qt 4.8 + current qtcreator by the way)
#netrom
code in MainWindow : QMainWindow constructor:
trayIcon = new QSystemTrayIcon(this);
showAction = new QAction(tr("&Show"), this);
connect(showAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Quit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(showAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon->setContextMenu(trayIconMenu);
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon->setIcon(QIcon(":trayicon.svg"));
trayIcon->show();
Create iconegines directory in your app directory (for example: c:\MyApp\iconengines).
Copy qsvgicon.dll to this new directory (for example: c:\MyApp\iconengines\qsvgicon.dll) from qt plugins directory (in my case it's c:\qt\5.4\mingw491_32\plugins\iconengines\qsvgicon.dll).
Copy QtSvg.dll to your app directory (for example: c:\MyApp\Qt5Svg.dll) from Qt bin directory (in my case it's c:\qt\5.4\mingw491_32\bin\Qt5Svg.dll).
P.S. I know I'm late, this answer is for those who will google same problem.
Again, way later for DuckDuckGo-ers:
If your icon is not in .svg you might need another solution. For my .ico system tray icon I had to copy <path-to-qt>\plugins\imageformats into the application folder. Actually only qico.dll was needed in that folder in my case, but you get my drift.