PDA

View Full Version : Give feedback to a QDialog from another thread



JohnnyRep
7th March 2010, 10:23
Hi!

I am new to Qt developement so this might be a very easy question for the experts out there :)

A have serveral C++ Classes handling a socket connection with a remote device. These Classes are part of an older Framework we developed some time ago (so there is no inheritance from QObject etc.).
The application has a few threads and one of them should "inform" the GUI about a certain event.

There is a QT-Dialog Window waiting for this information. So I guess, as far as I understood, I could generate a signal to solved this?!
As long as my class is not based on a QObject (and I don't want to change that) I could need some kind of model-view-controller structure. So that the signal can be generated by the controller?!

May be there is a much simpler way to achieve my goal of sending a signal to the GUI by my thread?!
Would be perfect, I somebody could help me out :)
Thank you!

J.R.

nish
7th March 2010, 11:01
what about just creating a simple QObject class which works as a signal thrower.. make an your thread call some public function of the class which in turn emits the relevant signal to gui

JohnnyRep
7th March 2010, 11:27
Hey!
Thx for your answer.
That would be in a way a smaller version of my idea of using a model-view-controller pattern. I just wondered whether Qt offers a direct way to receive signals from a "foreign" non-Qt class!
Thanks again!

JohnnyRep
8th March 2010, 11:14
Now I got stuck by using your way of solving the problem:

3 Classes (A non-Qt, a small QObject class and a QDialog)... Idea is that the non-Qt class can report something to the dialog by using signals.

This is the small QObject class which should create a signal and call a slot in the dialog class.

class ControllerDialog : public QObject {

public:
ControllerDialog(DialogRobot *dlgRobot);

public slots:
void sendSocketReceived() {
emit socketReceived();
}
signals:
void socketReceived(){};

private:
DialogRobot *dlg;

};

Here is the snippet of the connection, placed (quick&dirty) within the ControllerDialog Constructor:

QObject::connect(this, SIGNAL(socketReceived()),
dlgRobot, SLOT(robotSocketComplete()));

And this is the part of the QDialog:

public slots:
void on_btnStart_clicked();
void on_btnFinished_clicked();
void robotSocketComplete() { ... }

Problem is that connect() reports

Object::connect: No such signal QObject::socketReceived() in src/draw/Qt/controller/controller_dialog.cpp:12
Object::connect: (receiver name: 'DialogRobotClass')

I used the search function of the forum and googled now for quite a while... And I found several HowTo's etc., but I don't see any bigger difference between my code and the samples :(

I used connect() successfully for connecting Button-Events etc., but in this case I don't see the problem :)

Lykurg
8th March 2010, 12:33
Use the Q_OBJECT macro!

JohnnyRep
8th March 2010, 19:12
Small macro -> big difference ;)

Thanks for your quick help guys, it really helped me a lot!