Skip to content

Commit

Permalink
docs(example): example script to get/set data
Browse files Browse the repository at this point in the history
Add an example of use for script dialog with a widget

Fixes #28
  • Loading branch information
smnppKDAB authored and narnaud committed Jun 26, 2024
1 parent 49ae6cf commit 9c48991
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 0 deletions.
59 changes: 59 additions & 0 deletions examples/ex_script_dialog.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Example script with all the different widgets managed by ScriptDialog showing how to set/get data

import Script 1.0

ScriptDialog {
id: root

// Function called at startup, once the dialog is setup
function init() {
// Initializing widget data
data.lineEdit = "Initialization text"
// Define checkBox as unchecked and radioButton as checked
data.checkBox = false
data.radioButton = true
data.spinBox = 1
data.doubleSpinBox = 4.2
// Defined the possible choices for the comboBoxfine
data.comboBoxModel = ["1", "2", "3"]
data.comboBox = "2"
}

function showData() {
Message.log("OK button is clicked")
Message.log("LineEdit data: " + data.lineEdit)
Message.log("CheckBox data: " + data.checkBox)
Message.log("RadioButton data: " + data.radioButton)
Message.log("SpinBox data: " + data.spinBox)
Message.log("DoubleSpinBox data: " + data.doubleSpinBox)
Message.log("ComboBox data: " + data.comboBox)
}

// Function called when the user click on the OK button
onAccepted: {
// Logging the state of each widget when OK is clicked
showData();
}

// Function called when the user click on the Cancel button
onRejected: {
// Logging when the Cancel button is clicked
Message.log("Cancel button is clicked")
}

// Function called when a button is clicked
onClicked:(name)=>{
if (name == "pushButton"){
Message.log("PushButton is clicked")
}
else if (name == "toolButton"){
Message.log("ToolButton is clicked")
}
}
// Function to automatically test the script, useful for automated testing
// It runs the script without user interaction
function test() {
showData();
close();
}
}
151 changes: 151 additions & 0 deletions examples/ex_script_dialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>407</width>
<height>374</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>pushButton</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>lineEdit:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>checkBox</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>CheckBox</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>radioButton</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QRadioButton" name="radioButton">
<property name="text">
<string>RadioButton</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>spinBox</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QSpinBox" name="spinBox"/>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_6">
<property name="text">
<string>doubleSpinBox</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QDoubleSpinBox" name="doubleSpinBox"/>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>comboBox</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QComboBox" name="comboBox"/>
</item>
<item row="8" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>249</height>
</size>
</property>
</spacer>
</item>
<item row="12" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>dialogButtonBox</string>
</property>
</widget>
</item>
<item row="12" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QToolButton" name="toolButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>ToolButton</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_9">
<property name="text">
<string>toolButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
1 change: 1 addition & 0 deletions tests/tst_knut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private slots:

KNUT_EXAMPLE(ex_gui_interactive)
KNUT_EXAMPLE(ex_gui_progressbar)
KNUT_EXAMPLE(ex_script_dialog)
};

QTEST_MAIN(TestKnut)
Expand Down

0 comments on commit 9c48991

Please sign in to comment.