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...
Forum Updated to NodeBB v4.3 + New Features

Setting and getting property on a widget...

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 866 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on 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
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2
      This post is deleted!
      SGaistS 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        • JoeCFDJ JoeCFD

          This post is deleted!

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 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

          SPlattenS JoeCFDJ 2 Replies Last reply
          1
          • SGaistS SGaist

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

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on 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
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

              SPlattenS 1 Reply Last reply
              1
              • SGaistS SGaist

                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);
                
                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on 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

                JoeCFDJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

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

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on 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.

                  SPlattenS 1 Reply Last reply
                  2
                  • JoeCFDJ JoeCFD

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

                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #9

                    @JoeCFD , thank you.

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0
                    • SGaistS SGaist

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

                      JoeCFDJ Offline
                      JoeCFDJ Offline
                      JoeCFD
                      wrote on last edited by JoeCFD
                      #10

                      @SGaist I was wrong. Thanks.

                      1 Reply Last reply
                      0

                      • Login

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