如何在非 GUI 应用程序中使用 QWebPage

发布于 2025-01-04 05:03:53 字数 1287 浏览 2 评论 0 原文

我想在非 GUI Qt 应用程序中使用 QWebPage。我的意思是我根本不想与窗口服务器通信。不过,使用 QtGui 不是问题。

QWebPage 在内部创建一些 QWidget 实例。因此,使用QCoreApplication是不可能的。

不过,当创建 QApplication 实例时,我已经立即获得了一个 MacOSX 停靠图标。我不想要这样。这也意味着它以某种方式在 Cocoa 中将自己注册为 GUI 应用程序。

我的问题不仅仅限于 Mac。我想知道 Qt 是否有“官方”方式来做到这一点。仅当没有时,我才想知道执行此操作的具体方法,例如目前在 Mac 上。


关于 Mac 更具体一些:

还有可以为应用程序设置的 LSBackgroundOnly 属性包并且它进入了我想要的方向(因此我仍然不确定它是否真的是真正的仅限控制台,例如在没有 Quartz 的情况下也可以工作,等等)。但是,我根本没有应用程序包;它只是一个简单的二进制文件(用作 shell 中的命令行工具)。

现在,我有一个小的解决方法来隐藏停靠图标,但这非常丑陋,因为它首先弹出然后消失:(Python代码,但这并不重要......)

def hideMacDockIcon():
    # http://stackoverflow.com/a/9220857/133374
    import AppKit
    # https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSRunningApplication_Class/Reference/Reference.html
    NSApplicationActivationPolicyRegular = 0
    NSApplicationActivationPolicyAccessory = 1
    NSApplicationActivationPolicyProhibited = 2
    AppKit.NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited)

app = QApplication(sys.argv)
if sys.platform == "darwin":
    hideMacDockIcon()

另外,我不确定是否它也可以在其他环境中工作,可能作为系统守护进程等。

I want to use QWebPage in a non-GUI Qt application. By that, I mean that I don't want to communicate with the window server at all. Using QtGui is not a problem, though.

QWebPage internally creates some QWidget instances. Thus, using QCoreApplication is not possible.

When creating a QApplication instance though, I already immediately get a MacOSX dock icon. And I don't want that. It also means that it somehow registers itself in Cocoa as a GUI application.

My question is not Mac-only. I would like to know if there is an "official" way for Qt to do this. Only if there is not, I would like to know specific ways to do this, e.g. on Mac for now.


Somewhat more specific about Mac:

There is also the LSBackgroundOnly property which can be set for an App bundle and which goes into the direction to what I want (whereby I'm still not sure if it is really truly console-only, e.g. would also work without Quartz, etc.). However, I don't have an App bundle at all; it's just a simple binary (to be used as a command-line-tool in shells).

For now, I have a small workaround to hide the dock icon but that is quite ugly as it first pops up and then goes aways: (Python code but that doesn't really matter...)

def hideMacDockIcon():
    # http://stackoverflow.com/a/9220857/133374
    import AppKit
    # https://developer.apple.com/library/mac/#documentation/AppKit/Reference/NSRunningApplication_Class/Reference/Reference.html
    NSApplicationActivationPolicyRegular = 0
    NSApplicationActivationPolicyAccessory = 1
    NSApplicationActivationPolicyProhibited = 2
    AppKit.NSApp.setActivationPolicy_(NSApplicationActivationPolicyProhibited)

app = QApplication(sys.argv)
if sys.platform == "darwin":
    hideMacDockIcon()

Also, I'm not sure if it also works in other environments, maybe as a system daemon or so.

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

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

发布评论

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

评论(6

娇纵 2025-01-11 05:03:53

您可以使用 QPA 来做到这一点。这恰好是 PhantomJS 实现无头的方式。修改 QT 预配置文件以指定 QPA:


QT_CFG +=' -qpa' # X11-less with QPA (又名 Lighthouse)

还有一些关于 QMinimalWindowSurface 的内容。

https://github.com/ariya/phantomjs/commit/6c8a1c2dc1
https://github.com/ariya/phantomjs/commit/c78ae190a9

You can do this with QPA. This happens to be how PhantomJS achieved headlessness. The QT preconfig file was modified to specify QPA:


QT_CFG +=' -qpa' # X11-less with QPA (aka Lighthouse)

Also something about QMinimalWindowSurface.

https://github.com/ariya/phantomjs/commit/6c8a1c2dc1
https://github.com/ariya/phantomjs/commit/c78ae190a9

陈甜 2025-01-11 05:03:53

QApplication 初始化 QWidget 使用的静态变量。因此,在创建 QApplication 实例之前,您将无法创建任何小部件。

如果您需要浏览器,请尝试使用 WebkitAwesomium(商业)或 chromiumoffscreenrenderer(LGPL 分支)

QApplication initializes static variables that are used by QWidgets. So you will not be able to create any widgets until you create an QApplication instance.

If you need a browser try using Webkit, Chromium, Berkelium, Awesomium(commersial) or chromiumoffscreenrenderer(LGPL fork)

还不是爱你 2025-01-11 05:03:53

您是否尝试过将“no gui”标志传递给 QApplication?

QApplication ( int & argc, char ** argv, bool GUIenabled )

Have you tried passing the 'no gui' flag to QApplication?

QApplication ( int & argc, char ** argv, bool GUIenabled )
や莫失莫忘 2025-01-11 05:03:53

恐怕没有不使用 QtGui 的简单方法。如果您查看 QWebPage的源代码,你会看到使用了QPainter,作为QtGui导出的一些方法\对象。这是预期的,因为 API 中具有诸如 QWidget* QWebPage::view() const 之类的函数。

您可以破解源代码思想,但您的 Qt 库是唯一且不兼容的。这是一个负担。

I am afraid there is no simple way of not using QtGui. If you look at the source code of QWebPage, you see that a QPainter is used, as some exported methods\objects from QtGui. This was expected as you have functions like QWidget* QWebPage::view() const in the API.

You can hack the source code thought but then your Qt libraries are unique and incompatible .That is a burden.

月下凄凉 2025-01-11 05:03:53

您想使用 QWebPage 做什么?
也许有更适合您需求的课程?
如果没有:可以选择从 QWebPage 的源代码复制并粘贴。

更新:
您想创建类似命令行浏览器的东西吗?或者只是看起来像网络服务器的浏览器的东西?
在这些情况下,您可能只是隐藏 QWidget,这样停靠栏中就不会出现任何内容(不确定这是否是 OS X 上的工作方式;在 Windows 上,可能会出现没有任务栏条目的窗口,我认为)。

What is it that you want to use QWebPage for?
Maybe there is a class better suited for your needs?
If not: Copy and pasting from QWebPage's source code is an option.

Update:
Do you want to create something like a command line browser? Or just something that looks like a browser to a web server?
In these cases you might just hide the QWidget so nothing appears in the dock bar (not sure if this is how it works on OS X; on Windows it's possible to have windows with no task-bar entry, I think).

小忆控 2025-01-11 05:03:53

PyPhantomJs 是一个使用 pyqt 的无头网页浏览器,甚至 IT 也使用 QApplication: http://code.google.com/p/phantomjs/source/browse/python/pyphantomjs/pyphantomjs.py?name=4ec8df3a84&r=4dc051a60ec3d59bf125838a5caa2a24d59bd0ee

您始终可以使用各种使应用程序作为系统托盘应用程序运行的窗口标志

更新

因为我看到您正在使用osx,您可以将此设置添加到您的应用程序plist中,使其作为不带图标的系统服务启动:http://www.cocoadev.com/index.pl?LSBackgroundOnly

我将其用于位于顶部任务栏并提供聚光灯风格的界面

PyPhantomJs is a headless webrowser using pyqt, and even IT uses QApplication: http://code.google.com/p/phantomjs/source/browse/python/pyphantomjs/pyphantomjs.py?name=4ec8df3a84&r=4dc051a60ec3d59bf125838a5caa2a24d59bd0ee

You can always just Use the various window flags that make the app run as a system tray app

update

since I see you are using osx, you can add this setting to your apps plist to make it launch as a system service with no icons: http://www.cocoadev.com/index.pl?LSBackgroundOnly

I use this for an app that sits in the taskbar on the top and provides a spotlight style interface

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