Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use QWebPage in a non-GUI application

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.

like image 524
Albert Avatar asked Dec 27 '22 06:12

Albert


2 Answers

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

like image 71
kanzure Avatar answered Feb 12 '23 17:02

kanzure


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)

like image 20
Dmitriy Avatar answered Feb 12 '23 15:02

Dmitriy