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

State target: object vs type



  • Hello,

    I am new to QML and was going through the QT Quick introduction training about user interaction, input and states session, provided at Qt website.
    I must say it is nice set of series to introduce the QML and Qt Suite. States seems to be really powerful concept. I was wondering shouldn't it be possible to specify type in target of state rather than object?

    Suppose I have 10 text fields on a dialog and I want a similar behaviour and if I specify only states targeted at each object then there will be tremendous duplication of code, instead I can specify that within this item (dialog) all items of type: textfield should be applied this state.

    Thanks for your answers.

    Best,
    Sarang



  • I am not entirely sure I understand what you mean.

    I think you could do what you want by simply creating your own type (that is, a new file), and make that item reference the parents state, for instance. Something along these lines:

    @
    //file: MyTextField.qml

    TextEdit {
    id: textField
    state: parent.state
    enabled: (state == "TheEnabledState")

    // whaterver else you want in here
    }
    @

    Or am I totally off track here?



  • Hi Andre,

    Many thanks for replying.
    Actually my point was:

    @TextEdit {
    id: textField1
    state: parent.state
    enabled: (state == "TheEnabledState")

    // whaterver else you want in here
    }

    TextEdit {
    id: textField2
    state: parent.state
    enabled: (state == "TheEnabledState")

    // whaterver else you want in here
    }
    @
    then I will have to create 2 states for two text fields. But If I have 10 such fields in dialog then I may need to write 10 states, if we go by objects.
    What I am asking is it possible to create 10 textfields and associate them with only one state for consistency and maintenance.

    Other option may be to create a separate textfield qml file (is it called component?) and then use it.

    Thanks,
    Sarang

    Edit: please use @ to mark code sections; Andre



  • No, the point was, that you don't need to maintain 10 states. Instead, after defining your own MyTextEdit as explained above, you can simply do:

    @
    MyTextEdit {
    id: TextEdit1;
    }
    MyTextEdit {
    id: TextEdit2;
    }
    @

    The state is automatically managed the same for all instances of MyTextEdit that way.



  • Ok.

    So the basic philosophy of Qt QML is to create lots of customized components. But then I will have to rename the files and my components accordingly so that I don't have clashes.

    Is there any best practices document about this?

    Thanks!
    Sarang


Log in to reply