[Qt-interest] Switching QApplication without GUI to use GUI

Daniel Grubbs dgrubbs at dexrex.com
Thu Mar 26 22:01:41 CET 2009


Well, I had to do something. Here's what I came up with...

#include <QtGui>

#include <signal.h>


bool AppExiting = false;

QApplication* pApp;

QDialog* pDialog;

SysTray* pTray;

void term(int param)

{

AppExiting = true;


// Do other clean up stuff here

delete pApp;

exit(0);

}


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

pApp = NULL;


 // Start up threads that do stuff here


pTray = NULL;

 #if defined(LINUX)


// Set function to handle cleanup if we want to quit nicely.

void (*prev_fn)(int);

prev_fn = signal(SIGTERM,term);


// Go through loop checking to see if a GUI shows up.

char line[50];

FILE *fp = popen( "ps -C Xorg --no-headers", "r");

while(!fp || (fp && !fgets(line, sizeof line, fp)))

{

if(fp)

{

pclose(fp);

}

nap(10000);

fp = popen( "ps -C Xorg --no-headers", "r");

}

putenv("DISPLAY=:0.0");

pclose(fp);

signal(SIGTERM,prev_fn);


// GUI's here now we can use QApplication.

pApp = new QApplication(argc, argv);

QApplication::setQuitOnLastWindowClosed(false);


// It might take a while for the SysTray to be initialized.

while(!QSystemTrayIcon::isSystemTrayAvailable())

{

nap(1000);

}

#else

pApp = new QApplication(argc, argv);

QApplication::setQuitOnLastWindowClosed(false);

if (!QSystemTrayIcon::isSystemTrayAvailable()) {

QMessageBox::critical(0, QObject::tr("Systray"), QObject::tr("I couldn't
detect any system tray on this system."));

return 1;

}

#endif

pDialog = new QDialog();

pDialog->hide();

pApp->setQuitOnLastWindowClosed(false);

pTray = new SysTray();

pTray->show();
        pApp->exec();

term(0);

return 0;

}

This seems to work now but it would be nice to be able to shut off the GUI
nicely when xwindows stops, continue running and start the GUI again when it
returns.



On Wed, Mar 25, 2009 at 3:44 PM, Andreas Pakulat <apaku at gmx.de> wrote:

> On 25.03.09 14:46:18, Daniel Grubbs wrote:
> > Hi Folks,
> >
> > I've got an application which I would like to run as a background daemon
> if
> > there is no GUI present, but with a GUI if one is present.  Also, it may
> > start up before the GUI starts and then add some interface once the GUI
> > starts.  Could I start the qApp as QApplication( argc, argv, false) and
> > later change to GUIenabled = true somehow after X window system starts?
>
> No. The only way I can think of is splitting gui and non-gui part into two
> applications which then communicate via some IPC mechanism with each other
> (sockets or stdin/stdout) and the daemon starting the gui-client once it
> knows on which display the X11 server runs.
>
> Andreas
>
> --
> Your aim is high and to the right.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090326/98a44a3d/attachment.html 


More information about the Qt-interest-old mailing list