PDA

View Full Version : Second Application opening QSystemTray



JediSpam
10th December 2011, 18:12
I currently have an application that is using QSystemTray and works well. I now have another application that needs to open up the first application if it's minimized to the tray OR on the start bar. This is the current code I have to open it.



Function
{
HWND hwnd=FindWindow(TITLE)
ShowWindow(hwnd, SW_SHOW);
if(!SetForegroundWindow(hwnd))
{
qWarning("Unable to find");
}
}


The only issue is that once it opens the entire application is WHITE and freezes. I'm guessing this is because Qt still thinks it's closed and won't show the dialog correctly. Any ideas on what's a workaround for this? Thanks!!!!

Spitfire
12th December 2011, 10:49
Use QtSingleApplication (http://doc.qt.nokia.com/solutions/4/qtsingleapplication/qtsingleapplication.html) for your system tray app and QProcess (http://doc.qt.nokia.com/latest/qprocess.html) to launch/show it from any other app.

When your system tray app is launched using QProcess, in main() you just check if app is already running, and if it does - show it.

something like this:

int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);

if (app.isRunning())
{
app.show();
return 0;
}

MyMainWidget mmw;

app.setActivationWindow(&mmw);

mmw.show();
return app.exec();
}

JediSpam
13th December 2011, 21:32
I actually have SingleApplication on my application already with this main.cpp. What is the QProcess going to look like? Been trying some of its function but having no luck.

Spitfire
14th December 2011, 11:41
QProcess p(this);
p.start("Path/to/your/app/app.exe");
If main() in your app looks like the one few posts up (if app is running it shows it and bails out) then every attempt to launch the app while it's running will cause it to be shown.

JediSpam
15th December 2011, 16:45
Ok let me give that a shot. I tried something similar but will try again

JediSpam
15th December 2011, 23:04
From what you showed me,



int main(int argc, char **argv)
{
QtSingleApplication app(argc, argv);

if (app.isRunning())
{
app.show(); //THIS CAN'T BE DONE
return 0;
}

MyMainWidget mmw;

app.setActivationWindow(&mmw);

mmw.show();
return app.exec();
}


This is what I currently have tried. What happens is that a second instance of the application appears for a short second then disappears

My Main.CPP


int main(int argc, char **argv)
{
QtSingleApplication app(argc,argv)

MainWindow w;

if (app.isRunning())
{
w.show(); //MAYBE I SHOULD TRY THIS? DOESN'T DO WHAT I WANT
//app.show();
return 0;
}

Q_INIT_RESOURCE(tray);
if (!QSystemTrayIcon::isSystemTrayAvailable()) {
QMessageBox::critical(0, QObject::tr("Systray"),
QObject::tr("I couldn't detect any system tray on this system."));

return 1;
}
QApplication::setQuitOnLastWindowClosed(false);

//MainWindow w;
app.setActivationWindow(&w);
w.hide();
return app.exec();
}



External program calling


QStringList s;
s << "C:\\PATH.EXE;
QProcess process(s);
process.start();

Spitfire
16th December 2011, 10:29
Ok, my bad.
I was guessing the interface.

Quick search on google takes you here (http://doc.qt.nokia.com/solutions/4/qtsingleapplication/qtsingleapplication.html#activateWindow), so the correct method is
void QtSingleApplication::activateWindow() [slot]
Read through the link above, it should help you with QtSingleApplication. It also shows you how otherwise you could solve your problem.

JediSpam
16th December 2011, 14:28
ah crap i saw that slot! i didn't think it would apply for some reason :mad: