I am using QT.QSystemTrayIcon
to create a tray icon.
When clicking on a tray icon, I need a window to opens right above the icon (in the bottom right corner). How can I do thi开发者_Python百科s?
If you want to open the window aligned with the icon (as the built-in volume control does), take a look at the manual for this:
QRect QSystemTrayIcon::geometry()
Ok, I'm answering 4 years later, because I found this question today, trying to solve the same problem.
Note: This is working well on Win, tested on XP, 7 and 8 but, as pointed by @MichaelScheper, this isn't working on Mint/GNOME. Feedback on other platforms is welcome.
void main_window::create_tray_icon()
{
m_tray_icon = new QSystemTrayIcon(QIcon(":/icon.png"), this);
QAction *quit_action = new QAction( "Exit", m_tray_icon );
connect( quit_action, SIGNAL(triggered()), this, SLOT(on_exit()) );
QAction *another_action = new QAction( "Do something", m_tray_icon );
connect( another_action, SIGNAL(triggered()), this, SLOT(on_do_something()) );
QMenu *tray_icon_menu = new QMenu;
tray_icon_menu->addAction( another_action );
tray_icon_menu->addAction( quit_action );
m_tray_icon->setContextMenu( tray_icon_menu );
m_tray_icon->show();
}
I'm not sure what exactly you mean by a window. My first answer refers to a context menu that popups where you can add several actions.
If you want to have a small information window with a message in it like in Windows, you should use QSystemTrayIcon::showMessage. There you can popup a message, set the duration of this message and set an icon for it.
Do you mean open a window such as QMainWindow or QWidget above the tray icon?
You need to get a QDesktopWidget
using QApplication::desktop()
, then query that QDesktopWidget's screenGeometry()
function to determine the screen dimension, then create a window and position it appropriately in the right bottom corner based on the coordinates obtained from screenGeometry()
.
精彩评论