QSystemtrayicon: no image on Mac - c++

I've got a problem with the QSystemTrayIcon class on Mac and Linux.
I made a program creating a System tray icon, and I have no problem on Windows, but under Linux (ubuntu 12) and Mac (OSX 10.8), the tray icon is here, but the image on it doesn't show up.
This is what I'm doing in the ctor of my class:
icon = new QIcon("trayIcon.png");
m_pTrayIcon = new QSystemTrayIcon();
m_pTrayIcon->setIcon(*icon);
m_pTrayContextMenu = new QMenu();
m_pTrayContextMenu->addAction(openSettings);
m_pTrayContextMenu->addAction(switchSyncMode);
m_pTrayContextMenu->addAction(openFolder);
m_pTrayContextMenu->addSeparator();
m_pTrayContextMenu->addAction(quit);
m_pTrayIcon->setContextMenu(m_pTrayContextMenu);
m_pTrayIcon->show();
I'm using the Qt 4.8 library.
Anyone have an idea?

Two possibilities
It cannot find the file. Try what happens if you give the global path.
It cannot load the png; see if loading QT module Multimedia helps.

Related

Why Windows font disappered after running my Qt application?

Background
I write an uninstaller on Windows platform using Qt and C++. When running, It copy itself to windows temp folder and rerun the exe in that folder. The new process of uninstaller will stop and uninstall some service & driver I installed, then remove some files on the disk.
Problem
After the uninstallation is complete, the font of the Windows system will disappeared partly, as following picture shows:
As you can see, the oringe one is my uninstaller, and both the font of folder name in windows File explorer and the font in my blue button is disappeared, but some other font is still visible like the left side nav bar.
And most important, it just happend by accident(But it really happened many times on some machines). It cannot reappear stably.
I can recover it by restart the File explorer.exe or reboot the Windows system.
Environment
I used a static lib of Qt 5.15.2 which I build from source.
C++ 17
All the executable is compiled and linked by microsoft visual studio(2019) c++ kits
All the application is linked statically with MT option.
I tested the uninstaller on Win7 & Win10 (The problem had appeared on both platforms).
The font which is used in my uninstaller is 'Alibaba PuHui' and 'Roboto'.
Qt font usage
I think the most possible reason is the wrong usage of the Qt font loading in my uninstaller, so I put the method I used here.
Firstly, I embed the font in my application through .qrc file
<RCC>
<qresource prefix="/">
<file>res/font/Alibaba-PuHuiTi-Medium.ttf</file>
<file>res/font/Alibaba-PuHuiTi-Regular.ttf</file>
<file>res/font/Roboto-Regular.ttf</file>
<file>res/font/Roboto-Medium.ttf</file>
</qresource>
</RCC>
Then I init the font by the initFont() function
#include <QString>
#include <QFontDatabase>
inline QString ali_font_regular = "";
inline QString ali_font_medium = "";
inline QString roboto_font_regular = "";
inline QString roboto_font_medium = "";
inline void initFont() {
int med_font_id = QFontDatabase::addApplicationFont(":/res/font/Alibaba-PuHuiTi-Medium.ttf");
ali_font_medium = QFontDatabase::applicationFontFamilies(med_font_id).at(0);
int reg_font_id = QFontDatabase::addApplicationFont(":/res/font/Alibaba-PuHuiTi-Regular.ttf");
ali_font_regular = QFontDatabase::applicationFontFamilies(reg_font_id).at(0);
int roboto_reg_font_id = QFontDatabase::addApplicationFont(":/res/font/Roboto-Regular.ttf");
roboto_font_regular = QFontDatabase::applicationFontFamilies(roboto_reg_font_id).at(0);
int roboto_medium_font_id = QFontDatabase::addApplicationFont(":/res/font/Roboto-Medium.ttf");
roboto_font_medium = QFontDatabase::applicationFontFamilies(roboto_medium_font_id).at(0);
}
And Finally, the font setting of my Qt widget is like this:
logo_text_b = new QLabel(this);
logo_text_b->setMargin(0);
logo_text_b->setText("test");
QFont f_b(ali_font_medium);
f_b.setPixelSize(12);
logo_text_b->setFont(f_b);
logo_text_b->setStyleSheet("QLabel { color : white; }");
I have no thoughts what causes it happend, And I can't find a way to debug because it happend by accident. I hope someone can give me some direction. THANKS!

How do I set my application version for Windows in Qt?

When my application crashes, the Windows Event Viewer always reports my application version as "0.0.0.0".
I can't figure how to set the application version in a way that the Windows Event Viewer recognizes. Changing it with QApplication::setApplicationVersion() doesn't seem to do it.
Obviously there are better ways to debug a program than the Windows Crash Log, but in lieu of all of that, how would I go about setting this value so that Windows recognizes it? My IDE is Qt Creator.
You can set the VERSION qmake variable in your pro file:
VERSION = 1.0.0.0
On Windows, triggers auto-generation of an .rc file if the RC_FILE and
RES_FILE variables are not set. The generated .rc file will have the
FILEVERSION and PRODUCTVERSION entries filled with major, minor, patch
level, and build number.
Use the QCoreApplication class.
QCoreApplication::setApplicationVersion("1.0");
If you develop a widget app or qml, maybe you want to show your version in WindowTitle full solution would be:
In .pro file add:
VERSION = 1.2.3
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
and in QWdiget or QMainWindow
setWindowTitle("Qt " +qtVersion + " Version"+ APP_VERSION);
or in QML
ApplicationWindow {
title: "Qt " +qtVersion + " Version"+ APP_VERSION
...

Error on QT5.5 camera preview

I'm trying to use a camera in my QT5.5 application using the QCamera type of QT. A simple example of what I'm doing is
QList<QCameraInfo> cameras = QCameraInfo::avalilableCameras();
qdebug() << cameras.length();
const QCameraInfo &cameraInfo = cameras.first();
QCamera *camera = new QCamera( cameraInfo );
When I run this example, I get "No m_videoSink available!" on the command line. QT is build on Ubuntu 12.04 with libgstreamer0.10-0, libgstreamer0.10-dev and libgstreamer-plugins-base0.10-0 installed. QT is built with -qt-xcb. I have no idea what is wrong here. Does anyone have an idea?
PS: Of course I make sure that line 2 prints >= 1 ;).
Ok, finally I found the basic problem: Unfortunately Qt dropped Ubuntu 12.04 LTS support with version 5.5. With version 5.4 everything works as expected.

Irrlicht Mac OS X crash

I'm trying to get Irrlicht running on Mac OS X, but when I try to run the "Demo" project, I see a screen with some options, but as soon as I click something, I get a lldb error for the following line of code:
[Window setIsVisible:FALSE];
It's line 554 of file CllrDeviceMacOSX.mm, and it gives this error in about every example I try to run
My system:
MacBook Pro Retina late 2012
Mac OS X 10.9 Mavericks
It appears that the NSWindow object Window is being released before the call to [Window setIsVisible:FALSE];.
Looking at Apple's documentation (NSWindow isReleasedWhenClosed) this is the expected behaviour as by default NSWindow objects are automatically released when closed.
As a workaround add:
[Window setReleasedWhenClosed:FALSE];
after the Window = [[NSWindow alloc]..... calls in CllrDeviceMacOSX.mm (there are two of them). I do not know enough about Irrlicht to know if this is a valid fix.

System tray application Linux Qt/C++

I'm writing an application in C++ with Qt that utilizes the system tray. I have implemented the system tray using a QSystemTrayIcon class as shown in the examples, but it doesn't have the same behavior as other system tray icons that are present on my computer. For instance, I have Spotify installed on Ubuntu 12.04 and it shows a system tray icon with a drop down menu. With my application, it shows a system tray icon with a context menu, meaning you have to right click it to make the menu active. With Spotify, all that needs to be done is to click on the icon and the menu will show. What can I do to get native system tray icons in Ubuntu? I'm fine with using specific code for X11/Linux and not the built-in Qt functions. Thanks much.
Here's my code:
void MainWindow::closeEvent(QCloseEvent *event)
{
if (trayIcon->isVisible()) {
hide();
event->ignore();
}
}
void MainWindow::createActions()
{
restoreAction = new QAction(tr("&Show"), this);
connect(restoreAction, SIGNAL(triggered()), this, SLOT(show()));
quitAction = new QAction(tr("&Exit"), this);
connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
}
void MainWindow::createTrayIcon()
{
trayIconMenu = new QMenu(this);
accountsMenu = trayIconMenu->addMenu(tr("Accounts"));
trayIconMenu->addSeparator();
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
Try to drop down menu from activated signal of QSystemTrayIcon.
void Window::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Trigger:
// show your menu here
}
}
I am commenting for the benefit of others here...
I had the same issue when we deployed our product (built using Qt)on Ubuntu 12.04 LTS.
We use the qt.conf way of deployment.
After a lot of hunting and going through the source on sni-qt I found that plugins need to be properly found out. So I created and copied the plugins from our build environment to the plugins directory relative to my application path mentioned in qt.conf against the 'Plugins = ' entry. Also made sure that sni-qt is update and installed on the deployed Ubuntu 12.04 machine. The menus appeared as they appear for other tray applications.
You can copy plugins from /usr/lib/i386-linux-gnu/qt4/plugins/ on a 32 bit machine or its equivalent path on 64 bit machine. For this problem plugin under systemtrayicon is the required one.
HTH.