Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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)

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

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

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.

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).

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top