Qt QSystemTrayIcon::setIcon or QIcon() not working - c++

using qt 5.5 on linux (ubuntu), I have very simple code (in main.cpp):
//QCoreApplication a(argc, argv);
QApplication a(argc, argv);
// Test if QLabel can show the icon
QLabel *label = new QLabel();
label->setPixmap(QPixmap("icon16.png"));
label->show();
// Do the same for QSystemTray
QSystemTrayIcon i;
QIcon icon(QPixmap("icon16.png"));
i.setIcon(icon);
i.show();
// i.showMessage("hey dude!", "this is my message");
qDebug("done\n\n");
So here I am trying to dislpay a system tray icon... which works, but the icon is a red circle with a cross through it (showing that no icon is available). The commented out "showMessage()" function also works fine.
I know that the icon itself can be loaded and displayed by qt because it works for the QLabel.
The problem is that the QSystemTrayIcon is not displayed.
I have been through this post Another Persons Issue With QSystemTrayIcon, but he has a different issue to me in the end.
I can't figure out what the problem is here :(
edit
There are these links that suggest this is broke:
here
and here

Related

How to play a sound before splash in QT?

I want to play a sound within the splash but with the following code I get the splash to show up and then, only when the splash is gone, the sound to play. Can someone help me out? Thank you very much.
QApplication a(argc, argv);
QMediaPlayer * splashSound = new QMediaPlayer;
splashSound->setMedia(QUrl("qrc:/sfx/splash_sound.wav"));
splashSound->play();
QSplashScreen * mainSplash = new QSplashScreen;
mainSplash->setPixmap(QPixmap(":/img/splash.png"));
mainSplash->show();
MainWindow w;
QTimer::singleShot(2500, mainSplash, SLOT(close()));
QThread::msleep(2500);
w.show();
return a.exec();
The problem is your call to QThread::msleep(2500); it prevents the Qt event loop from executing (because a.exec() can't run until after it returns), and that in turn prevents the audio from playing.
The easy fix is to remove that line and the call to w.show(), and replace them with something like this:
QTimer::singleShot(2500, &w, SLOT(show()));
... That will cause your MainWindow widget to appear at the same time the splash-image disappears.

Attempting to display multiple png files in one Qt Window using CPP

I am using Qt5 in cpp, and I am attempting to display multiple png files in one window. All my attempts to date, will place one png image on top of the other
The png file names are passed to the program as arguments
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item1(QPixmap((char*) argv[1]));
scene.addItem(&item1);
QGraphicsPixmapItem item2(QPixmap((char*) argv[2]));
scene.addItem(&item2);
view.show();
a.exec();
Reading through the Qt documentation, I thought QGraphicsItemGroup might hand this for me. It did not make a difference.
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item2(QPixmap((char*) argv[1]));
QGraphicsPixmapItem item3(QPixmap((char*) argv[2]));
QGraphicsItemGroup grp;
grp.addToGroup(&item2);
grp.addToGroup(&item3);
scene.addItem(&grp);
view.show();
a.exec();
These are both examples both build, but both have the same error, one png is on top of the other. I am trying to get both png files to show in the same window.
Any suggestions would be appreciated. Thanks.
If you want the 2 images next to one another, you need to control their position yourself.
Have a look at QGraphicsAnchorLayout in
Simple Anchor Layout Example
and QGraphicsLayout in
Graphics View Flow Layout Example

Qt set common background for all dialog boxes

I am working on developing a Qt 5 widgets desktop application where I want to give a common background for all windows and dialog boxes that pop up. The problem is that for each window I have to specify the same piece of code over and over again to load the same background. I am also using paint function override so as not to distort the background when window is resized. Here's my code:
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
pixmapBg.load(":/images/google-material-design-wallpaper-10.jpg");
}
void SettingsDialog::paintEvent(QPaintEvent *pe)
{
QPixmap pixmapBgL = pixmapBg.scaled(this->size());
QPalette palette;
palette.setBrush(QPalette::Background, pixmapBgL);
this->setPalette(palette);
}
Is there a way to accommodate this in Qt using a single file rather than mentioning it for each window?
Yes, you can! You will have to provide your own stylesheet, or initialize your application by calling QApplication::setStyleSheet(styleName).
Follow up from comment: setStyleSheet is the quickest approach, i.e.
qApp->setStyleSheet("QDialog, QMessageBox {background-image: url(:/images/google-material-design-wallpaper-10.jpg);}");
assuming you have a valid QApplication reference qApp. Note that you can also refer to your custom subclasses as well, if you want to refine the scope of the stylesheet.
Here is some code using QApplication::setStyleSheet:
QString styleSheet = "QWidget{\
background-color: yellow\
}"//style sheet in CSS style
int main(int argc, char** argv){
QApplication app(argc, argv);
app.setStyleSheet(styleSheet);//will set all the QWidgets' background color to yellow
return app.exec();
}
There is actually a background-image property but I'm not sure about which widgets are supporting it so you can check right there.

How to specify title icon and dock icon separately in Qt

I'm using the code below to specify the app icon. But that would change both the title and dock icons.
QApplication app(argc, argv);
QIcon icon(":res/image/my-icon.png");
app.setWindowIcon(icon);
Is there any way to specify them separately?

Is it possible to remove borders a single QWidget around a QMainWindow?

This is a very simple problem to reproduce. Create a new project with Qt Creator with a QMainWindow. Using designer add a single widget (it doesn't matter which one) and then use the right button to set a layout (any layout as with a single widget the results are always the same).
There will be a gray border around the widget. I would like to remove this border so that the widget occupies the entirety of the main window area (which Qt always names centralWidget). Basically the single widget is all I want to see.
Is this possible? If so, how?
Yes, it's possible. All you need is setting margins of that centralWidget to 0. (note that you should first set up a layout)
In properties panel set the following properties to 0.
layoutLeftMargin
layoutTopMargin
layoutRightMargin
layoutBottomMargin
Also note that using a QWidget (not QMainWindow) as your main app window is perfectly valid, so if your app has only one widget, you won't need a QMainWindow at all. It's enough to show that widget.
The answer above is the right answer but I also wanted to share what I've found out. I basically wanted to create a QGraphcisView that occupies the entire screen. and I thought of the above method, which Hi I'm Frogatto answered. Another way is to create a simple app with this code:
#include <QCoreApplication>
#include <QApplication>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);
QApplication app(argc, argv);
QGraphicsView *view = new QGraphicsView();
view->showFullScreen();
return app.exec();
}
And this .pro file:
QT += core gui widgets
CONFIG += c++11
TARGET = Test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
This also does what I wanted!. Maybe it can help someone.