Author Topic: Scripting: how do I set a checkbox state?  (Read 2725 times)

I have a plugin which needs to remember user settings. I use the alg.settings namespace to store the values which works fine. However, I have a checkbox on a dockWidget and I need to set the checked state depending on the retrieved setting.

How do I do this?

When you create your checkbox in qml, you can set a property named "checked".
Simply change its value based on what is stored in the settings. :)
Don't forget your log file. It can be exported from the Help menu of the software.
-----------
Fabrice Piquet aka Froyok. Product Manager, Technical Artist and Documentation at Adobe.

I can do this from the same file, but how do I do this from main.qml (checkbox is in DockWidged.qml) or from the .js file?

Can you show an example of your code and what you want to achieve ?
Don't forget your log file. It can be exported from the Help menu of the software.
-----------
Fabrice Piquet aka Froyok. Product Manager, Technical Artist and Documentation at Adobe.

See the line "Problem is here". I want to read a settings value at startup and then set the state of the checkbox accordingly.

In main.qml:
Code: [Select]
    //executed at startup
    Component.onCompleted:{
       
//add toolbar
var button = alg.ui.addToolBarWidget("button.qml")//add a button to toolbar

var dockWidget = alg.ui.addDockWidget("dockWidget.qml")//add dock widget
var exportExternal = alg.settings.value("exportExternalDockWidget", false);

//Problem is here. How to set the checkbox (checked or not checked) as retrieved by the alg.settings value.
//Something like this? But that doesn't work.
dockWidget.exportExternalCheckBox.checked = exportExternal;

NormalFix.log("Normal Fix Plugin has been created", "info")
}

In dockWidget.qml:

Code: [Select]
CheckBox {
y:30
checked: false
id: exportExternalCheckBox

style: CheckBoxStyle {
label: Text {
color: "white"
text: "Export to external directory"
}
}

onClicked:{

NormalFix.exportExternalDockWidget = checked;
alg.settings.setValue("exportExternalDockWidget", NormalFix.exportExternalDockWidget);
}
}

Have you tried to use an 'alias' instead of the 'id'?
Create an alias in your root component in dockWidget.qml like this: property alias exportExternal: exportExternalCheckBox
Then, in your main.qml, replace dockWidget.exportExternalCheckBox by dockWidget.exportExternal
Don't forget your log file. It can be exported from the Help menu of the software.
-----------
Fabrice Piquet aka Froyok. Product Manager, Technical Artist and Documentation at Adobe.

Ok, will try that.

Just tried it but unfortunately it doesn't work. Using the alias I can't set the checkbox state on startup.

Just tried it but unfortunately it doesn't work. Using the alias I can't set the checkbox state on startup.
Can you explain a bit more ? What doesn't work ?
The code mentioned above was working on my side.
Don't forget your log file. It can be exported from the Help menu of the software.
-----------
Fabrice Piquet aka Froyok. Product Manager, Technical Artist and Documentation at Adobe.

The checkbox was still set to the default state at startup and using the alias wouldn't override that. But if it works on your side, maybe I did something wrong. Will try again.

Is it also possible to set the checkbox state in java script?

I have attached a little example. Hopefully that will explain a bit better how it works. :)
Don't forget your log file. It can be exported from the Help menu of the software.
-----------
Fabrice Piquet aka Froyok. Product Manager, Technical Artist and Documentation at Adobe.

Thanks, I got it to work now. Even with the example it took quite some experimenting. Turns out I had to place the alias in the root, not in the rectangle:

Code: [Select]
Item {
    id: root
property alias alias_checkbox: id_checkbox
        objectName: "Normal Fix"

    Rectangle {
       id: rect    
       anchors.fill: parent
       color: "#2e2e2e"   
   
// file dialog
        Item {
          id: browse  
          anchors.left: parent.left
          anchors.leftMargin: 5
          y:15
Last Edit: November 18, 2016, 12:05:04 pm