Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pop up a QMessageBox from a QThread
Qt 6.11 is out! See what's new in the release blog

Pop up a QMessageBox from a QThread

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Macro
    wrote on last edited by
    #1

    I'm in a thread and I would like to pop up a QMessageBox displaying a Text when a QPushButton is clicked, how do I do it?
    Here's my code...

    myThread.h

    @#ifndef MYTHREAD_H
    #define MYTHREAD_H

    #include <QWidget>

    namespace Ui {
    class myThread;
    }

    class myThread: public QWidget
    {
    Q_OBJECT

    public:
    explicit myThread(QWidget *parent = 0);
    ~myThread();

    private slots:
    void on_pushButton_clicked();
    void workThread();
    };

    #endif // MYTHREAD_H@

    myThread.cpp

    @#include "myThread.h"
    #include "WorkThread.h"
    #include <QThread>

    myThread::myThread(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::myThread)
    {
    ui->setupUi(this);
    }

    myThread::~myThread()
    {
    delete ui;
    }

    void myThread::on_pushButton_clicked()
    {
    workThread();
    }

    void myThread::workThread()
    {
    WorkThread *worker = new WorkThread(this);
    QThread *workerThread = new QThread(this);

    connect(workerThread, &QThread::started, worker, &WorkThread::doWork);
    worker->moveToThread(workerThread);
    
    workerThread->start();
    

    }@

    WorkThread.h

    @#ifndef WORKTHREAD_H
    #define WORKTHREAD_H

    #include <QObject>

    class WorkThread : public QObject
    {
    Q_OBJECT
    public:
    explicit WorkThread(QWidget *parentWidget, QObject *parent = 0);

    public slots:
    void doWork();

    private:
    QWidget *parentWidget;
    };

    #endif // WORKTHREAD_H@

    WorkThread.cpp

    @#include "WorkThread.h"
    #include <QMessageBox>

    WorkThread::WorkThread(QWidget *parentWidget, QObject *parent) :
    QObject(parent)
    {
    }

    void WorkThread::doWork()
    {
    QMessageBox *mBox = new QMessageBox(parentWidget);
    mBox->setText("This is my Text");
    }@

    Here my application gets crashed which says that widgets must be created in the GUI Thread? Please Suggest.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You can't create widgets in any other thread than the main thread (the one from app.exec()) If you want to popup a message box based on something happening in a thread, you must emit a signal from that thread back to e.g. your main window and use your message box there

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved