PDA

View Full Version : Show different QSystemTrayIcon context menu on different shortcut key combination



Rajesh.Rathod
4th September 2013, 10:36
Hi All,
I want to show two different menu on system tray icon based on "mouse right click" or "mouse right click + ctrl", i am using qt 4.7.3.

First issue was keyboardModifiers function is not working for me and it says ctrl key is not pressed even though it is pressed and I can not use queryKeyboardModifiers as it is not supported in Qt 4.7.3, so I have written one function which uses platform specific API to detect ctrl key press in activate handler for system tray icon and that problem is solved now.

but in System Tray icon activate signal handler, after detecting ctrl key press, when I tries to set new context menu it doesn't show new menu in popup immediately, but in the next mouse click it shows the updated popup menu, so my question is how to immediately update system tray icon context menu from activate signal handler.

I am using windows 7, for this code.

For some hint, i have pasted activate signal handler code here...

//Sample code.
void QMainWindow::iconActivated(QSystemTrayIcon::Activa tionReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Context:
{
// this is utility function which uses platform specific code to identify if ctrl key is pressed or not.
if (IsCtrlKeyPressed())
{
// For ctrl + right click show menu 1.
m_pTrayIcon->setContextMenu(m_pMenu1);
}
else
{
// For only right click show menu 2.
m_pTrayIcon->setContextMenu(m_pMenu2);
}
}
break;

default:
break;
}
}

Note that m_pTrayIcon is instance of QSystemTrayIcon.


Thanks.