Multiple threads to open the same url in console application
-
Hi ,
I am new to this great QT environment and working on a demo application.
Basically I need to open urls in the console application.- Once the url is finished loading , I need to sleep / wait that thread for a few seconds
- Meanwhile I will start another thread and open a different url and do the same kind of thing.
I guess there are two approaches to accomplish this task 1)QteventLoop 2)QThread , I chose QThread.
My code for main.cpp is
@int main(int argc, char *argv[])
{
QApplication app(argc, argv);Thread tr ; //Thread object QWebSettings *webSettings = QWebSettings::globalSettings(); webSettings->setAttribute(QWebSettings::AutoLoadImages, true); webSettings->setAttribute(QWebSettings::JavascriptEnabled, true); webSettings->setAttribute(QWebSettings::DeveloperExtrasEnabled,true); Html5ApplicationViewer *viewer = new Html5ApplicationViewer(); viewer->setOrientation(Html5ApplicationViewer::ScreenOrientationAuto); viewer->showExpanded(); viewer->loadUrl(QUrl(QLatin1String("http://google.com")));--
//where to run the created thread ? as tr.run() works bt doesn't serve the applicaion purpose
return app.exec();}@
I have created a class for thread
@class Thread: public QThread
{
public:
Thread();
void run();
void stop();
private:
volatile bool stopped;
};Thread::Thread()
{
std::cout<<"creating the object";
stopped = false;
}void Thread::run()
{
while(!stopped)
{
std::cout << "running " << std::endl;
}stopped = false;
}
void Thread::stop()
{
stopped = true;std::cout<<"stopped" << std::endl;
}@
The code runs without error but How can I open the set of urls in the same application as in the end it returns app.exec() .
Hope I was clear to explain my problem.
Any help would be appreciated,
Shrikant -
Can anybody help me with this ?
-
Why do you need a different thread? you can use QTimer.
By the way. Merely instantiating Thread object doesnt start the thread, one has to invoke start() method on it. -
Also your QThread subclass implementation of the run function is missing the exec() call, so your Thread has no running eventloop. Have a look at "this link":http://qt-project.org/wiki/QThreads_general_usage .
In case the Html5ApplicationViewer instance is using any Gui class like e.g. QWebView you won't be able to run instances of this class in any other thread but the main thread. If that is what you wanted to do.