Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Qt Academy Launch in California!

    Solved What type of value does function connect return?

    General and Desktop
    3
    6
    1688
    Loading More Posts
    • 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.
    • Q
      QTLeearn last edited by

      Hi, I have a question about connect, according to documents connect returns the type QMetaObject::Connection, but in the real case we can assign its returning value to a bool variable. What I am doing is the unit test where I can force function connect to return any valid values I want( unit test another story).
      My essential question is different from the title: If I want the function connect to return true, should I manipulate QMetaObject::Connection? For example , to force QMetaObject::Connection::operator bool() const to always return true? I didn't write the title in this way because that might be too wired for Qt development...

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        You can wrap it in your own thing that happily converts to and from a connection and make the bool operator always return true:

        class MyConnection {
        public:
            MyConnection(const QMetaObject::Connection& c) : conn(c) {}
            MyConnection& operator=(QMetaObject::Connection& c) { conn = c; return *this; }
            operator QMetaObject::Connection() const { return conn; }
            operator bool() const { return true; }
        private:
            QMetaObject::Connection conn;
        };
        

        If it's just for testing then you could typedef it, something like:

        #ifdef TESTING
        using ConnType = MyConnection;
        #else
        using ConnType = QMetaObject::Connection;
        #endif
        

        and use it like this:

        ConnType conn = connect(...);
        
        Q_ASSERT(conn); // always true in testing, can fail otherwise
        

        but I kinda agree with @VRonin that it makes little sense.

        1 Reply Last reply Reply Quote 2
        • V
          VRonin last edited by

          QMetaObject::Connection already has and operator bool(): https://doc.qt.io/qt-5/qmetaobject-connection.html#operator-bool

          What is your exact requirement here?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Q 2 Replies Last reply Reply Quote 2
          • Q
            QTLeearn @VRonin last edited by QTLeearn

            @VRonin Ok, sorry I didn't declare my question clearly. I will re-declare it: Suppose I can manipulate any return values in any Qt library functions regardless of input parameters, by manipulating which I can let the function connect always return true? For example, to force

            QMetaObject::Connection::operator bool() const
            

            to return true then

                static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal,
                                    const QObject *receiver, const QMetaMethod &method,
                                    Qt::ConnectionType type = Qt::AutoConnection);
            

            will always return true regardless of any input parameters

            1 Reply Last reply Reply Quote 0
            • Q
              QTLeearn @VRonin last edited by

              @VRonin I know my question is kinda too wired....since it's opposite to Qt development, in Qt development in order to let the connect return true you should ensure the input parameters of connect. However my question is by manipulating which I can let the connect always return true? Sorry about this question

              1 Reply Last reply Reply Quote 0
              • V
                VRonin last edited by

                You can't manipulate the QMetaObject::Connection directly but if you want to override its bool return value you can use:

                const bool alwaysTrue = QObject::connect(sender,signal,receiver,slot) ? true:true;

                I still can't see a single case at all where what you want to achieve would be useful though.

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply Reply Quote 3
                • Chris Kawa
                  Chris Kawa Moderators last edited by

                  You can wrap it in your own thing that happily converts to and from a connection and make the bool operator always return true:

                  class MyConnection {
                  public:
                      MyConnection(const QMetaObject::Connection& c) : conn(c) {}
                      MyConnection& operator=(QMetaObject::Connection& c) { conn = c; return *this; }
                      operator QMetaObject::Connection() const { return conn; }
                      operator bool() const { return true; }
                  private:
                      QMetaObject::Connection conn;
                  };
                  

                  If it's just for testing then you could typedef it, something like:

                  #ifdef TESTING
                  using ConnType = MyConnection;
                  #else
                  using ConnType = QMetaObject::Connection;
                  #endif
                  

                  and use it like this:

                  ConnType conn = connect(...);
                  
                  Q_ASSERT(conn); // always true in testing, can fail otherwise
                  

                  but I kinda agree with @VRonin that it makes little sense.

                  1 Reply Last reply Reply Quote 2
                  • First post
                    Last post