QMessageBox and Thread Slot
-
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_OBJECTprivate:
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_OBJECTprivate:
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:- 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
- The user push the button ChgStateobj1