Cannot convert Qt4 Signal and slot to Qt5 Syntax

  • Hello! Ealier I have the following signal and slot connect in my project, with Qt4 Syntax. And it runs well for a long time. ( CameraBus::getInstance() is the instance of a singleton, and getMainFrameObserverObj() returns a shared pointer of its member who is a instance of MainCameraFrameObserver. )

                 SIGNAL(createdNewImg(cv::Mat)), this,SLOT(repaintImage(cv::Mat)));

    Now I want to write it in the Qt5 Syntax, since my project is using more and more Qt5 functions and features, so I change the code to this:

     &MainCameraFrameObserver::createdNewImg,  this,&QmlImagePainter::repaintImage);

    However, I got the following two errors. I haven't understand everything of the Qt5 signal and slot, just changed the syntax but nothing else, anyone has idea about this?

    error: invalid conversion from ‘QObject*’ to ‘const Object* {aka const MainCameraFrameObserver*}’ [-fpermissive]

    error: no matching function for call to ‘QmlImagePainter::connect(QObject*, void (MainCameraFrameObserver::)(cv::Mat), QmlImagePainter, void (QmlImagePainter::*)(cv::Mat))’

  • Lifetime Qt Champion


    What is the signature of your getter function ?

    Do you have overloads for your slot ?

  • Hi thanks for the reply.

    The getMainFrameObserverObj() function is of type QObject* , it's a shared pointer of the sender. The slot has no overloads.

  • Lifetime Qt Champion

    In that case shouldn't your rather have something like:

    MainCameraFrameObserver *observer = qobject_cast<MainCameraFrameObserver *>CameraBus::getInstance().getMainFrameObserverObj();
    QObject::connect(observer, &MainCameraFrameObserver::createdNewImg, this, &QmlImagePainter::repaintImage);

    And only then the connection statement ?

    On a side note, it seems a bit strange that QmlImagePainter needs to know about your singleton and the MainCameraFrameObserver. This makes a tight coupling that will likely bring problems of maintenance in the long run.

  • @SGaist Thanks a lot! I see, compared with the earlier syntax with SIGNAL() and SLOT(), the sender in the connect cannot simply be a QObject* anymore. I'll fix it.

    Also thanks for your note, my idea is that, I have a singleton named CameraBus, it emits images continously. And different other classes will be notified and then run their different slots(image displaying, processing... etc). So I connect the singleton's image signal with different slots in different classes.

    I'm not experienced in large projects. Could you please give me a few hints about how to do it correctly? Thank you very much!

  • Lifetime Qt Champion

    The problem with your approach is that the classes that might be interested with the update have to know about your singleton, it should rather be done on a higher level e.g. the class that will manage the lifetime of QmlImagePainter.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.