QtConcurrent::run for loading HTML page crashes app
-
Hello,
I have been trying for several days to speed up the load process of an HTML file from disk. using
Qt6.5.1
onUbuntu 22.04LTS
and Qt Creator10.0.2
.In
mainwindow.h
I define:QWebEnginePage *page = new QWebEnginePage(); //as a public object private: void workerFunction();
In constructor of
mainwindow.cpp
this is the relevant code:ui->setupUi(this); QFuture<void> t1 = QtConcurrent::run([this](){ qDebug()<<"In thread"; workerFunction(); }); connect(page,SIGNAL(loadFinished(bool)),this,SLOT(readPage(bool))); //couldn't get QWebEnginePage::loadFinished(bool) to work //so using the ancient SIGNAL/SLOT
And the
workerFunction()
is defined:void MainWindow::workerFunction(){ page->load(QUrl("qrc:/html_page.html")); }
readPage(bool)
is a slot where html is parsed and ui is set so I keep this away from the separate thread.The app however crashes. I have tried many variants of QConcurrent::run, but they always gave errors. This doesn't give errors but it crashes.
I triedQFuture<void> t1 = QtConcurrent::run(workerFunction);
withextern
added beforevoid
in definition of function as per official documentation. It saidReference to non-static member function must be called...
, so I ditched it and tried:QThreadPool pool; QFuture<void> t1 = QtConcurrent::run(&pool, workerFunction);
which is also from official docs and got same error as above. I tried creating a worker class as per this answer but it also crashed. I used
t1.waitForFinished()
but it also gives same error.
If I just have aqDebug()
in lambda function it runs fine. As soon as I addpage->load...
it crashes.How do I use concurrency and threads to speed up my page load (if not parsing) from disk?
-
You must not access gui elements from outside the main (gui) thread and there is no need since QWebEnginePage::load() is asynchronous (like all such functions).
-
@Christian-Ehrlicher Okay thank you. But how do I speed the loading up? File is just 50KB. I load it, parse the HTML and display on listWidget on UI. On 16GB RAM ubuntu it loads well (could be better), but on Windows 11 with 8GB RAM, it takes a few seconds.
-
@MH24 I don't know but a thread will (when it would work) also not speed it up.