Show QWidget or QWindow next to QSystemTrayIcon in QT C ++

I managed to get a QSystemTrayIcon view similar to this:

Assume that the VMWare icon is my QSystemTrayIcon

using the following line of code (with working signal slots):

#include "dialog.h" #include "ui_dialog.h" #include <QMessageBox> #include <form.h> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); QIcon icon("/Users/JohnnyAppleseed/IMAGE.png"); m_ptrTrayIcon = new QSystemTrayIcon(icon ); m_ptrTrayIcon->setToolTip( tr( "Bubble Message" ) ); // m_ptrTrayIcon->setContextMenu(m_trayIconMenu); connect(m_ptrTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason))); } Dialog::~Dialog() { delete ui; } 

However, when I try to implement the code to show a QWidget / QWindow next to the QSystemTrayIcon that I created, it does not appear next to it. It also appears and disappears quickly (even if I don't want it to be next to the QSystemTrayIcon) using this code:

 void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason) { form fr; fr.setWindowFlags(Qt::Popup); fr.show(); } 

For clarity, I would like to show my QWidget / QWindow in the same way as the VMWare Fusion approach (or the clock found in Microsoft Windows Vista or later ... )

Mac OS X / Linux No description

Microsoft Windows enter image description here

Can someone point out what I'm doing wrong? Thanks!

To make things a lot easier, download the project: http://zipshare.net/sv

UPDATE # 1

Regarding the QWidget / QWindow release issue, vahancho recommended I move form fr; from the void Dialog::iconActivated(QSystemTrayIcon::ActivationReason reason) function to the title bar of the working window. And it worked successfully thanks to vahancho . A window will appear, but not yet next to the QSystemTrayIcon: (

+5
source share
1 answer

The problem is that you create the form object on the stack and it is deleted as soon as execution exits your iconActivated () slot. That is why it disappears as soon as you see it. To solve the problem, you need to create a popup on the heap.

UPDATE

To place a dialog box next to the tray icon, you must determine the position of the icon in the tray. You can use the QSystemTrayIcon :: geometry () function for this. You will look like this (adjust the coordinates according to your needs):

 QRect rect = m_ptrTrayIcon->geometry(); fr.move(rect.x(), rect.y()); fr.show(); 
+1
source

All Articles