Nominate our 2022 Qt Champions!

Best practice: headless mode

  • Hello,

    I have a software with a GUI and some Qnetwork and Qsettings features (kind of FTP client).

    I would like to add the possibility, when the soft is run with an option like "-auto", to run a 'default' upload with no GUI.

    My question is, is there a way to do it in Qt or do i have to create 2 different part of my soft (one using a QApplication, the other using a QCoreApplication) ?
    I look for this because I implement most of my function directly in my GUI objects :D (noob mistake i guest)

    Of course I look into the doc but I don't know if what I'm looking for even exist so I ask here too.


  • Start refactoring, I'd say, and separate your business logic from your UI.

  • that's what i feared, thanks you Andre.

  • Noboby forces you to show a UI, even when using a QApplication. The OS will still need to load all the UI libraries, which would not be necessary for a QCoreApplication. But that might be OK for your use case.

  • I agree with Tobias. It depends on if you want to simply not show a GUI, or if you really want to run your application in an environment that has no graphical capabilities. For the former, just don't show your widgets. For the latter, then as Andre says you will need to refactor a little. It is normally not too difficult to split out the functionality into a bunch of QObject derived classes.

    If you do want an app capable of running in a non-GUI environment in addition to the GUI version then I would suggest you look at pulling the functionality out into a shared library. You GUI and non-GUI apps then just become slim wrappers around the shared lib.

  • yes, i'll try in the first time to run it just without displaying the GUI (it's for a windows environment so there will be a display anyway) but i was wondering how to start it after running the QApplication.exec()
    i could call a function before and make a little sleep on it to wait for the slot/signal to start but if you have a better idea it's welcome.

  • It depends :-)

    I fyou have only synchronous things without waiting, you could just call it without the exec. otherwise make some QMetaObject::invoke(..., Queued...) stuff to start first thing and ensure to call some QApplication::quit in the end.

  • yes i have some asynchronous stuf in Qftp but QMetaObject::invoke seams to be what i need, thanks ^^

  • welcome.

    You should have some way to ensure, that when everything is done, the eventloop is closed. Otherwise the program runs forever.

  • everything work fine (recursive signal/slot is fun oO), thanks again

  • Good stuff. Glad you got it working. :)

Log in to reply