PDA

View Full Version : error in qt concurrent



anbu01
22nd December 2014, 11:29
hi,
I am trying some example on Qt::Concurrent using this example http://codeprogress.com/cpp/libraries/qt/QTConcurrentAvoidGUIFreezing.php#.VJgBKf8Fzw but application crashes for some reason.Can any one help on this.

It says
cannot send events to objects owned by different thread.What should i do to overcome this.

thanks,

anda_skoa
22nd December 2014, 11:58
That example is broken in so many ways that it is hard to believe anyone actually posted it on the Internet.

Luckily the Qt documentation has an example as well: http://qt-project.org/doc/qt-4.8/qtconcurrent-runfunction.html

Cheers,
_

anbu01
22nd December 2014, 12:29
hi,
That example is good but if i use widgets(using the documentation example) i get the same error.Is the problem because i try to access widgets from different thread.so how we can make make a responsive Gui where data runs in background.I have attached the code that i tried.

void colorizeButton1()
{
int index = 0;
while(true)
{
if(index%2 == 0)
{
button1->setPalette(*(new QPalette(Qt::green)));
}
else
{
button1->setPalette(*(new QPalette(Qt::yellow)));
}
index++;
QThread::sleep(1);
}
}


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

QMainWindow *window = new QMainWindow();

QWidget *central = new QWidget(window);
button1 = new QPushButton(central);

QHBoxLayout* layout = new QHBoxLayout(centralWidget);

layout->addWidget(button1);

window->setCentralWidget(central);

QFuture<void> f1 = QtConcurrent::run(colorizeButton1);

f1.waitForFinished();
window->show();

app.exec();
}
thanks,

anda_skoa
22nd December 2014, 14:05
That example is good
I am sorry, but no, it is not.

An example that does not work is not a good example.
An example that leaks memory like crazy is not a good example.
An example that ignores the requirements for access to UI resources in multithreading context is not a good example.

The usage of global variables does not help either.



I have attached the code that i tried.

So you are taking the worst possible example and attempt it make it even worse by waiting for the end of a never ending function?

I am afraid we have a very different definition of "good" when it comes to examples on programming.

Cheers,
_

wysota
23rd December 2014, 07:01
The example is really bad indeed... Using QTimer would be sufficient to obtain the same functionality without any threading.

By the way, in some conditions the discussed example function might not even execute or it can break other functionality using Qt Concurrent by starving jobs.