user63898
user63898

Reputation: 30885

How can I start application in background, i.e. without showing GUI?

I'm using Qt for taking screenshots (screen printing).

QPixmap::grabWindow(QApplication::desktop()->winId());

I'd like the application to start in background, i.e. I want it to be hidden when it starts (or even run in console mode).
How can I do that in Qt?

Upvotes: 2

Views: 8639

Answers (3)

As I understand, you need an app that, when executed, doesn't show a widget (UI) and runs in the background.

One way to achieve this is to launch the app and show an icon in System tray instead of showing a dialog/window on the screen.

You can code this something like this:

// main.cpp

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // this is the important bit
    app.setQuitOnLastWindowClosed(false);
    
    // Say "Window" is your class which inherits QWidget or QDialog or QMainWindow
    Window window;
    // Don't call window.show()
    // Setup and invoke system tray icon in the constructor

    return app.exec();
}

There are a few other things you'd need to set. You can refer to the System Tray Icon Example for that.


Update 2022:
If you are looking for a way to invoke the app using a global shortcut you might wanna look into options other than Qxt, as libqxt is no longer maintained and might not work with newer versions of Qt.

Upvotes: 8

Andrei Moiseev
Andrei Moiseev

Reputation: 4074

I recommend you to use QCoreApplication instead of QApplication:

// main.cpp
#include <QCoreApplication>

int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
// ...
return app.exec();
}

By the way, you can select Console App in QtCreator, but I always use Empty Project.

Upvotes: 1

shader
shader

Reputation: 447

You can start with Qt Console Application.

Remember to include <GtGui> in your headers.

Open your .PRO file, remove the line with -= gui

Upvotes: 1

Related Questions