如何在后台启动应用程序,即不显示 GUI?

发布于 2024-09-10 23:57:24 字数 178 浏览 1 评论 0原文

我正在使用 Qt 进行屏幕截图(丝网打印)。

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

我希望应用程序在后台启动,即我希望它在启动时隐藏(甚至在控制台模式下运行)。
如何在 Qt 中做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安人多梦 2024-09-17 23:57:25

据我了解,您需要一个在执行时不显示小部件(UI)并在后台运行的应用程序。

实现此目的的一种方法是启动应用程序并在系统托盘中显示图标,而不是在屏幕上显示对话框/窗口。

您可以这样编写代码:

// 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();
}

您还需要设置一些其他内容。您可以参考系统托盘图标示例 为此。


2022 年更新:
如果您正在寻找使用全局快捷方式调用应用程序的方法,您可能需要查看 Qxt,因为 libqxt 不再维护,并且可能无法与较新版本的 Qt 一起使用。

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.

夏末染殇 2024-09-17 23:57:25

您可以从 Qt 控制台应用程序开始。

请记住在标头中包含

打开您的 .PRO 文件,删除带有 -= gui 的行

You can start with Qt Console Application.

Remember to include <GtGui> in your headers.

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

孤独岁月 2024-09-17 23:57:25

我建议您使用 QCoreApplication 而不是 QApplication:

// main.cpp
#include <QCoreApplication>

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

顺便说一句,您可以在 QtCreator 中选择 Console App,但我总是使用 Empty Project。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文