Please nominate your Qt Champions for 2021!

const QRect availableGeometry(...) return value is const?

  • This is a public function for QDesktopWidget.
    Qt Novice, using Qt 4.
    I don't understand how a function can return a value that must be declared const. The who purpose of a return value is to put it somewhere. That means to write the value somewhere. Please enlighten me.

    To be complete from the QT assistant the declaration is:

    const QRect availableGeometry( int screen = -1) const
    const QRect availableGeometry( const QWidget * widget) const;

    I can get it to work with

    const QRect size = desk_top_widget.availableGeometry( -1 );

    The return value is not a pointer which means that the return value is passed by "value" which mean that, as I see this, declaring it const has no purpose or advantage.

  • Qt Champions 2017

    Yes, it is const value. It means that you should not change as the return value is final. This method is obsolete as well. You can use QScreen class

  • This is a bit of an anachronism in C++ (not specific to Qt). The purpose of the const qualifier was intended to ensure that the caller will not modify the returned value. (It's more meaningful when the return value is something like a pointer.) It's value is a matter of debate, particularly since C++11 gave us rvalue references.

    A reasonable discussion of this can be found here.

    In the particular example you cite, it means you can't modify the QRect that is returned by the availableGeometry() call. You'll probably get a compiler warning if you attempt to.

  • That also means that a QRect cannot be declared in dot h file and the value set by the function. And that means it cannot be referenced outside the place the gets the value, so if I want the screen attributes it must either be copied one part at a time to a saved location or a new value must be captured just before it is used.
    But that method is obsolete and the new method is probably not in Qt4, so the question might be moot.

    Qt4 says QScreen is only available for embedded Linux.
    I will make do with what I have.
    Thanks for your time.

Log in to reply