Dead QML elements receiving signals? - qt

I have code similar to the following:
...
id: myComponent
signal updateState()
property variant modelList: []
Repeater {
model: modelList
MyButton {
...
Connection {
target: myComponent
onUpdateState: {
...
}
}
}
}
I assign a value to modelList and then issue myComponent.updateState() to update the MyButton components in the repeater. At this point I get a lot of warnings about non existent properties
It seems like the signal gets passed to the MyButton(s) that doesn't exist anymore (since the repeater will rerun when I change modelList).
Is there a way of avoiding this or should I simply ignore the warnings?

I had a similar problem when destroying QML components connected to C++ signals. The problem was solved by adding a handler for disconnecting the signals when the components were destroyed. In the dynamically generated components you could try manually connecting the signals, in order to manually disconnect them on destruction. In my case the code looks something like this:
MyComponent {
Component.onCompleted: signal.connect(callback_function)
Component.onDestruction: signal.disconnect(callback_function)
function callback_function() {
// process signal
}
}
It might be, there is a better solution not needing manually connecting and disconnecting the signals, but this worked for me. If you add a console.log("Destroying...") to the onDestruction handler you can check whether or not the components are disconnecting the signal, thus actually being destroyed.

Related

How to find subsequent (all) SLOT from a subclass

I need to block specific buttons on an MMI.
I implemented a button blocking function in a subclass of QPushButton.
For this, I used the clicked() signal and blocked the button with blockSignals(true).
This means that with each button clicked on my MMI, 2 SLOTS are always called.
But when calling the blocking of a specific button, I get the first SLOT (clicked()) of my subclass, in which I block the button, then I then arrive in the original SLOT linked to this button, which is still called despite the blocking (the first time only).
How can I in my QPushButton subclass know the subsequent SLOTs linked to this button and avoid them (delete them)?
void QbtnStandardButton::slotButtonClicked(void)
{
if (modeProtection)
{
// Special mode to protect/unprotect the button
if (isProtected())
{
// Reset the protection
this->blockSignals(false);
}
else
{
// Set the protection: button will be unclickable
this->blockSignals(true);
}
modeProtection = false;
}
if (isProtected())
{
QMessageBox *pMsgBox = new QMessageBox(QMessageBox::Information,
"Protection",
"This button is protected!",
QMessageBox::Ok);
pMsgBox->exec();
pMsgBox->deleteLater();
// Here: remove subsequent SLOT of this button ?
}
}
I think it's very difficult if not impossible to find SLOTS linked to a button.
I worked around the problem by using an eventFilter() instead of a SIGNAL() in my base class.
In this case, I can filter the "clicked()" event before it is reissued.

QML - Capture all UI events of children objects

In my QML project, I need an object to capture all the UI events of its children objects. So, if any of its children register a click or something, the parent object needs to know about it. The issue here is that all of the children objects are pre-defined classes such as MyButton or MyComboBox. These classes all have defined MouseAreas and onClicked() functions that can't be overridden. Therefore, I need the parent object to capture all the events of its children WITHOUT modifying the MouseAreas of the children. Please let me know the best way to accomplish this.
You can crawl the object tree, connecting a function to every onClicked-signal.
For this we need three parts:
The signal that shall be connected
The function that does the crawling
A function that creates another function to call the signal with custom arguments.
I chose, that my signal shall have to arguments: sender and arguments. Sender is the object, that I clicked on, and arguments are the arguments of the clicked-signal of the clicked object. This is empty for QtQuick.Controls 2.x Buttons and contains one entry (mouse) for MouseAreas.
signal somethingWasClicked(var sender, var arguments)
As this signal has not the same signature as clicked for every clickable object, we can't connect it directly to this signal. We need a intermediary function that calls the signal for us, has the needed arguments in it's scope and has no arguments. We need to build this dynamically for each object we spy on.
function createSpyFunction(object) {
return function() { somethingWasClicked(object, arguments) }
}
And lastly for the crawl-function. We need to store the function we create somewhere. For this I utilize the fact that all QtObjects are JS-Objects in some sense, so I can use Object.defineProperty to dynamically add JS-properties to them. Here I can store our function, without the need of modyfing the sourcecode of the components them selves.
After creating this function, I connect it to the onClicked-handler.
function crawlChildren(obj) {
if (obj.onClicked) {
Object.defineProperty(obj, '__clickedFunction', { value: createSpyFunction(obj) })
obj.onClicked.connect(obj.__clickedFunction)
}
if (obj.children) {
var i = 0
for (; i < obj.children.length; i++) {
crawlChildren(obj.children[i])
}
}
}
This function I finally call in
Component.onCompleted: crawlObj(this.contentItem)
of the root ApplicationWindow of my programm.
As I only call it in the Component.onCompleted-handler, objects that will be added after this, won't be spied uppon. To change this, we will also need to spy on onChildrenChanged, and crawl the newly added objects as well. The process there is almost similar, so this is left as an exercise to the user, or might be subject to a new question.
You can try to overlay your items with a MouseArea. In the event handlers you can check the position and call the event handlers of the underlying items.
Item {
MyButton { id: mybutton
/* set the anchors */
}
MyMouseComboBox { id: myMouseComboBox
/* set the anchors */
}
MouseArea {
anchros.fill: parent
onClicked: {
// mouse.accepted = false
// Check whether clicked point is within mybutton
// on true, call mybutton.doSomething()
// or call mybotton.onPressed(mouse)
}
}

QListWidget: change selection without triggering selectionChanged

I'm using a QListWidget to control and display some state.
Now to control the state, user-selection in the widget is used.
to respond to that, I've connected the selectionChanged signal.
However the state can change by itsself and when that happens, I have a complete new state and want the selection to change.
To achieve that I'm iterating over the state and the items like this:
for item, s in zip(items, state):
item.setSelected(s)
However this triggers selectionChanged (even in every single iteration)
I don't want that to happen at all.
is there another way to respond to the selection-change?
You can simply use the QSignalBlocker class. Before calling a function which emits a signal, instantiate a QSignalBlocker object.
// ui->ListWidget is available.
{
QSignalBlocker blocker( ui->ListWidget );
for ( auto item : items )
{
item->setSelected();
}
}

Page loaded signal

We have a gui with multiple pages. On each page there are custom text components with a data coupling. When a page is shown/loaded the text components update their data binding using the Component.oncompleted and onVisibleChanged signals. To finalize this a call must be made to the communication module after each child has done its updates.
When pages are changed this can be done in the "onVisibleChanged" of the page. The problem is finding a point to do this when the page is created. The Component.onCompleted is performed before the one of the children.
Do you know any signal to use for this?
As a workaround I have an empty NumberAnimation with a Component.onCompleted call. This apperently runs after the children but feels like a hack.
Here goes the example code
DataText
DataText
{
property string address: ""
text: name + Model.getSingleItems().get(address).value //property binding here
onVisibleChanged: { Model.getSingleItems().setSubscribeStatus(address, visible);} //Stages the address for (un)subscribe
Component.onCompleted:{ Model.getSingleItems().setSubscribeStatus(address, visible);} //Stages the address for (un)subscribe
}
An example page
Rectangle
{
DataText
{
address: "datakey"
}
//This code must be executed after all setSubscribeStatus calls to
//finish the subscriptions.
Component.onCompleted:{ Model.getSingleItems().finialize();}
onVisibleChanged: { Model.getSingleItems().finialize();}
}
I'm looking for a way to subscribe to a batch of data adresses in the communication module. We can't keep all the data up to date due to limitations in our bandwith with the actual device.

QML Calendar : Programmatically call onClicked handler

My QML application is displaying a Calendar element.
When the selected date is changed (clicked), I need to update an other element (an image).
I have something like this:
Calendar {
id: calCalendar
onClicked: {
// update other QML element
}
}
It works fine when the user click with the mouse on the calendar: the other element (the image) is correctly updated.
My problem is initialization : when my app is started, the calendar displays the current date by default, and I'd like to programmatically call the onClicked handler, to make sure the image is up to date.
I don't know how to do that.
If you want to do something when a QML component is done initializing you can use the Component.onCompleted : slot.
Calendar {
id: calCalendar
onClicked: {
// update other QML element
}
Component.onCompleted: {
// Do stuff for initialization.
// you could do this here : calCalendar.Clicked()
// if you want to use the same code for initialization and for user input handling later on.
}
}
The point is the following : onXXX : { declares a slot to handle the singal XXX. Here the signal is Clicked. You can trigger the signal programmatically as you say, just by invoking it like a function. You'll need to know a valid overload for the arguments (if any).

Resources