Results 1 to 9 of 9

Thread: Qt5 result of connect() call

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qt5 result of connect() call

    In the good ole days of Qt 4 many places I worked had a policy of checking connect() results something like this:

    bool rslt=true;

    rslt &= connect( blah...)
    rslt &= connect( blah...)
    rslt &= connect( blah...)
    rslt &= connect( blah...)
    rslt &= connect( blah...)

    if (!rslt) do someting

    I'm now using qt5.4 from the _run file on Ubuntu 12.4.5 32-bit. According to the Assistant connect() is supposed to return QMetaObject::Connection. Ok, fine. According to the Assistant documentation one #include <Connection> should obtain a class definition which has a bool() method.

    Problem 1: There is no Connection header file, but the IDE does seem to recognize the class.
    Problem 2: There is no bool() method shown in the list of options. There is an operator RestrictedBool and something else called RestrictedBool

    I _assume_ the doc hasn't caught up to the coding changes. Just want to know how I'm supposed to actually check it now. Hopefully the doc only has the header file wrong and this can quickly be resolved.

    Thanks,

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt5 result of connect() call

    Connection is a class defined within QMetaObject. You use the QMetaObject header to include it, but this is already done for you if you include the header for any QObject-derived class.

    QMetaObject::Connection has a bool() cast operator (QMetaObject::Connection::bool()) which means your current code will continue to work just fine because C++ will cast the Connection object returned by connect() to a bool (which will be set to the status of the connect() call).

    You can verify this by using the debugger to examnine the content of your "rslt" variable after the connect call. I presume you initialize it to "true" before using it in "&=", right?

  3. #3
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt5 result of connect() call

    Quote Originally Posted by d_stranz View Post
    Connection is a class defined within QMetaObject. You use the QMetaObject header to include it, but this is already done for you if you include the header for any QObject-derived class.

    QMetaObject::Connection has a bool() cast operator (QMetaObject::Connection::bool()) which means your current code will continue to work just fine because C++ will cast the Connection object returned by connect() to a bool (which will be set to the status of the connect() call).

    You can verify this by using the debugger to examnine the content of your "rslt" variable after the connect call. I presume you initialize it to "true" before using it in "&=", right?
    Yes, I init the bool.

    I would verify it if it would compile. This method, which always worked in Qt4, seems busted now.


    bool rslt=true;

    rslt &= connect( &m_appProcess, SIGNAL(aboutToClose()), this, SLOT(appClosed()));
    if (!rslt)
    qDebug() << "unable to connect all signals and slots";


    /home/developer/MyWatchdog/mywrapper.cpp:26: error: no match for 'operator&=' in 'rslt &= QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)(qFlagLocation(((const char*)"2finished(int,QProcess::ExitStatus)\000../MyWatchdog/mywrapper.cpp:26")), ((const QObject*)(&((MyWrapper*)this)->MyWrapper::<anonymous>)), qFlagLocation(((const char*)"1appShutDown(int,QProcess::ExitStatus)\000. ./MyWatchdog/mywrapper.cpp:26")), (Qt::ConnectionType)0u)'

    Changing rslt &= to just rslt = makes it compile clean. Of course I have many other connects so that isn't a solution. m_appProcess is just a QProcess object declared as a member variable.

    If I do an __ugly__ hack, declare bool yn then assign the result of the connect directly to it, then rslt &= yn; things work, but it makes for some mighty ugly code.

    Btw, I had to change names of files and variables in that error message so please don't pick at it too closely.
    Last edited by RolandHughes; 10th June 2015 at 16:59. Reason: add information

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt5 result of connect() call

    So do an explicit cast: rslt &= bool( connect( ... ) ); The compiler is complaining because bool operator&=() const is not defined for QMetaObject::Connection.

  5. The following user says thank you to d_stranz for this useful post:

    RolandHughes (10th June 2015)

  6. #5
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt5 result of connect() call

    Quote Originally Posted by d_stranz View Post
    So do an explicit cast: rslt &= bool( connect( ... ) ); The compiler is complaining because bool operator&=() const is not defined for QMetaObject::Connection.
    Thanks, but not allowed. It's a coding standards thing. Not my rules, I just charge by the hour.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt5 result of connect() call

    Quote Originally Posted by RolandHughes View Post
    Thanks, but not allowed. It's a coding standards thing. Not my rules, I just charge by the hour.
    Then either define that operator yourself or rewrite the code to use X = X && Y syntax. Or better yet rewrite it to something more useful, e.g. series of if(!connect(...)) { ...; return; } as it probably makes little sense to make further connections if earlier ones fail.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt5 result of connect() call

    @wysota,

    I originally had a series of if statements, but that is not the standard here. I do what the client pays for. Thanks for your input though.

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qt5 result of connect() call

    Seems like the client needs some re-education. But you're not being paid for that, you're being paid to write to the "standard". Guarantees that the same mistakes will keep being perpetuated so that future developers will think it's OK. Sure glad I work for myself and by own rules (which, being malleable, can be changed as needed to keep them up to date).

  10. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt5 result of connect() call

    Because QMetaObject::Connection don't have defined operator & (bitwise AND).
    rslt &= another_rslt is equivalent to rslt = rslt & another_rslt. This is not the same as rslt = rslt && another_rslt.

Similar Threads

  1. Replies: 1
    Last Post: 27th June 2014, 16:29
  2. Replies: 5
    Last Post: 27th January 2014, 22:22
  3. Replies: 2
    Last Post: 9th May 2011, 10:38
  4. Replies: 0
    Last Post: 8th March 2011, 22:08
  5. Result of DBUS call
    By conexion2000 in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2009, 08:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.