Results 1 to 7 of 7

Thread: Fatal error occur when crating instance of messagebox in therad function.

  1. #1
    Join Date
    Aug 2013
    Location
    India
    Posts
    43
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Fatal error occur when crating instance of messagebox in therad function.

    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;
    }

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fatal error occur when crating instance of messagebox in therad function.

    And what is a problem ? The message is very clear. GUI functions can only be called from the main (GUI) thread.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Fatal error occur when crating instance of messagebox in therad function.

    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:

    Qt Code:
    1. {
    2. QMessageBox messageBox;
    3. // ...
    4. messageBox.exec();
    5. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Aug 2013
    Location
    India
    Posts
    43
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Fatal error occur when crating instance of messagebox in therad function.

    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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,481
    Thanks
    252
    Thanked 711 Times in 701 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Fatal error occur when crating instance of messagebox in therad function.

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    Join Date
    Aug 2013
    Location
    India
    Posts
    43
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Fatal error occur when crating instance of messagebox in therad function.

    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.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,499
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Fatal error occur when crating instance of messagebox in therad function.

    Read doc about QObject::connect and especially about the connection type.

Similar Threads

  1. Replies: 1
    Last Post: 30th September 2016, 11:34
  2. Replies: 3
    Last Post: 19th November 2014, 19:44
  3. fatal error C1001: An internal error has occurred in the compiler
    By noodles in forum Installation and Deployment
    Replies: 0
    Last Post: 12th August 2010, 11:24
  4. Fatal error ..!!!!
    By joseph in forum Qt Programming
    Replies: 9
    Last Post: 13th November 2007, 14:05
  5. QT4 beginner Fatal Error
    By Remyfr in forum Installation and Deployment
    Replies: 3
    Last Post: 11th March 2006, 01:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.