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. Setting and getting property on a widget...
Servers for Qt installer are currently down

Setting and getting property on a widget...

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 831 Views 2 Watching
  • 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 29 Nov 2021, 19:39 last edited by
    #1

    I'm not sure if the way I'm doing this is correct, I try to store an ID with the widget:

    QString strID(strGetAttribute(clsXMLnode::mscszAttrID));
    if ( strID.isEmpty() != true ) {
        mvarID = strID;
        mpobjWidget->setProperty(clsXMLnode::mscszAttrID, mvarID);
    }
    

    mvarID is a QVariant stored in the class, I'm only making it a class member as the second parameter is a reference and I'm not sure if it needs to have a life that extends the call?

    clsXMLnode::mscszAttrID is a const char[] with the content "id".

    When I try to retrieve the property:

    QVariant varID(pobjButton->property(clsXMLnode::mscszAttrID));
    if ( varID.isValid() != true ) {
        return;
    }
    QString strID(varID.toString());
    if ( strID.isEmpty() == true ) {
        return;
    }
    

    I can see in the debugger varID is not valid, what am I doing wrong?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JoeCFD
      wrote on 29 Nov 2021, 19:45 last edited by
      #2
      This post is deleted!
      S 1 Reply Last reply 29 Nov 2021, 19:47
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 Nov 2021, 19:45 last edited by
        #3

        Hi,

        There's no need for that QVariant member variable. The second parameter of the setProperty method is not just a reference, it's a const reference.

        You have to check that pobjButton and mpobjWidget point to the same object.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • J JoeCFD
          29 Nov 2021, 19:45

          This post is deleted!

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 29 Nov 2021, 19:47 last edited by
          #4

          @JoeCFD said in Setting and getting property on a widget...:

          the properties you can define are here.
          https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY
          if you define your own property, it may not work.
          Store an ID as object name if it is not used.

          Please read the setProperty documentation and the part about dynamic properties.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          S J 2 Replies Last reply 29 Nov 2021, 19:58
          1
          • S SGaist
            29 Nov 2021, 19:47

            @JoeCFD said in Setting and getting property on a widget...:

            the properties you can define are here.
            https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY
            if you define your own property, it may not work.
            Store an ID as object name if it is not used.

            Please read the setProperty documentation and the part about dynamic properties.

            S Offline
            S Offline
            SPlatten
            wrote on 29 Nov 2021, 19:58 last edited by SPlatten
            #5

            @SGaist , I've modified the setProperty to:

            QString strID(strGetAttribute(clsXMLnode::mscszAttrID));
            if ( strID.isEmpty() != true ) {
                const QVariant cvarID(strID);
                bool blnRC(mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID));
            qdbg() << blnRC;
            }
            

            I've single stepped into setProperty and all is ok until this line is stepped over:

            const int idx = d->extraData->propertyNames.indexOf(name);
            

            On entry to the function name contains "id" and value contains "win2", however after stepping over the above, name changes to 0x0 and value changes to <null reference>.

            I can see that blnRC after calling setProperty is false, so I will take a look at the documentation.

            Kind Regards,
            Sy

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 29 Nov 2021, 20:04 last edited by
              #6

              What do you get if you run:

              QString strID(strGetAttribute(clsXMLnode::mscszAttrID));
              if ( strID.isEmpty() != true ) {
                  const QVariant cvarID(strID);
                  mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID);
              }
              qDebug() << mpobjWidget->property(clsxmlnode::mscszAttrID);
              

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply 29 Nov 2021, 20:18
              1
              • S SGaist
                29 Nov 2021, 20:04

                What do you get if you run:

                QString strID(strGetAttribute(clsXMLnode::mscszAttrID));
                if ( strID.isEmpty() != true ) {
                    const QVariant cvarID(strID);
                    mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID);
                }
                qDebug() << mpobjWidget->property(clsxmlnode::mscszAttrID);
                
                S Offline
                S Offline
                SPlatten
                wrote on 29 Nov 2021, 20:18 last edited by
                #7

                @SGaist said in Setting and getting property on a widget...:

                qDebug() << mpobjWidget->property(clsxmlnode::mscszAttrID);

                I modified the code:

                    QString strID(strGetAttribute(clsXMLnode::mscszAttrID));
                    if ( strID.isEmpty() != true ) {
                        const QVariant cvarID(strID);
                        bool blnRC(mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID));
                qdbg() << "HACK: " << blnRC << ", " << mpobjWidget->property(clsXMLnode::mscszAttrID);
                    }
                

                The output is:

                S000000000003E000000000068T20:16:26.368DL00003842F../clsMainWnd.cpp[void clsXMLnode::setWidget]
                HACK: false, QVariant(QString, win2)
                

                Not sure why it returns false when the value returned by property is completely correct, need to take a closer look now as why it doesn't work when I use the same code to get the property elsewhere.

                Kind Regards,
                Sy

                J 1 Reply Last reply 29 Nov 2021, 20:21
                0
                • S SPlatten
                  29 Nov 2021, 20:18

                  @SGaist said in Setting and getting property on a widget...:

                  qDebug() << mpobjWidget->property(clsxmlnode::mscszAttrID);

                  I modified the code:

                      QString strID(strGetAttribute(clsXMLnode::mscszAttrID));
                      if ( strID.isEmpty() != true ) {
                          const QVariant cvarID(strID);
                          bool blnRC(mpobjWidget->setProperty(clsXMLnode::mscszAttrID, cvarID));
                  qdbg() << "HACK: " << blnRC << ", " << mpobjWidget->property(clsXMLnode::mscszAttrID);
                      }
                  

                  The output is:

                  S000000000003E000000000068T20:16:26.368DL00003842F../clsMainWnd.cpp[void clsXMLnode::setWidget]
                  HACK: false, QVariant(QString, win2)
                  

                  Not sure why it returns false when the value returned by property is completely correct, need to take a closer look now as why it doesn't work when I use the same code to get the property elsewhere.

                  J Offline
                  J Offline
                  JoeCFD
                  wrote on 29 Nov 2021, 20:21 last edited by JoeCFD
                  #8

                  @SPlatten If the property is defined in the class using Q_PROPERTY then true is returned on success and false otherwise. If the property is not defined using Q_PROPERTY, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.
                  https://doc.qt.io/qt-5/qobject.html#setProperty
                  your property is yours and not defined in https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY.
                  It is ok to have false.

                  S 1 Reply Last reply 29 Nov 2021, 20:22
                  2
                  • J JoeCFD
                    29 Nov 2021, 20:21

                    @SPlatten If the property is defined in the class using Q_PROPERTY then true is returned on success and false otherwise. If the property is not defined using Q_PROPERTY, and therefore not listed in the meta-object, it is added as a dynamic property and false is returned.
                    https://doc.qt.io/qt-5/qobject.html#setProperty
                    your property is yours and not defined in https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY.
                    It is ok to have false.

                    S Offline
                    S Offline
                    SPlatten
                    wrote on 29 Nov 2021, 20:22 last edited by
                    #9

                    @JoeCFD , thank you.

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • S SGaist
                      29 Nov 2021, 19:47

                      @JoeCFD said in Setting and getting property on a widget...:

                      the properties you can define are here.
                      https://doc.qt.io/qt-5/qobject.html#Q_PROPERTY
                      if you define your own property, it may not work.
                      Store an ID as object name if it is not used.

                      Please read the setProperty documentation and the part about dynamic properties.

                      J Offline
                      J Offline
                      JoeCFD
                      wrote on 29 Nov 2021, 20:23 last edited by JoeCFD
                      #10

                      @SGaist I was wrong. Thanks.

                      1 Reply Last reply
                      0

                      1/10

                      29 Nov 2021, 19:39

                      • Login

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