Search code examples
qtsignals-slots

Qt emit form class A to slot in class B


I'm trying to make a log window for my Gui app.

I have classes named sql and MyService

How do i build and emit signal from class sql to update MyService log window?

in main.cpp:

    MyService myService;
    sql mySql;
    QObject::connect(mySql, SIGNAL(updateMyLog(QString(msg))),myService, 
    SLOT(updateMyLog(QString(msg))));

EDIT:
I forgot Q_OBJECT in my sql class and the error dosnt show any more

error: C2665: 'QObject::connect' : 
none of the 3 overloads could convert all the argument types

EDIT2:
my slot for updateMyLog is:

public slots:
void updateMyLog(QString logmessage);

and i get different error:

QObject::connect: No such signal sql::updateMyLog(QString msg)

Solution

  • Does it work if you write it like this?

    QObject::connect(&mySql, SIGNAL(updateMyLog(QString)), &myService, 
    SLOT(updateMyLog(QString)));
    

    Please note that you should pass pointers as the first and third parameters. As myService is not a pointer, you should get the memory address where it is allocated (a pointer to it).