PDA

View Full Version : Fatal error occur when crating instance of messagebox in therad function.



prabhatjha
10th April 2020, 08:21
Hello Everyone,

I have to create a QmessgeBox in thread function. But getting runtime error at QMessageBox * msgBox this line.
fatal: assert failure in qwidget: "widgets must be created in the gui thread.", file kernel\qwidget.cpp:1125.
Please assist me.

bool MainWindow::thread_cleaning()
{
QMessageBox * msgBox;
msgBox->setWindowTitle("Confirmation");
msgBox->setText("Match Data already exists in current project do you want to override");
msgBox->setStandardButtons(QMessageBox::Yes);
msgBox->addButton(QMessageBox::No);
msgBox->setDefaultButton(QMessageBox::No);
if (msgBox->exec() == QMessageBox::No)
{
qDebug() << "Incremental SFM skipped" << endl;;
return true;
}
return true;
}

Lesiok
10th April 2020, 09:16
And what is a problem ? The message is very clear. GUI functions can only be called from the main (GUI) thread.

d_stranz
10th April 2020, 17:32
GUI functions can only be called from the main (GUI) thread.

And so one solution is to send a signal from the worker thread to the GUI thread, and the slot in the GUI thread posts the message box. As in your other post, if the worker thread needs to know the user's response to the message box, then this signal should pass a reference to a "result" variable that the slot can fill and pass back.

By the way, your method of creating a QMessageBox using new() creates a memory leak. The more common way to create a QMessageBox that is only used within a local scope is to create it on the stack or use the QMessageBox static methods:



{
QMessageBox messageBox;
// ...
messageBox.exec();
}

prabhatjha
11th April 2020, 16:14
Thank you so much for your reply.
I did the same what you have suggested.
int result = 0; // ALWAYS initialize in case the slot doesn't update "result"

emit mySignal( result );

but getting error. warning:QObject::connect: cannot queue arguments of type 'ínt&'(Make sure 'int&' is registered using qRegisterMetatype())
please assit me how to register int & in qRegisterMetatype and where we need to resgister.

d_stranz
11th April 2020, 17:06
Change the argument to "int *" and pass the argument as "&result". Change the slot argument to "int *" also, and be sure to check that the pointer is non-null before you try to assign to it ( if ( result ) *result = 42; )

I guess you can't pass references across thread boundaries.

prabhatjha
13th April 2020, 07:22
Thank you so much for your response.
I have made the changes in argument from reference to pointer but emit is not waiting for the return value which we are setting on messageBox button click.
for refrence:
int retval = 0;
emit sigOpenMessageBox(&retval)
if( retval == 1)
{
qDebug()<< "Yes button click in messageBox "
}
else
{
qDebug()<< "No button click"
}
here emit is not waiting for button click response. next line of code is executing before button click response.

Lesiok
13th April 2020, 16:44
Read doc about QObject::connect and especially about the connection type.