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. Problem with Q_PROPERTY - application crashes when I try to assign value.
Forum Updated to NodeBB v4.3 + New Features

Problem with Q_PROPERTY - application crashes when I try to assign value.

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.4k Views 3 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.
  • V Offline
    V Offline
    Vladislav Lisovenko
    wrote on last edited by
    #1

    Hi.
    I added property to my class:

    Q_PROPERTY(QString guid READ guid WRITE setGuid NOTIFY guidChanged USER true)
    
    public:
        QString guid() const;
        void setGuid(const QString &value);
    
    signals:
        void guidChanged();
    

    and definitions:

    QString MyClass::guid() const
    {
        return property("guid").toString();
    }
    
    void MyClass::setGuid(const QString &value)
    {
        setProperty("guid", QVariant(value));
        emit guidChanged();
    
    }
    

    Application crashes on this line:

    setProperty("guid", QVariant(value));
    

    And I don't understand what wrong?

    Where there's a will, there is a way.

    1 Reply Last reply
    0
    • Joel BodenmannJ Offline
      Joel BodenmannJ Offline
      Joel Bodenmann
      wrote on last edited by Joel Bodenmann
      #2

      I haven't worked with the property system that much so I am sorry if I am giving wrong information.
      As far as I can tell the problem is that you use the property system to access the property. You cannot use setProperty() and property() inside the functions that are accessed by the property system. You need a (private) member named guid of type QString in your class. Then you implement the getter and setter functions to access and modify that string. This will allow other classes/objects to access that property through the property system.

      Note that it's also possible to give the class member that holds/represents the property a different name. In that case you would use the MEMBER attribute of the Q_PROPERTY macro to inform the property system which member this property is about.

      I hope that helps.

      Industrial process automation software: https://simulton.com
      Embedded Graphics & GUI library: https://ugfx.io

      V 1 Reply Last reply
      1
      • Joel BodenmannJ Joel Bodenmann

        I haven't worked with the property system that much so I am sorry if I am giving wrong information.
        As far as I can tell the problem is that you use the property system to access the property. You cannot use setProperty() and property() inside the functions that are accessed by the property system. You need a (private) member named guid of type QString in your class. Then you implement the getter and setter functions to access and modify that string. This will allow other classes/objects to access that property through the property system.

        Note that it's also possible to give the class member that holds/represents the property a different name. In that case you would use the MEMBER attribute of the Q_PROPERTY macro to inform the property system which member this property is about.

        I hope that helps.

        V Offline
        V Offline
        Vladislav Lisovenko
        wrote on last edited by
        #3

        @Joel-Bodenmann It helps, thank you.

        Where there's a will, there is a way.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          To add to @Joel-Bodenmann what you wrote is an infinite loop. setProperty will call your setter function which will call setProperty etc. hence infinite loop.

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

          V 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            To add to @Joel-Bodenmann what you wrote is an infinite loop. setProperty will call your setter function which will call setProperty etc. hence infinite loop.

            V Offline
            V Offline
            Vladislav Lisovenko
            wrote on last edited by
            #5

            @SGaist Thank you.

            Where there's a will, there is a way.

            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