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

Looking for suggestions or hints on best way to...



  • I'm implementing classes which are derived from Qt controls and I need to implement a way of saving an original state of say for example QCheckBox or QComboBox etc.

    The checkbox is easy enough I could just save a boolean value and compare if later, but I'm looking for something more generic.

    I would like to have a function that saves the a copy of the control state and then later I can compare the current state with the original for change. I realise that using memcpy isn't going to be a workable solution for comparing classes, can I rely on the '=' operator to compare a copy against the current version?

    If anyone can suggest a portable and generic way that I can use for Qt controls, very much appreciated.


  • Lifetime Qt Champion

    Hi,

    You can do some meta system programming and store the value of the property that is marked as USER.

    See the property system documentation for more information.



  • @SGaist , I'm looking at this now, I would imagine that implementing this method would require a specific implementation for each control, I was hoping to add something that would require minimal differences between different controls. I started with a base class that has a couple of pure virtual functions such as:

    virtual bool blnChanged() = 0; // Returns true if control has changed from original
    virtual void saveOriginal() = 0; // Saves the current state as the original
    

  • Lifetime Qt Champion

    That's why I am suggesting using meta system programming. Inspect the objects QMetaObject to get the right property and store that one.



  • @SGaist , I'll read up.



  • Doesn't look like a simple solution, I need to take a break and come back fresh another day. Need to think about this.



  • @SGaist , can properties be added at run-time?

    I want to store the original data for each GUI object, I was thinking of doing this in properties the comparing the original data with the current data and returning true if there is a difference.

    Just found this:
    https://doc.qt.io/qt-5/properties.html#dynamic-properties

    Looking into it.



  • Try to use QStateMachine.



  • @SGaist said in Looking for suggestions or hints on best way to...:

    That's why I am suggesting using meta system programming. Inspect the objects QMetaObject to get the right property and store that one.

    Having a quick look shows that QMetaObject even has a userProperty() member functions. The link @SGaist already provided says the following about the USER property:

    The USER attribute indicates whether the property is designated as the user-facing or user-editable property for the class. Normally, there is only one USER property per class (default false). e.g., QAbstractButton::checked is the user editable property for (checkable) buttons.

    So, I would suggest trying something like this:

    const char *propertyName = widget->metaObject()->userProperty()->name();
    QVariant value = widget->property(name);
    ...
    widget->setProperty(name, value);
    

    You need to figure out, though, what happens if there is no USER property. Maybe propertyName would be an empty string or nullptr?



  • I've made some progress in this area, not all widgets have USER property, but its pretty simple to figure out.


Log in to reply