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. C++ function QObject::connect with paramter QObject and function pointer ;
QtWS25 Last Chance

C++ function QObject::connect with paramter QObject and function pointer ;

Scheduled Pinned Locked Moved Unsolved General and Desktop
qobjectc++
10 Posts 6 Posters 1.1k Views
  • 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.
  • A Offline
    A Offline
    Anas_Deshmukh
    wrote on last edited by Anas_Deshmukh
    #1

    i want to write ConnectMathod(...) in such way that it accept QObject* and receiver slot. and establish connection
    //pseudo code as below

    class A : public QObject {
    public :
    A();
    ~A();
    signals :
    void sigA(int);
    slots :
    void slotA(bool);
    }

    class B : public QObject {
    public :
    B();
    ~B();
    signals :
    void sigB(bool);
    slots :
    void slotB(int);
    }

    // class C : i want to write common mathod which expect receiver (QObject*) and slot

    class C : public QObject
    {
    public :
    C();
    ~C();
    signals :
    void signalC(bool);

    // i want help to write ConnectMathod
    QMetaObject::Connection ConnectMathod(QObject* receiverObject, functionPointer)
    {
    QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
    return connection;
    }
    }

    /////// main.cpp ///////
    main()
    {
    A objA = new A();
    B objB = new B();
    C objC = new C();
    QMetaObject::Connection connection = objC->ConnectMathod(objA, objA->slotA);
    connection = objC->ConnectMathod(objB, objB->slotB);
    }

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What's your actual question/error message and please use the code tags to make your code more readable.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3

        you code does not launch any qt event loop.

        A 1 Reply Last reply
        0
        • JoeCFDJ JoeCFD

          you code does not launch any qt event loop.

          A Offline
          A Offline
          Anas_Deshmukh
          wrote on last edited by
          #4

          @JoeCFD its pseudo code.
          i need help to write below function corretly, i am doin it wrong
          // i want help to write ConnectMathod
          QMetaObject::Connection ConnectMathod(QObject* receiverObject, functionPointer)
          {
          QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
          return connection;
          }

          Christian EhrlicherC C 2 Replies Last reply
          0
          • A Anas_Deshmukh

            @JoeCFD its pseudo code.
            i need help to write below function corretly, i am doin it wrong
            // i want help to write ConnectMathod
            QMetaObject::Connection ConnectMathod(QObject* receiverObject, functionPointer)
            {
            QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
            return connection;
            }

            Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Anas_Deshmukh Then take a look at the definition of QObject::connect() and you will see how to pass it. Hint: you have to make it a template function.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

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

              Hi,

              Take a look at the connect methods implementation.

              That said, why do you need that version in the first place ?

              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
              2
              • Christian EhrlicherC Christian Ehrlicher

                @Anas_Deshmukh Then take a look at the definition of QObject::connect() and you will see how to pass it. Hint: you have to make it a template function.

                A Offline
                A Offline
                Anas_Deshmukh
                wrote on last edited by
                #7

                @Christian-Ehrlicher yes, thats where i am facing trouble. any work around with lambda functions?

                1 Reply Last reply
                0
                • A Anas_Deshmukh

                  @JoeCFD its pseudo code.
                  i need help to write below function corretly, i am doin it wrong
                  // i want help to write ConnectMathod
                  QMetaObject::Connection ConnectMathod(QObject* receiverObject, functionPointer)
                  {
                  QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
                  return connection;
                  }

                  C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  Perhaps I am missing something here. What does writing this method provide you that is not already provided? This seems to be the equivalent of what you seek and does not require reinventing the wheel:

                  A *objA = new A();
                  B *objB = new B();
                  C *objC = new C();
                  QMetaObject::Connection connection1 = QObject::connect(objC, &C::signalC, objA, &A::slotA);
                  QMetaObject::Connection connection2 = QObject::connect(objC, &C::signalC, objB, &B::slotB);
                  
                  JonBJ 1 Reply Last reply
                  2
                  • C ChrisW67

                    Perhaps I am missing something here. What does writing this method provide you that is not already provided? This seems to be the equivalent of what you seek and does not require reinventing the wheel:

                    A *objA = new A();
                    B *objB = new B();
                    C *objC = new C();
                    QMetaObject::Connection connection1 = QObject::connect(objC, &C::signalC, objA, &A::slotA);
                    QMetaObject::Connection connection2 = QObject::connect(objC, &C::signalC, objB, &B::slotB);
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @ChrisW67
                    For unknown reason, I think the OP wants a "utility/helper" connect() method which has its own inbuilt signal-side parameters for connect() but accepts the slot-side as parameters. Suppose what he wants is:

                    QMetaObject::Connection C::ConnectMethodForSignalC(QObject* receiverObject, SomeGenericFuncType *functionPointer)
                    {
                        someLogMethod(receiverObject);
                        someLogmethod(functionPointer);
                        // potentially some more code
                        // so that this is all worth factoring into its own function
                        QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
                        return connection;
                    }
                    

                    and he wants to know what this method declaration needs to look like to do it.

                    As @Christian-Ehrlicher has noted, he will need to declare this as a template function.

                    [One thing to note is: the use of this for the signalling object appears to mean the connection is being made from the signalling class to some slot object. Which is usually not the way to do things: signallers should not know about slotters, usually connections are made from the slot object class, or something which knows about the slot object, not the other way round.

                    @Anas_Deshmukh: Your desire to write this helper method in the signalling class

                    i want to write common mathod which expect receiver (QObject*) and slot

                    is "questionable" or "inadvisable", in terms of common practice. I take the point that this is only a "generic" function, and only accepts information about the slot as parameters, but still....]

                    A 1 Reply Last reply
                    1
                    • JonBJ JonB

                      @ChrisW67
                      For unknown reason, I think the OP wants a "utility/helper" connect() method which has its own inbuilt signal-side parameters for connect() but accepts the slot-side as parameters. Suppose what he wants is:

                      QMetaObject::Connection C::ConnectMethodForSignalC(QObject* receiverObject, SomeGenericFuncType *functionPointer)
                      {
                          someLogMethod(receiverObject);
                          someLogmethod(functionPointer);
                          // potentially some more code
                          // so that this is all worth factoring into its own function
                          QMetaObject::Connection = QObject::connect(this, &C::signalC, receiverObject, functionPointer);
                          return connection;
                      }
                      

                      and he wants to know what this method declaration needs to look like to do it.

                      As @Christian-Ehrlicher has noted, he will need to declare this as a template function.

                      [One thing to note is: the use of this for the signalling object appears to mean the connection is being made from the signalling class to some slot object. Which is usually not the way to do things: signallers should not know about slotters, usually connections are made from the slot object class, or something which knows about the slot object, not the other way round.

                      @Anas_Deshmukh: Your desire to write this helper method in the signalling class

                      i want to write common mathod which expect receiver (QObject*) and slot

                      is "questionable" or "inadvisable", in terms of common practice. I take the point that this is only a "generic" function, and only accepts information about the slot as parameters, but still....]

                      A Offline
                      A Offline
                      Anas_Deshmukh
                      wrote on last edited by
                      #10

                      @JonB Hi, sorry for the late reply.
                      extacly, i already have signal parameter, only want to write generic mathod - which has its own inbuilt signal-side parameters for connect() but accepts the slot-side as parameters.

                      1 Reply Last reply
                      0

                      • Login

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