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

C++ syntax for accessing object "properties"



  • After RTFM I am more confused then ever.
    I am assigning

    widget->setWindowTitle("Sub Window");
    

    and adding "widget" to mdiArea subwindow.
    It works fine when used directly, not via subwindow.

    In this case "window title" is identified as "properties".

    My question is - how do I access these "properties" in C++ code ?
    "getWindowTitle" does not directly exist.

    One of the suggestion is to make "properties" public.
    If so how do I do it in QWidget ?

    (It looks as one of the reasons for using MDIChild instead of bypassing it )

       QWidget *widget = new QWidget(mdiArea);
    
        // build widget in code
        QGridLayout *gridLayout = new QGridLayout(widget);
        widget->setLayout(gridLayout);
        QLabel *label = new QLabel("Hello, I am sub window!!!", widget);
        gridLayout->addWidget(label);
        // add another test  label
        QLabel *TEST_LABEL  = new QLabel("NEW TEST LABEL Hello, I am sub window!!!", widget);
        gridLayout->addWidget(TEST_LABEL);
        // need here to add to subwindow
        widget->setWindowTitle("Sub Window");
        widget->show();
        // idetify new subwindow is without  file
        // add to subwindows mdiArea
        mdiArea->addSubWindow(widget);
    

    0ed2390f-7cc5-44cb-b900-b11fc9d27d93-image.png


  • Lifetime Qt Champion

    Hi,

    See QWidget::windowTitle property.

    Qt's style guide does not prefix the getter with get.
    Basically you setProperty and ask for property. It makes the code easier to read. In the same idea, when your property is a Boolean you will use for example setEnabled and isEnabled. Again easier to read and reason about.

    The list of properties for a class is listed at the top of the class documentation.



  • Thanks , just figured it out.

    Of course being me I have to add

    makes little sense to have "set" and no "get"

    SOLVED ?


  • Lifetime Qt Champion

    What do you find more readable and understandable ?

    if (myObject.isEnabled()) {
        doStuff();
    }
    

    Or

    if (myObject.getEnabled()) {
        doStuff();
    }
    


  • @SGaist said in C++ syntax for accessing object "properties":

    What do you find more readable and understandable ?

    if (myObject.isEnabled()) {
        doStuff();
    }
    

    Or

    if (myObject.getEnabled()) {
        doStuff();
    }
    

    OK, I'll bite.
    You probably figured out that

    1. I am not an English speaking native , hence my interpretation is often way in the left field.
    2. I wrote my first for pay program in 1973
    3. I believe in digital concept - yes / no - NOT maybe.

    "is enabled (?)" means just that - asking ( question) about the object current state. I believe most people do not make the "implied question" from the English expression.

    "GET enabled" means - irregardless of current state ( did not ask, do not care what it is ) - change it to "enabled"

    Call me pedantic , but I see the difference.
    Probably because my education and background was in hardware first. Software came later in my professional life.

    Now it is my form of self punishment... and BOTH hardware and software.


  • Lifetime Qt Champion

    @AnneRanch said in C++ syntax for accessing object "properties":

    I am not an English speaking native , hence my interpretation is often way in the left field.

    Neither am I.

    I also have a background in hardware even if younger than you.

    That said, I did not imply that all uses of getXXX was bad. Typically "getting" a value from a hardware device falls in that case however I would use something clearer than getEnabled.

    And usually, getter related to hardware uses the return value to indicate whether the call was successful and use an output parameter to actually return the value of interest.

    While programs are not literature, making them easy to read and reason about is one big step away from maintenance hell.



  • @AnneRanch said in C++ syntax for accessing object "properties":

    "GET enabled" means - irregardless of current state ( did not ask, do not care what it is ) - change it to "enabled"

    That is not what the English word GET means, neither in common parlance nor in programming. What you describe is precisely what SET means, in both environments.


Log in to reply