[solved]Problem with script communicating with ui

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
User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

[solved]Problem with script communicating with ui

Post by petevick » Mon Mar 08, 2021 2:32 pm

I'm expanding my LineBreakSymbol script to include three options in the options toolbar, a checkbox, a spinbox and a pair of radiobuttons. I'm communicating with the ui using the .prototype.slot<....>Changed = function(value) method. I've taken the method from BreakOutManual.js, and based the ui on BreakOutManual.ui but I am unable to get the script to communicate with the ui to update the variables. I also followed the guidance here -> https://qcad.org/en/tutorial-interactive-script-actions and particularly the Adding Widgets to the Options Toolbar section.

I just can't see what I'm doing wrong. script and ui attached. Please help, brain is aching !!
Attachments
LineBreakSymbol.zip
(2.95 KiB) Downloaded 323 times
Last edited by petevick on Mon Mar 08, 2021 8:27 pm, edited 1 time in total.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

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

Re: Problem with script communicating with ui

Post by andrew » Mon Mar 08, 2021 2:40 pm

It's the order of things:

Code: Select all

LineBreakSymbol.prototype.slotRemoveSegmentChanged = function(value) {
    this.removeSegment = value;
};

LineBreakSymbol.prototype.slotBreakHeightChanged = function(value) {
    this.breakHeight = value;
    this.updatePreview(true);
};

LineBreakSymbol.prototype.slotIncSegChanged = function(value) {
    this.incSeg = value;
};

LineBreakSymbol.prototype = new EAction();
The last line here overwrites and essentially removes those methods defined above it.

Make sure that

Code: Select all

LineBreakSymbol.prototype = new EAction();
is directly after the constructor function, before attaching any functions to it:

Code: Select all

// constructor:
function LineBreakSymbol(guiAction) {...}

// inheritance (defining the prototype):
LineBreakSymbol.prototype = new EAction();

// methods (attaching functions to prototype):
LineBreakSymbol.prototype.myFunction1 = function(...) {...}
LineBreakSymbol.prototype.myFunction2 = function(...) {...}

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: Problem with script communicating with ui

Post by petevick » Mon Mar 08, 2021 2:49 pm

Thanks for the reply Andrew. I've tried moving the LineBreakSymbol.prototype = new EAction(); to after the function LineBreakSymbol(guiAction) {}, followed by the three .prototype.slot<....>Changed = function(value), but then the script hangs in Qcad.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

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

Re: Problem with script communicating with ui

Post by andrew » Mon Mar 08, 2021 2:56 pm

If you look at the output in the terminal from which you are starting QCAD, you can see the exception caused by the script:

Code: Select all

script engine exception:  "TypeError: Result of expression 'this.startpt' [undefined] is not an object."

Code: Select all

Warning:  "<anonymous>(preview = true) at scripts/Misc/MiscDraw/LineBreakSymbol/LineBreakSymbol.js:143\n<anonymous>(clear = true) at /Users/andrew/data/RibbonSoft/projects/QCAD3/qcad/scripts/EAction.js:1714\n<anonymous>(value = 1) at scripts/Misc/MiscDraw/LineBreakSymbol/LineBreakSymbol.js:53\n<anonymous>(widget = QToolBar, group = 'LineBreakSymbol', signalReceiver = RActionAdapter(0x7fa359c8b600)) at /Users/andrew/data/RibbonSoft/projects/QCAD3/qcad/scripts/WidgetFactory.js:830\n<anonymous>(resume = false) at /Users/andrew/data/RibbonSoft/projects/QCAD3/qcad/scripts/EAction.js:409\n<anonymous>() at /Users/andrew/data/RibbonSoft/projects/QCAD3/qcad/scripts/EAction.js:99\n<anonymous>() at scripts/Misc/MiscDraw/LineBreakSymbol/LineBreakSymbol.js:66\n<native>(RActionAdapter(0x7fa359c8b600)) at -1\n<global>() at 1"
You might want to check if this.startpt is defined at the start of getOperation and return undefined if not.

Code: Select all

if (!isVector(this.startpt)) {
    return undefined;
}

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: Problem with script communicating with ui

Post by petevick » Mon Mar 08, 2021 3:00 pm

Thanks Andrew, I'll take a look.
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

User avatar
petevick
Premier Member
Posts: 392
Joined: Tue May 19, 2020 9:34 am
Location: North Norfolk coast UK

Re: Problem with script communicating with ui

Post by petevick » Mon Mar 08, 2021 8:26 pm

Thanks Andrew, again, all working now :mrgreen:
Pete Vickerstaff
Linux Mint 21.2 Cinnamon, Qcad Pro 3.29.6

Post Reply

Return to “QCAD Programming, Script Programming and Contributing”