PDA

View Full Version : QSystemTrayIcon thread issue



TheGrimace
25th September 2007, 19:56
I am running into a situation where I create a QSystemTrayIcon in the constructor of the main widget. But when I try to call show on it, it outputs about a dozen "Cannot move to target thread" messages. It still displays and is usable, but it is disconcerting to get any error messages. Here is the code:
IngestorGUI::IngestorGUI()
{
if(QSystemTrayIcon::isSystemTrayAvailable())
{
m_ptr_SystemTrayIcon = new QSystemTrayIcon();
m_TrayIcon = QIcon("dmax_icon.png");
m_ptr_SystemTrayIconContextMenu = new QMenu(this);
m_ptr_actShowGui = new QAction(this);
m_ptr_actShowGui->setText("Show Interface");
m_ptr_actShowGui->setCheckable(true);
connect(m_ptr_actShowGui,SIGNAL(changed()),this,SL OT(slotShowInterface()));
m_ptr_SystemTrayIconContextMenu->addAction(m_ptr_actShowGui);
m_ptr_SystemTrayIcon->setIcon(m_TrayIcon);
m_ptr_SystemTrayIcon->setContextMenu(m_ptr_SystemTrayIconContextMenu);
m_ptr_SystemTrayIcon->show();
}
}

On a side note; Is there any way to force explorer to update the system tray when I shut down? It never seems to update until I mouse over it. I tried to hide the icon before deleting, but it still didn't update. here is my destructor code:
IngestorGUI::~IngestorGUI()
{
if(m_ptr_actShowGui != (QAction*) NULL)
{
delete m_ptr_actShowGui;
}
if(m_ptr_SystemTrayIconContextMenu != (QMenu*) NULL)
{
delete m_ptr_SystemTrayIconContextMenu;
}
if(m_ptr_SystemTrayIcon != (QSystemTrayIcon*)NULL)
{
m_ptr_SystemTrayIcon->hide();
delete m_ptr_SystemTrayIcon;
}
}

wysota
25th September 2007, 20:44
Are you using threads in your application?

TheGrimace
25th September 2007, 20:50
I will be, but not yet. Right now this IngestorGUI object is created in main along with a QApplication. The QApplication is then executed. Here is the main code:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

IngestorGUI window;

return app.exec();
}

wysota
25th September 2007, 21:12
Can you create a minimal example that only creates the tray icon and calls exec() on the application object and see if the problem persists? BTW. Which version of Qt are you using?

TheGrimace
25th September 2007, 21:15
4.3.1

I will try that and post back.

EDIT: The issue persists on the minimal example.

wysota
25th September 2007, 21:22
This might be a bug in Qt. You might check if it was reported in the task-tracker.

TheGrimace
25th September 2007, 21:27
Doesn't seem to be. Can anyone corroborate this issue on their system?

jpn
25th September 2007, 21:43
Could you post the minimal example? System Tray Icon Example (http://doc.trolltech.com/4.3/desktop-systray.html) gives me no warnings at all with Qt/Win 4.3.1.

TheGrimace
25th September 2007, 21:51
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSystemTrayIcon syst;
syst.setIcon(QIcon("dmax_icon.png"));
syst.show();

return app.exec();
}

put the icon of your choice in place of the png