AngryDuck
AngryDuck

Reputation: 4607

Signals and slots QT

I have asked a number of different questions now all regarding one main issue in my program and still not solved it at all, I'm using threading to keep my UI from locking up, but basically it still does because apparently you can't do UI stuff in threads.

So I've been told to use custom signals and slots (not that any examples were given).

So from the documentation I've read I came up with this code:

.h

signals:

void paint_signal(double x, double y);

.cpp

  connect(this,SIGNAL(paint_signal(double x, double y)), this, SLOT(PaintSomething(x,y)));

the Paintsomething function is within the same class as all of this....

thread:

*future2 = QtConcurrent::run(this, &GUI::paintAll);

paint all emits the paint_signal and passes 2 doubles

emit paint_signal(x, y);

but I get this error which I just don't understand at all

 connect: No such signal GUI::paint_signal(double x, double y)

Upvotes: 1

Views: 339

Answers (2)

Barış Akkurt
Barış Akkurt

Reputation: 2256

Floris Velleman's answer is ok, however by using the new signal slot syntax, you can catch errors during compile time and get rid of the redundant paranthesis.

Old Syntax:

connect(this,
        SIGNAL(paint_signal(double, double)), 
        this, 
        SLOT(PaintSomething(double,double)));

New Syntax:

connect(this,
        &SenderClass::paint_signal, 
        this, 
        &ReceiverClass::PaintSomething);

Upvotes: 1

Floris Velleman
Floris Velleman

Reputation: 4888

connect(this,
        SIGNAL(paint_signal(double, double)), 
        this, 
        SLOT(PaintSomething(x,y)));

Remove parameter names and it should work. If this one doesn't work this one will:

    connect(this,
        SIGNAL(paint_signal(double, double)), 
        this, 
        SLOT(PaintSomething(double,double)));

Let me know if this works out for you :)

Update

The idea is that you cannot use the UI in a thread, instead you emit signals from the thread to the UI. Because this answer probably gets you back to the beginning (and possibly a new question) here is a working example of how to emit signals from threads:

QT Signals to UI in a thread

Upvotes: 4

Related Questions