[solved] Grab values from text boxes created by a .ui file

Use this forum to ask questions about how to do things in QCAD.

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files and screenshots.

Post one question per topic.

Post Reply
cjm
Junior Member
Posts: 11
Joined: Sun Jun 25, 2017 5:54 pm

[solved] Grab values from text boxes created by a .ui file

Post by cjm » Wed Apr 17, 2019 10:59 am

Say I have a text box widget created by a .ui file:

Code: Select all

<widget class="RMathLineEdit" name="Length">
	<property name="toolTip">
		<string>The length</string>
	</property>
	<property name="text">
		<string notr="true">1000</string>
	</property>
</widget>
and a button:

Code: Select all

<item>
	<widget class="QToolButton" name="Confirm">
		<property name="toolTip">
			<string>Confirm dimensions</string>
		</property>
		<property name="icon">
			<string>confirm.svg</string>
		</property>
	</widget>
</item>
How can I grab the text from the textbox with a function in my .js file?

So far I've been using the workaround of using the slotLengthChanged(length) function to change my variable in the file and executing it using a checkbox instead of a button with slotConfirmedChanged(confirmed) but this is messy and inefficient, I want to grab the text and move on to my next function when I press the button.

Thanks!
Last edited by cjm on Fri Apr 26, 2019 12:27 am, edited 1 time in total.

User avatar
andrew
Site Admin
Posts: 9058
Joined: Fri Mar 30, 2007 6:07 am

Re: Grab values from text boxes created by a .ui file

Post by andrew » Wed Apr 17, 2019 11:42 am

Let's say your dialog widget is called "dialog":

Code: Select all

var lengthEdit = dialog.findChild("Length");

cjm
Junior Member
Posts: 11
Joined: Sun Jun 25, 2017 5:54 pm

Re: Grab values from text boxes created by a .ui file

Post by cjm » Wed Apr 17, 2019 1:49 pm

andrew wrote:
Wed Apr 17, 2019 11:42 am
Let's say your dialog widget is called "dialog":

Code: Select all

var lengthEdit = dialog.findChild("Length");
Using

Code: Select all

qDebug("User input: " + dialog.findChild("Length"));
returns

Code: Select all

"ReferenceError: Can't find variable: dialog"
I am opening the file with

Code: Select all

this.setUiOptions("file.ui");
if that helps.

Edit: Is this assuming my ui file looks like the following?

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
	<class>dialog</class>
	<widget class="QWidget" name="dialog">
	...

User avatar
andrew
Site Admin
Posts: 9058
Joined: Fri Mar 30, 2007 6:07 am

Re: Grab values from text boxes created by a .ui file

Post by andrew » Wed Apr 17, 2019 11:25 pm

In this case, you're talking about the options toolbar at the top, not a dialog:

Code: Select all

var optionsToolBar = EAction.getOptionsToolBar();
var lengthEdit = optionsToolBar.findChild("Length");

cjm
Junior Member
Posts: 11
Joined: Sun Jun 25, 2017 5:54 pm

Re: Grab values from text boxes created by a .ui file

Post by cjm » Thu Apr 18, 2019 12:54 am

andrew wrote:
Wed Apr 17, 2019 11:25 pm
In this case, you're talking about the options toolbar at the top, not a dialog:

Code: Select all

var optionsToolBar = EAction.getOptionsToolBar();
var lengthEdit = optionsToolBar.findChild("Length");
Excellent! That works perfectly when I append .text to the end of the findChild methods. Thank you very much!

How about detecting the clicks on the button and performing a function when it is clicked?

Edit: I'm thinking it should either be

Code: Select all

optionsToolBar.findChild("Confirm").addEventListener("click", function(){});
or

Code: Select all

optionsToolBar.findChild("Confirm").addClickEvent(function(){});
but QCAD doesn't seem to find my button, returning

Code: Select all

TypeError: Result of expression 'optionsToolBar.findChild("Confirm")' [undefined] is not an object.
I'm not even sure it should be a QToolButton and not some other kind of button, considering I want to achieve something exactly like the toolbar in the "Split Entities" tool, but can't find the script file on Github.

User avatar
andrew
Site Admin
Posts: 9058
Joined: Fri Mar 30, 2007 6:07 am

Re: Grab values from text boxes created by a .ui file

Post by andrew » Tue Apr 23, 2019 3:35 pm

Code: Select all

button.clicked.connect("functionName");
or

Code: Select all

button.clicked.connect(MyClass, "functionName");
Please be sure to also check out the Qt API doc and search through the extensive QCAD sources for examples.

Note: usually you don't want to connect signals, etc. You can simply implement slotMyButton for a toolbutton named MyButton. Have a look at the Line2P example.

cjm
Junior Member
Posts: 11
Joined: Sun Jun 25, 2017 5:54 pm

Re: Grab values from text boxes created by a .ui file

Post by cjm » Fri Apr 26, 2019 12:35 am

andrew wrote:
Tue Apr 23, 2019 3:35 pm
Please be sure to also check out the Qt API doc and search through the extensive QCAD sources for examples.

You can simply implement slotMyButton for a toolbutton named MyButton. Have a look at the Line2P example.
Many thanks for all your help, andrew! I think I now have everything I need to finish my tool - expect to see it in the Finished Scripts forum within the next few weeks!

Post Reply

Return to “QCAD 'How Do I' Questions”