Help me, I do not understand it. My code works even I believe it shouldnt.
I can receive an event in a thread even though the eventloop of the thread has not been started with exec.
See the code appended. I used the example "calculatorform". When some special numbers are entered by user it sends a customevent
to my thread. The thread displays then a messagebox. The thread is running, this->ii increments and it costs full power of 1of 4 processor cores.
/***************** main.cpp ***************/
#include <QApplication>
#include <QEvent>
#include "calculatorform.h"
#include "mythread.h"
int main(int argc, char *argv[])
{
CalculatorForm calculator;
MyThread testit1;
MyThread testit2;
MyThread testit3;
MyThread testit4;
trhead = &testit1;
appptr = &app;
my_customevent
= (QEvent::Type) QEvent::registerEventType ( -1 );
testit1.start();
//testit2.start();
//testit3.start();
// testit4.start();
calculator.show();
return app.exec();
}
#include <QApplication>
#include <QEvent>
#include "calculatorform.h"
#include "mythread.h"
QApplication * appptr;
QObject * trhead;
QEvent::Type my_customevent;
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
CalculatorForm calculator;
MyThread testit1;
MyThread testit2;
MyThread testit3;
MyThread testit4;
trhead = &testit1;
appptr = &app;
my_customevent = (QEvent::Type) QEvent::registerEventType ( -1 );
testit1.start();
//testit2.start();
//testit3.start();
// testit4.start();
calculator.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
/***************** my_thread.cpp ***************/
extern QEvent::Type my_customevent;
void MyThread::run()
{
//exec();
while(1) this->ii++;
}
int myMessageBox(long value);
void MyThread
::customEvent ( QEvent * event
) {
if (event->type() == my_customevent)
{
myMessageBox(this->ii);
}
else
{
}
}
int myMessageBox(long value)
{
// result == "pi = 3.14"
msgBox.setText("This is a test message box");
msgBox.setInformativeText(result);
int ret = msgBox.exec();
return ret;
}
extern QEvent::Type my_customevent;
void MyThread::run()
{
//exec();
while(1) this->ii++;
}
int myMessageBox(long value);
void MyThread::customEvent ( QEvent * event )
{
if (event->type() == my_customevent)
{
myMessageBox(this->ii);
}
else
{
QThread::customEvent(event);
}
}
int myMessageBox(long value)
{
QMessageBox msgBox;
QString result;
QTextStream(&result) << "Resultcode = " << value;
// result == "pi = 3.14"
msgBox.setText("This is a test message box");
msgBox.setInformativeText(result);
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
return ret;
}
To copy to clipboard, switch view to plain text mode
Bookmarks