Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What type of value does function connect return?



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


  • Moderators

    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.



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

    What is your exact requirement here?



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



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



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


  • Moderators

    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.


Log in to reply