So I am trying to create an expense tracking application. I'm building the UI in Slint. For backend I will be using Rust.
The most basic function is to accept the item name and item cost from the user. I built a decent layout for the elements. Here it is: (I will be highlighting the functions and code that I have written in an attempt to achieve my target with a '//##')
slint::slint!{
import { Button, LineEdit} from "std-widgets.slint";
export component Box inherits Window {
width: 400px;
height: 300px;
background: #f0f0f0;
in-out property <string> amt; //##
in-out property <string> name; //##
//## (This function is created to act as a callback to the backend,
built in Rust, but is currently of no use, as the values input by the user aren't
being collected at once as of yet.)
callback clicked <=> submitBtn.clicked;
//## (This whole function)
function submit() {
name.setName();
amt.setCost();
txt.text = "Name: " + root.name + ", Cost: " + root.amt;
}
//## (I added this element to see if I could make changes in here directly)
//## (Doesn't work)
txt := Text {
text: "Name: " + root.name + ", Cost: " + root.amt;
font-size: 16px;
color: #000;
horizontal-alignment: center;
vertical-alignment: center;
y: 250px;
}
VerticalLayout {
Rectangle {
width: parent.width;
height: 20px;
background: #ccc;
}
Text{
text: "Hello";
font-size: 24px;
color: #333;
horizontal-alignment: center;
vertical-alignment: center;
}
Text {
text: "Enter expense information below:";
font-size: 20px;
vertical-alignment: center;
horizontal-alignment: center;
}
HorizontalLayout {
alignment: center;
spacing: 66px;
VerticalLayout {
Text{
text: "Name";
font-size: 16px;
color: #555;
horizontal-alignment: center;
}
name:= LineEdit {
placeholder-text: "Enter name";
width: 101px;
horizontal-alignment: center;
//## (This function, attempt to update name variable)
function setName() {
root.name = name.text;
}
}
}
VerticalLayout {
Text{
text: "Cost";
font-size: 16px;
color: #555;
horizontal-alignment: center;
}
amt:= LineEdit {
placeholder-text: "Enter cost";
width: 101px;
horizontal-alignment: center;
//## (Successful attempt at checking whether root variable 'amount'
is accessible by this 'amt' element -- part of debugging)
// text: root.amt;
//## (This function, attempt to update amount variable)
function setCost() {
root.amt = amt.text;
}
}
}
}
spacing: 10px;
//## (This button is defined so that the callback can be created at root level)
submitBtn:= Button {
text: "Click me";
width: 100px;
height: 40px;
x: 150px;
clicked => {
//## (This loophole was created because elements and their properties
such as 'text' can only be accessed by the child or their parents.
I think this button falls in the *uncle* category.
root.submit();
}
}
// padding: 20px;
Rectangle {
width: parent.width;
height: 20px;
background: #cccfcc;
}
}
}
}
So, in the end, what's happening is that considering how elements and their properties can only be accessed by the child or the parent, I thought of creating a loop, where
- the button when clicked ->
- Calls the 'submit' function at root ->
- calls both setName and setAmt functions in 'name' and 'amt' elements, which update the value of 'amt' and 'name' properties of the root element.
However, this approach is not working for me. What could be the issue? Do you have a simpler solution? I would really prefer that.