Systray

From qtnode

Jump to: navigation, search

Contents

A System Tray Icon in Qt 4.2

The new QSystemTrayIcon Class

If you want the usual trayicon behaviour for your mainwindow (i.e. closing it leaves the application running with just the trayicon being visible) then you could use something like this:

/**
 * Sets up trayicon and links single left mouseclick to show/hide
 **/
void MyMainWindow::setupTray()
{
	mTray = new QSystemTrayIcon( this );

	// NOTE: Always have an icon set befor calling show()
	mTray->setIcon ( QIcon("someicon.png") );

	//mTray->setContextMenu( yourTrayMenu );

	connect(mTray, SIGNAL(activated(int)), this, SLOT(slotTrayActivated(int)));

	mTray->show();
}

void MyMainWindow::slotTrayActivated(int reason)
{
	if (reason == QSystemTrayIcon::Trigger)
		slotToggleVisibility();
}


/**
 * Slot to call from somewhere else if you want to
 * show/hide to/from systray
 **/
void MyMainWindow::slotToggleVisibility()
{
	if(isHidden())
	{
		show();
		if(isMinimized())
		{
			if(isMaximized())
				showMaximized();
			else
				showNormal();
		}
		raise();
		activateWindow();
	}
	else
	{
		hide();
	}
}


void MyMainWindow::closeEvent(QCloseEvent *e)
{
	e->accept();
	// only quit if there is no trayicon
	if (!mTray)
		qApp->quit();
}

A System Tray Icon for Earlier Qt Versions

Psi Trayicon Code for Qt 4.0.x and 4.1.x

A port of the Psi trayicon for Qt4: trayicon.tar.bz2.

Using the TrayIcon class from above is not very complicated but involves a bit more than just "new TrayIcon()". Make sure you check out the examples directory first.

Another Systray Implementation

http://notry.net/dump/code/systemtray/

Personal tools