<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QMessageBox and Thread Slot]]></title><description><![CDATA[<p dir="auto">Good morning,</p>
<p dir="auto">I have some doubts about the implementation of QMessageBox... To better explain the problem I post the code..</p>
<p dir="auto">myobject.h<br />
@class myobject: public QObject<br />
{<br />
Q_OBJECT</p>
<p dir="auto">private:<br />
bool GoToOtherState=false;<br />
QTimer  *timer;</p>
<p dir="auto">public:<br />
myobject();<br />
~myobject();</p>
<pre><code>    public slots:
 void doWork();
 void ChangeState();
</code></pre>
<p dir="auto">};<br />
@</p>
<p dir="auto">myobject.cpp<br />
@<br />
myobject::myobject()<br />
{<br />
qDebug() &lt;&lt;"myobject constructor "&lt;&lt;"Thread ID:  "&lt;&lt; QThread::currentThreadId()&lt;&lt; endl;<br />
timer=new QTimer(this);<br />
timer-&gt;setInterval(100);<br />
connect(timer, SIGNAL(timeout()), this, SLOT (doWork()));<br />
timer-&gt;start();<br />
}<br />
void myobject::doWork()<br />
{<br />
if (GoToOtherState)<br />
{<br />
// implementation of code......<br />
}<br />
GoToOtherState =false;<br />
}<br />
myobject::ChangeState()<br />
{<br />
GoToOtherState=true;<br />
}<br />
@</p>
<p dir="auto">mainwidget.h<br />
@class mainwidget: public QWidget<br />
{<br />
Q_OBJECT</p>
<p dir="auto">private:<br />
myobject *obj1;<br />
myobject *obj2;<br />
QMessageBox *msgbox;</p>
<p dir="auto">public:<br />
mainwidget();<br />
~mainwidget();</p>
<p dir="auto">public slots:<br />
void sendMessage();<br />
};<br />
@<br />
mainwidget.cpp<br />
@<br />
mainwidget::mainwidget()<br />
{<br />
qDebug() &lt;&lt;"mainwidget constructor "&lt;&lt;"Thread ID:  "&lt;&lt; QThread::currentThreadId()&lt;&lt; endl;</p>
<p dir="auto">msgbox = new QMessageBox;</p>
<p dir="auto">obj1= new myobject();<br />
obj2= new myobject();</p>
<pre><code>    QThread *threadobj1 = new QThread;
</code></pre>
<p dir="auto">QThread *threadobj2 = new QThread;</p>
<pre><code>  obj1-&gt;moveToThread(threadobj1 );
</code></pre>
<p dir="auto">connect(threadobj1 , SIGNAL(started()), obj1, SLOT(doWork()));</p>
<p dir="auto">obj2-&gt;moveToThread(threadobj2 );<br />
connect(threadobj2 , SIGNAL(started()), obj2, SLOT(doWork()));</p>
<pre><code>  QPushButton *ChgStateobj1 = new QPushButton("ChgStateobj1",this);
</code></pre>
<p dir="auto">//connect(ChgStateobj1 , SIGNAL(clicked()), obj1, SLOT(ChangeState()));<br />
connect(ChgStateobj1 , SIGNAL(clicked()), this, SLOT(sendMessage()));</p>
<p dir="auto">QPushButton *ChgStateobj2 = new QPushButton("ChgStateobj2",this);<br />
//connect(ChgStateobj2 , SIGNAL(clicked()), obj2, SLOT(ChangeState()));<br />
connect(ChgStateobj2 , SIGNAL(clicked()), this, SLOT(sendMessage()));</p>
<p dir="auto">QHBoxLayout *layout = new QHBoxLayout;</p>
<pre><code>layout-&gt;addWidget(ChgStateobj1 );
layout-&gt;addWidget(ChgStateobj2);

this-&gt;setLayout(layout);
</code></pre>
<p dir="auto">threadobj1 -&gt;start();<br />
threadobj2-&gt;start();<br />
}</p>
<p dir="auto">void mainwidget::sendMessage()<br />
{<br />
qDebug() &lt;&lt;"mainwidgets sendMessage "&lt;&lt;"Thread ID:  "&lt;&lt; QThread::currentThreadId()&lt;&lt; endl;<br />
QMessageBox ::StandardButton reply;<br />
reply = QMessageBox::question(this, "Message", "Are you sure to change the state?", QMessageBox::Yes|QMessageBox::No);<br />
///................<br />
}<br />
@</p>
<p dir="auto">main.cpp<br />
@int main(int argc, char *argv[]) {</p>
<pre><code>QApplication app(argc, argv);
mainwidget *mywindow=new mainwidget();
mywindow-&gt;show;
</code></pre>
<p dir="auto">mywindow-&gt;exec();<br />
@</p>
<p dir="auto">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.<br />
For Added security before calling the slots "ChangeState" I would implement a Dialog with the user via a QMessageBox... So the idea is:</p>
<ol>
<li>The user push the button ChgStateobj1<br />
2)A QMessageBox appear<br />
3)If the user clicks yes the slot ChangeState will be called and executed in the thread threadobj1.<br />
The same is for the button ChgStateobj2.<br />
So I don't know which is the correct way to implement QMessageBox, Any ideas?</li>
</ol>
<p dir="auto">Thanks</p>
]]></description><link>https://forum.qt.io/topic/37975/qmessagebox-and-thread-slot</link><generator>RSS for Node</generator><lastBuildDate>Sat, 09 May 2026 16:31:13 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/37975.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 18 Feb 2014 11:50:26 GMT</pubDate><ttl>60</ttl></channel></rss>