This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Remove (as in delete) element in a javascript plasmoid

Tags: None
(comma "," separated)
User avatar
Blizzz
Registered Member
Posts
161
Karma
0
OS
Hi there,

this thing is bugging me for a while now...

basically it is as simple as that: i have a couple of lables and textedit-elements in a layout which i want to be able to remove - to reset the layout and fill it in another way on user action.

The layout classes offer two functions for that, removeItem() and removeAt(). The first method takes a variable referencing the element, the second an index. However, both possibilities end up in having the elements rather detached, but are still visible. So, has anyone an idea how i can really and finally remove such an element?

Below is a minimalistic, but working code snippet that demonstrates the behaviour. A layout contains a PushButton as well as a Label. When you click the PushButton, the Label gets "removed" (as in detached, but not destroyed) from the layout, so that the PushButton gets all the space (ok), but the Label is still visible and on top of it.

Code: Select all
function killLabel() {
    layout.removeItem(innocentLabel);
//     layout.removeAt(1); <-- all the same, eventually
}

plasmoid.resize(100, 200);

layout = new LinearLayout;
layout.orientation = QtVertical;

killButton = new PushButton;
killButton.text = "Kill teh Label!";
killButton.clicked.connect(killLabel);

innocentLabel = new Label;
innocentLabel.text = "Don' kill me, plz!";

layout.addItem(killButton);
layout.addItem(innocentLabel);


Image
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
You need to destroy the Label object somehow, not sure how one does that in javascript. You could try to call deleteLater() on it however, which is a Qt C++ call. It may not be available in the Javascript API though.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
Blizzz
Registered Member
Posts
161
Karma
0
OS
bcooksley wrote:You need to destroy the Label object somehow, not sure how one does that in javascript.

Exactly!

bcooksley wrote:You could try to call deleteLater() on it however, which is a Qt C++ call. It may not be available in the Javascript API though.

At least it is not documented, but may be hidden or so... i will have a look this evening, thanks.


Image
User avatar
Blizzz
Registered Member
Posts
161
Karma
0
OS
It seems like I found a solution. It looks kind of dirty, though, but seem to work. Full story:

I printed all members of the plasmoid object and indeed I found the methode deleteLater(). Using it by passing the element object did not have an visible effect, though – but an inner one, as i can run this line only once. I also tried it in combination with plasmoid.update() with the same results.

So I also gave out the members of the Label object and found the method close(). So i tried this and - woohoo - the Label vanished. However, the variable still has had the object reference. So I added "delete innocentLabel" and this was done, too.

→ I hope this is a solution that also sets free the occupied memory. It feels dirty anyway.

The full code snippet which hopefully really and finally deletes the object:
Code: Select all
function killLabel() {
    layout.removeItem(innocentLabel);
    plasmoid.deleteLater(innocentLabel);
    innocentLabel.close();
    delete innocentLabel;
    plasmoid.update();
}


@bcooksley: Thanks for the hint and for bringing me on the right path

Update: The delete line only deletes the reference to the object. Job of the garbage collection is to clear the object, if it is unreferenced. Is it possible that some internal stuff keeps it alive until the plasmoid is removed or plasma quitted?


Image
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Note that you shouldn't need to close it or otherwise delete it, as deleteLater() should ensure it is destroyed on the next pass of the event loop. It is probable that Plasma does something to keep these objects alive however.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
Blizzz
Registered Member
Posts
161
Karma
0
OS
Okay, PEBCAK.

Instead of calling plasmoid.deleteLater(thing) the correct usage is to call the method deleteLater() of the object itself. Must have overlooked it, when i studied the object's method :-/

Thanks for your help and insisting on deleteLater :)


Image


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient