tags:

views:

55

answers:

2

We need to create & destroy instances of QApplication, as we want to use Qt in a plug-in to an existing host application.

void multiQT()
{
    int argc = 0;
    QApplication app(argc, NULL);

    QWebView view;
    view.setHtml("<html><head><title>Title</title></head><body><h1>Hello World</h1></body></html>");
    view.show();

    app.exec();
}


main(int argc, char** argv)
{
    // First call works fine, QWebView renders the HTML just fine
    multiQT();

    // Second call fails, QWebView strips HTML tags from HTML text and 
    // and renders "TitleHello World"
 multiQT();
}

When showing the QWebView the second time, it does not render the HTML properly. Do we need to do some additional (re-)initializations in QApplication or QWebView?

+1  A: 

You might have run into something that has been very lightly tested, the QApplication object among others creates/holds some of the rendering context information of widgets, I don't think it was ever planned for people to take it down and put it back up again. There might be some static content that does not get reinitialised correctly when somebody tries what you are trying to do.

Harald Scheirich
I already suspected that QApplication wasn't meant to be destructed and constructed during a single process. I was hoping there was a workaround of some kind. Thanks for your answer
Matthias Ganninger
A: 

You are supposed to have only 1 QApplication object, and only 1 call to exec(). Maybe you should try this.

QWebView * multiQT()
{
    QWebView *view = new QWebView;
    view->setHtml("<html><head><title>Title</title></head><body><h1>Hello World</h1></body></html>");
    view->show();

    return view;
}

main(int argc, char** argv)
{
    QApplication app(argc, NULL);

    QWebView * web0 = multiQT();

    QWebView * web1 = multiQT();

    app.exec();
}
Didier Trosset
Thanks for your answer. Your solution works, but thats not what I need. I am using Qt inside an existing plug-in application. I really need to construct and destroy the QApplication on the fly.
Matthias Ganninger