Qmessagebox throw excepton



  • Hi, i have a thread. in that i wrote:
    QMessageBox::information(0,"ss","ss");
    but it has following error:
    !http://upload7.ir/images/03962076836726290781.jpg(Error)!
    any idea?


  • Moderators

    yep...don't use QMessageBox outside of gui thread ;)
    open the QMessageBox in a slot in the gui thread and call it with a queued connection signal.

    Edit. this doesn't even need to be the reason for the crash. Can you run the application in your IDE (in debug mode) and check the crash again?



  • on QMessageBox it throw exception. can you explain more how can i fix that?


  • Moderators

    as i said... create a slot which shows the message box in the gui thread.
    Meaning create the slot for example in your main window:

    @
    void MyMainWindow::showInformation(...) //SLOT
    {
    QMessageBox::information(this, ...);
    }
    @

    Then create in your thread class:
    @
    class MyThread : public QThread
    {
    public:
    MyThread() { ... }

    signals:
    void showInfoBox(...);

    protected:
    virtual void run()
    {
    ....
    emit showInfoBox(...);
    ....
    }
    }
    @

    and where you create your thread object (assuming in your main window class):
    @
    MyThread* myThread = new MyThread;
    connect(myThread, SIGNAL(showInfoBox(...)), this, SLOT(showInformation(...)), Qt::QueuedConnection);
    @



  • thanks. i will try that.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.