UI change Label by Input

Discussion forum for C++ and script developers who are using the QCAD development platform or who are looking to contribute to QCAD (translations, documentation, etc).

Moderator: andrew

Forum rules

Always indicate your operating system and QCAD version.

Attach drawing files, scripts and screenshots.

Post one question per topic.

Post Reply
Simon2344
Newbie Member
Posts: 3
Joined: Fri Feb 12, 2021 6:37 pm

UI change Label by Input

Post by Simon2344 » Thu Mar 24, 2022 4:01 pm

Hey everyone,

I am trying to get some dynamics into my UI but don´t understand how the signal-slot concept of Qt works.
Below you can find an example code. Can someone show me how I take the values from widgets["txtInput"] and give it to widgets["txtOutput"]?
I use QCAD Version 3.26.4 with Qt Version 5.13.2 on Win10.

Code: Select all

// MyAction.js
 
// Include base class definition:
include("scripts/EAction.js");
include("scripts/WidgetFactory.js");
include("../MyScript.js");


function MyAction(guiAction){
	MyScript.call(this, guiAction);
}

MyAction.prototype = new EAction();
MyAction.includeBasePath = includeBasePath;

MyAction.prototype.beginEvent=function() {
	EAction.prototype.beginEvent.call(this);

	var dialog = WidgetFactory.createWidget(MyAction.includeBasePath, "MyAction.ui");

	// Restore the previous user data or display default values as set in Qt Designer:
	WidgetFactory. restoreState(dialog);

	//Display and execute the dialog:
	if(!dialog.exec()){
	// User hit cancel:
		dialog.destroy();
		EAction.activateMainWindow();
		this.terminate();
		return;
	}
	
	// User hit ok.
	//Store the new user input.
	WidgetFactory.saveState(dialog);

	var widgets						= getWidgets(dialog);
	var input							= widgets["txtInput"].text;
	var output						= widgets["txtOutput"].text;

	this.terminate();

};

MyAction.init = function(basePath) {
  var title = qsTr("MyAction");

  var action = new RGuiAction(title, RMainWindowQt.getMainWindow());
  action.setRequiresDocument(false);
  action.setScriptFile(basePath + "/MyAction.js");
  action.setStatusTip(qsTr(title));
  action.setGroupSortOrder(73100);
  action.setSortOrder(400);
  action.setWidgetNames(["MyScriptMenu"]);
}
Thank you in advance!

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

Re: UI change Label by Input

Post by andrew » Thu Mar 24, 2022 4:33 pm

This should work:

Code: Select all

widgets["txtOutput"].text = widgets["txtInput"].text;

Simon2344
Newbie Member
Posts: 3
Joined: Fri Feb 12, 2021 6:37 pm

Re: UI change Label by Input

Post by Simon2344 » Thu Mar 24, 2022 4:49 pm

Thank you Andrew for your fast response!

But I meant more like in a callback. So that while the user is typing something into txtInput those inputs gets shown in txtOutput.
So I mean directly in the UI.

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

Re: UI change Label by Input

Post by andrew » Thu Mar 24, 2022 8:08 pm

For this, you'll have to work with signals / slots:

Code: Select all

widgets["txtInput"].textChanged.connect(function(text) { widgets["txtInput"].text = text; });
You can find lots of other code example in our repository at:
https://github.com/qcad/qcad/search?q=%22textChanged%22

Simon2344
Newbie Member
Posts: 3
Joined: Fri Feb 12, 2021 6:37 pm

Re: UI change Label by Input

Post by Simon2344 » Tue Apr 12, 2022 3:08 pm

So after some work I was able to create one simple callback using the Qt Creator (C++).
It works just fine within the Creator but when I paste it in the original Project which is written in ECMA, open it via QCAD, the Callback does not work anymore.

It seems like that the new .cpp files doesn't get called by QCAD. Do you know if thats possible. Maybe there is a common mistake I'm doing right now. I could not find any examples in the repository regarding the interaction between C++ and ECMA.

Thank you in advance!

Sincerly,
Simon

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

Re: UI change Label by Input

Post by andrew » Tue Apr 12, 2022 8:09 pm

There's a fundamental misunderstanding here.

To program in C++, you would have to create a plugin. That's code compiled into a library that can be loaded at runtime. A plugin would be a .dll file under Windows created with a C++ compiler. You can read more about plugins in our tutorial Creating a QCAD Plugin.

More likely, you will want to keep your add-on strictly ECAMScript. The QCAD ECMAScript interface is very powerful and gives you access to almost the complete QCAD and Qt APIs. There's also a ton of example code in our git repository.

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”