woboq / verdigris

Qt without moc: set of macros to use Qt without needing moc
https://woboq.com/blog/verdigris-qt-without-moc.html
GNU Lesser General Public License v3.0
638 stars 58 forks source link

use W_SIGNAL & in class implemention #88

Closed Maziar123 closed 2 years ago

Maziar123 commented 2 years ago

Hi when i use in class implementation such as below sample how use W_SIGNAL for do_message ?

class foo { ... ... void a(...){ //some imp. }

void do_message() { //some imp... }

};

jcelerier commented 2 years ago

I'd recommend starting by checking the tutorial, it explains the library thoroughly: https://github.com/woboq/verdigris/blob/master/tutorial/tutorial.cpp

Maziar123 commented 2 years ago

Hi

I Read it have do in W_SLOT But same way in W_SIGNAL NOT work :

void do_message(const QString& name) {
    qDebug("hello %s", qPrintable(name)); }
W_SIGNAL(do_message)

error: expected unqualified-id before ‘{’ token 70 | W_SIGNAL(do_message) | ^~~~

jcelerier commented 2 years ago

There's a misunderstanding it seems: in Qt signals aren't supposed to have an user-given implementation.

See for instance the official Qt documentation: https://doc.qt.io/qt-5/signalsandslots.html ; notice how the signals aren't given any implementation (in "normal" Qt, it's moc which does that).

the code that will work is :

void do_message(const QString& name) 
W_SIGNAL(do_message)

If you want to call something in do_message then you have to connect the signal to a slot somewhere:

connect(my_obj, &Foo::do_message, 
        [] (const QString& name) { qDebug("hello %s", qPrintable(name)); });
Maziar123 commented 2 years ago

HI

thanx for lambda solution

i think this good for add to tutorial.cpp

jcelerier commented 2 years ago

It is already done here :)

https://github.com/woboq/verdigris/blob/master/tutorial/tutorial.cpp#L71