Archive for the ‘programming’ Category

XULRunner rocks!

March 15, 2011 Leave a comment

For one of my projects, I needed an application to display a web page in full screen mode. At first, I used Firefox with the AutoHide extension, but this solution was more of a hack and not easy to deploy to multiple machines — I worked with a pre-configured user profile that was copied every time the application started. Furthermore, after each update, Firefox would check for compatibility of installed plugins and displayed a nasty dialog in the meantime.

So I tried to move away from Firefox and do something on my own, something slim which did just what I wanted, nothing more, and do it good — according to the UNIX philosophy. But writing another C/C++/Python/whatever application from scratch was not an option (implementing an HTML renderer would be a pain, and I didn’t fancy reading extensive manuals about WebKit, Gecko or any other rendering engine).

After a while of thinking, which included thought fragments of Songbird and Conkeror, I decided to give XULrunner a shot (for those who do not know, XUL is the XML-based user interface language used by the Mozilla applications and the Firefox and Thunderbird extensions, and XULRunner is an interpreter and run-time environment for XUL documents).

So after a while of hacking (there is a good tutorial on the Mozilla Developer Network), I ended up with a few lines of code:

File ./chrome/content/main.xul:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="main.css" type="text/css"?>
<window xmlns=""
  id="viewer" windowtype="viewer" title="Infopoint HTML View" 

  <hbox flex="1">
  <iframe id="contentview" flex="1"
    src="chrome://infopointhtmlviewer/content/default.html" />
    // load URI given on command line
    var content = document.getElementById("contentview");
    var cmdLine = window.arguments[0].QueryInterface(
    var uri = content.getAttribute("src");
    alert("Default URL: " + uri);
    if(cmdLine.length > 0) {
      uri = cmdLine.getArgument(0);

    if(content != null) {
      content.setAttribute("src", uri);

    // resize to full screen
    window.resizeTo(screen.width, screen.height);

The above code is in the public domain.

And that was basically all of it. I was surprised that there was nothing more to it.

You can get the source code of the full application on Github.

Flattr this

Qt: (Nearly) synchronous QNetworkAccessManager calls

July 8, 2010 9 comments

The QNetworkAccessManager class is very user-friendly, but it makes asynchronous calls. I was in the need for synchronous calls to handle my HTTP communication, but I did not want the overhead of another thread, so I googled a bit and finally came up with a short call to an event loop that processed the request. Like this:

QNetworkAccessManager * pnam = new QNetworkAccessManager(this);
// the slot was declared at another place
connect(pnam, SIGNAL(finished(QNetworkReply *)), this,
QNetworkRequest req(QUrl(""));
pnam->post(req, postData);

// execute an event loop to process the request (nearly-synchronous)
QEventLoop eventLoop;
// also dispose the event loop after the reply has arrived
connect(pnam, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));

This way my user-defined slot for the pnam->finished() signal was called immediately, and I could be sure to have the HTTP reply at the end of this code snippet.

Found here: Qt-Interest Mailing List: QNetworkAccessManager and QNetworkReply, synchronous

Flattr this

Qt: Throw exceptions from signals and slots

July 8, 2010 2 comments

By default, you can not throw exceptions from signals and slots:

Qt has caught an exception thrown from an event handler. Throwing
exceptions from an event handler is not supported in Qt. You must
reimplement QApplication::notify() and catch all exceptions there.

So, what to do? The answer is simple: Overwrite the function bool QApplication::notify(QObject * receiver, QEvent * event) so that it catches all thrown exceptions. Here is some sample code:

#include <QtGui>
#include <QApplication>
class MyApplication : public QApplication {
  MyApplication(int& argc, char ** argv) :
    QApplication(argc, argv) { }
  virtual ~MyApplication() { }

  // reimplemented from QApplication so we can throw exceptions in slots
  virtual bool notify(QObject * receiver, QEvent * event) {
    try {
      return QApplication::notify(receiver, event);
    } catch(std::exception& e) {
      qCritical() << "Exception thrown:" << e.what();
    return false;

int main(int argc, char *argv[]) {
  MyApplication app(argc, argv);
  // ...

Of course, you can also inherit from QCoreApplication to get rid of the QtGui dependency, or display a nice dialog box instead of printing the messages to the console, or…

Found at: Stack Overflow: Qt and error handling strategy

Flattr this

Categories: howto, programming Tags: , , ,