qtcreator tray icon under window does not show up when deployed - c++

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.

Related

QPushButton setIcon not working

I have just started using Qt and I've been trying to get a single button to display an icon but for some reason I can't get it to work. This is my code
QPushButton* button = new QPushButton();
button->setIcon(QIcon("icon.png"));
button->show();
I don't know what I'm missing here, the icon.png and the .cpp file are both in the same folder.. What am I doing wrong?
Add icon folder to your resources.qrc and try this one:
setIcon(QIcon(QPixmap(":/icons/yourIcon")));
It works in my project

How can I stop a QApplication from showing up in the dock?

I'm making a console application on OS X that interacts with specific parts of the Desktop Environment (mainly the mouse using QCursor), so I cannot use a QCoreApplication (despite how much I want to).
The application works fine, it is just that it shows up in the dock whenever I run it from the command line. I looked at several other questions online, but none fixed the problem I'm having.
I looked into QSystemTrayIcon, and I would be fine with using it IF it would get rid of the pesky window that pops up. Here is my code narrowed down to a minimum that still has the problem I addressed above.
The .pro:
TARGET = project
QT += core
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QT -= gui
CONFIG += c++11
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
main.cpp:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCursor cur;
cur.setPos(0,0);
return a.exec();
}
A workaround would be doing it manually as describe here.
Luckily, if it is a cocoa application, you can hide the Dock icon yourself. To see if it is possible, right-click (Control-click) on the application icon. If "Show Package Contents" is in the menu that appears, you can hide the icon in the Dock.
If this is the case, select "Show Package Contents" and look for the "Info.plist" file inside the Contents folder. Open this file using TextEdit by right-clicking on it and choosing "Open With - Other" from the menu.
In the file, paste the following two lines just after on the 6th line:
<key>LSUIElement</key>
<string>1</string>
Save the file and close it. For the changes to take effect, you need to move the application to the desktop and them back to its original location (OS X keeps a cache of the file, so you need to trick it into checking it again).
Now when you open the application, no icon will appear in the Dock.
Source: http://www.macosxtips.co.uk/index_files/disable-the-dock-icon-for-any-application.php

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.

Confusion QLabel and QPixmap

I want to display a Pixmap into a Label on Qt.
Here is the problem, the above code doesn't display the image that I want.
Please correct me if something is wrong. I have checked again and again (even it's few lines) but it didn't lead to results!!!
QLabel *label;
label= new QLabel(this);
QPixmap jet("C:/images/image.jpg");
label->setPixmap(jet);
One other problem, it seems that it works in another machine!
By default, qt builds with “-system-libjpeg”, so sudo apt-get install libjpeg if you are on linux, and to check supporting image formats, add
qDebug() << QImageReader::supportedImageFormats ();
to add plugins to your project, go to QtSDK/Desktop/Qt/4.8.1/gcc/plugins or something like this, chose the version you use, and copy "plugins" folder to root of your project(more precisely - you need only "imageformats" folder, that lays within plugins), and in your main.cpp file add line addLibraryPath (by the way, plugin's folder contain not only dlls for release, but for debugging also, they are a bit heavy, so i would remove dlls with *d.dll, for example QtCore.dll - for release, QtCored.dll - for debugging)
QApplication a(argc, argv);
MainWindow w;
a.addLibraryPath("plugins");
w.show();

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

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.