Try this:
Note that I didn't test this if it compiles.
myobject.h
#include <QObject>
{
Q_OBJECT
public:
public slots:
void slotInMyObject();
};
#include <QObject>
class MyObject: public QObject
{
Q_OBJECT
public:
MyObject(QObject *parent = 0);
public slots:
void slotInMyObject();
};
To copy to clipboard, switch view to plain text mode
myobject.cpp
#include "myobject.h"
MyObject
::MyObject(QObject *parent
){
}
void MyObject::slotInMyObject()
{
//Do something
}
#include "myobject.h"
MyObject::MyObject(QObject *parent)
: QObject(parent)
{
}
void MyObject::slotInMyObject()
{
//Do something
}
To copy to clipboard, switch view to plain text mode
mywidget.h
#include <QWidget>
{
Q_OBJECT
public:
signals:
void signalFromMyWidget();
};
#include <QWidget>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
signals:
void signalFromMyWidget();
};
To copy to clipboard, switch view to plain text mode
mywidget.cpp
MyWidget
::MyWidget(QWidget *parent
){
}
...
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
}
...
To copy to clipboard, switch view to plain text mode
Emit a signal somewhere, but try to emit only signals when all objects are created in the main thread and all objects are moved to the correct thread. Otherwise you will pull your hair out.
I have no idea if QThread starts the eventloop automatically. If not, use the following thread class.
mythread.h
#include <QThread>
{
public:
void run();
};
#include <QThread>
class MyThread : public QThread
{
public:
void run();
};
To copy to clipboard, switch view to plain text mode
mythread.cpp
void MyThread::run()
{
exec();
}
void MyThread::run()
{
exec();
}
To copy to clipboard, switch view to plain text mode
Then put it all together:
main.cpp
#include "myobject.h"
#include "mywidget.h"
#include "mythread.h"
int main(...)
{
MyWidget *w = new MyWidget;
MyObject *o = new MyObject;
MyThread *t = new MyThread;
connect(w, SIGNAL(signalFromMyWidget()), o, SLOT(slotInMyObject()));
o->moveToThread(t);
t->start();
w->show();
return a.exec();
}
#include "myobject.h"
#include "mywidget.h"
#include "mythread.h"
int main(...)
{
QApplication a(...);
MyWidget *w = new MyWidget;
MyObject *o = new MyObject;
MyThread *t = new MyThread;
connect(w, SIGNAL(signalFromMyWidget()), o, SLOT(slotInMyObject()));
o->moveToThread(t);
t->start();
w->show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
I might have forgotten something, but that's the basics.
Bookmarks