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. What type of value does function connect return?

What type of value does function connect return?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.2k 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.
  • Q Offline
    Q Offline
    QTLeearn
    wrote on last edited by
    #1

    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
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #6

      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
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        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
        2
        • VRoninV VRonin

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

          What is your exact requirement here?

          Q Offline
          Q Offline
          QTLeearn
          wrote on last edited by QTLeearn
          #3

          @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
          0
          • VRoninV VRonin

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

            What is your exact requirement here?

            Q Offline
            Q Offline
            QTLeearn
            wrote on last edited by
            #4

            @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
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #5

              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
              3
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #6

                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
                2

                • Login

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