Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot convert Qt4 Signal and slot to Qt5 Syntax
Forum Updated to NodeBB v4.3 + New Features

Cannot convert Qt4 Signal and slot to Qt5 Syntax

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    summerfang
    wrote on last edited by
    #1

    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. )

    QObject::connect(CameraBus::getInstance().getMainFrameObserverObj(),
                 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:

    QObject::connect(CameraBus::getInstance().getMainFrameObserverObj(),
     &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]
    QObject::connect(CameraBus::getInstance().getMainFrameObserverObj(),
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~

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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What is the signature of your getter function ?

      Do you have overloads for your slot ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        summerfang
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply
          2
          • SGaistS SGaist

            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.

            S Offline
            S Offline
            summerfang
            wrote on last edited by
            #5

            @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!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved