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. QMessageBox and Thread Slot

QMessageBox and Thread Slot

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.0k 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.
  • L Offline
    L Offline
    lalyperu
    wrote on last edited by
    #1

    Good morning,

    I have some doubts about the implementation of QMessageBox... To better explain the problem I post the code..

    myobject.h
    @class myobject: public QObject
    {
    Q_OBJECT

    private:
    bool GoToOtherState=false;
    QTimer *timer;

    public:
    myobject();
    ~myobject();

        public slots:
     void doWork();
     void ChangeState();
    

    };
    @

    myobject.cpp
    @
    myobject::myobject()
    {
    qDebug() <<"myobject constructor "<<"Thread ID: "<< QThread::currentThreadId()<< endl;
    timer=new QTimer(this);
    timer->setInterval(100);
    connect(timer, SIGNAL(timeout()), this, SLOT (doWork()));
    timer->start();
    }
    void myobject::doWork()
    {
    if (GoToOtherState)
    {
    // implementation of code......
    }
    GoToOtherState =false;
    }
    myobject::ChangeState()
    {
    GoToOtherState=true;
    }
    @

    mainwidget.h
    @class mainwidget: public QWidget
    {
    Q_OBJECT

    private:
    myobject *obj1;
    myobject *obj2;
    QMessageBox *msgbox;

    public:
    mainwidget();
    ~mainwidget();

    public slots:
    void sendMessage();
    };
    @
    mainwidget.cpp
    @
    mainwidget::mainwidget()
    {
    qDebug() <<"mainwidget constructor "<<"Thread ID: "<< QThread::currentThreadId()<< endl;

    msgbox = new QMessageBox;

    obj1= new myobject();
    obj2= new myobject();

        QThread *threadobj1 = new QThread;
    

    QThread *threadobj2 = new QThread;

      obj1->moveToThread(threadobj1 );
    

    connect(threadobj1 , SIGNAL(started()), obj1, SLOT(doWork()));

    obj2->moveToThread(threadobj2 );
    connect(threadobj2 , SIGNAL(started()), obj2, SLOT(doWork()));

      QPushButton *ChgStateobj1 = new QPushButton("ChgStateobj1",this);
    

    //connect(ChgStateobj1 , SIGNAL(clicked()), obj1, SLOT(ChangeState()));
    connect(ChgStateobj1 , SIGNAL(clicked()), this, SLOT(sendMessage()));

    QPushButton *ChgStateobj2 = new QPushButton("ChgStateobj2",this);
    //connect(ChgStateobj2 , SIGNAL(clicked()), obj2, SLOT(ChangeState()));
    connect(ChgStateobj2 , SIGNAL(clicked()), this, SLOT(sendMessage()));

    QHBoxLayout *layout = new QHBoxLayout;

    layout->addWidget(ChgStateobj1 );
    layout->addWidget(ChgStateobj2);
    
    this->setLayout(layout);
    

    threadobj1 ->start();
    threadobj2->start();
    }

    void mainwidget::sendMessage()
    {
    qDebug() <<"mainwidgets sendMessage "<<"Thread ID: "<< QThread::currentThreadId()<< endl;
    QMessageBox ::StandardButton reply;
    reply = QMessageBox::question(this, "Message", "Are you sure to change the state?", QMessageBox::Yes|QMessageBox::No);
    ///................
    }
    @

    main.cpp
    @int main(int argc, char *argv[]) {

    QApplication app(argc, argv);
    mainwidget *mywindow=new mainwidget();
    mywindow->show;
    

    mywindow->exec();
    @

    As you can see in the constructor of mainwidget, in the first time I connected the signal clicked() of the pushbuttons ChgStateobj1 and ChgStateobj2 with the slot ChangeState that will be executed in the two different threads (obj1 e obj2). This work correctly.
    For Added security before calling the slots "ChangeState" I would implement a Dialog with the user via a QMessageBox... So the idea is:

    1. The user push the button ChgStateobj1
      2)A QMessageBox appear
      3)If the user clicks yes the slot ChangeState will be called and executed in the thread threadobj1.
      The same is for the button ChgStateobj2.
      So I don't know which is the correct way to implement QMessageBox, Any ideas?

    Thanks

    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