PDA

View Full Version : How to use qwebkit module in a Qt library where no QApplication instance available?



quantity
18th March 2011, 03:30
Here I have a problem:

I want to create a QT library used by other C or C++ program. In this library, I use qtwebkit module to do some html and http work. According to QT's requirement, there must be a QApplication (not QCoreApplication) before qtwebkit objects, such as QWebPage can be used. However, we plan to use the library in a console application. In this situation, there's no main function in the library where I can define a QApplication object myself (QApplication a(argc, argv);).

Is there any suggestion on this? Can I init a QApplication object in the library myself? What's more, I'm using other threads in the library, which is more confusing.:(

JohannesMunk
20th March 2011, 14:34
Hi!

Yes, you can create a QApplication yourself and give it a thread to live in. That's what I did in a library intended to be used by non qt applications.



class QtAppLoop : public QThread
{
protected:
void run() {
static int argc = 1;
static char* argv[] = {(char*)"dummy.exe", NULL };
QCoreApplication* app = new QCoreApplication(argc,argv);
app->exec();
}
};

QtAppLoop* apploop = 0;

void init()
{
if (QCoreApplication::instance() == NULL)
{
apploop = new QtAppLoop();
apploop->start(QThread::LowPriority);
}
}

There are probably better solutions than this, but it worked for me!

HIH

Joh