This is somehow what you were talking about? Or am I completely off the way?
#include <QtCore/QCoreApplication>
#include <QEventLoop>
#include <QWaitCondition>
#include <QMutex>
#include <QThread>
{
public:
MyEventLoop() {}
{
qDebug("Processing event.");
return true;
}
void run() {exec();}
};
int main(int argc, char* argv[])
{
thread.start();
for (int i = 0; i < 10; i++) {
cond.wait(&mutex, qrand() % 4000);
qDebug("Sending event.");
}
return a.exec();
}
#include <QtCore/QCoreApplication>
#include <QEventLoop>
#include <QWaitCondition>
#include <QMutex>
#include <QThread>
class MyEventLoop : public QThread
{
public:
MyEventLoop() {}
bool event(QEvent* event)
{
qDebug("Processing event.");
return true;
}
void run() {exec();}
};
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
QThread thread;
thread.start();
for (int i = 0; i < 10; i++) {
QWaitCondition cond;
QMutex mutex;
cond.wait(&mutex, qrand() % 4000);
qDebug("Sending event.");
QCoreApplication::postEvent(&thread, new QEvent(QEvent::Hide));
}
return a.exec();
}
To copy to clipboard, switch view to plain text mode
I would like to use this even following the command design pattern, so sending requests in the form of objects to better describe the request. Sill this is a good way to go? I suppose I have to subclass QEvent to add whatever else I need right?
Speaking about memory management, the documentation obviously tell me to allocate the object in the heap, but does it free memory automatically after the event(...) method has returned true or am I supposed to do it there myself?
The approach I reported in the other post was correct as well?
Thanks!
Bookmarks