Know if an Item is the currentItem of a StackView
-
Here we see a way to find the current item in a StackView checking the objectName property against the currentItem one.
But what about if I would know this information inside the item itself?Example, I push several items on the stack. Each one connects to some signal from the C++ code, but I want to execute them only if the item is on the top (i.e. visible). I tried to use the visible or z properties without success.
-
Here we see a way to find the current item in a StackView checking the objectName property against the currentItem one.
But what about if I would know this information inside the item itself?Example, I push several items on the stack. Each one connects to some signal from the C++ code, but I want to execute them only if the item is on the top (i.e. visible). I tried to use the visible or z properties without success.
@Mark81 said in Know if an Item is the currentItem of a StackView:
but I want to execute them only if the item is on the top (i.e. visible)
why didn't you check the docs?
-
I did. And I did it just again.
Perhaps I'm blind today but from that doc I really cannot understand how an Item might now it's own position on the stack.@Mark81
I don't quite get what you are looking for. http://doc.qt.io/qt-5.9/qml-qtquick-controls2-stackview.html#currentItem-prop is the "current top-most item in the stack", so either your item is or is not the current item, and that's what you want to know? -
@JonB, of course the
currentItem
property tells me what is the top-most item. But this is a property of theStackView
. As I said I need to know this inside each item, hence I have no access to theStackView
object.Example:
main.qml
StackView { id: stackView } ... stackView.push(item1) stackView.push(item2) stackView.push(item3)
Where "itemX" are instances of MyItem:
myitem.qml
function doSomething() { // if (I am the top/currentItem) do something... }
-
@JonB, of course the
currentItem
property tells me what is the top-most item. But this is a property of theStackView
. As I said I need to know this inside each item, hence I have no access to theStackView
object.Example:
main.qml
StackView { id: stackView } ... stackView.push(item1) stackView.push(item2) stackView.push(item3)
Where "itemX" are instances of MyItem:
myitem.qml
function doSomething() { // if (I am the top/currentItem) do something... }
@Mark81 said in Know if an Item is the currentItem of a StackView:
As I said I need to know this inside each item, hence I have no access to the StackView object.
and again ... take a look at the docs...
-
@JonB, of course the
currentItem
property tells me what is the top-most item. But this is a property of theStackView
. As I said I need to know this inside each item, hence I have no access to theStackView
object.Example:
main.qml
StackView { id: stackView } ... stackView.push(item1) stackView.push(item2) stackView.push(item3)
Where "itemX" are instances of MyItem:
myitem.qml
function doSomething() { // if (I am the top/currentItem) do something... }
@Mark81
So if you are saying neither "visible" not "z" are used to tell you want you want, you must have access to the stackview (by whatever means) in order to glean your information, since I presume they are not aware they are inside a stackview.If that is a problem, what about sub-classing the objects you put into the stackview so that they note their parent stackview in order to search it? Or, look at http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html#supported-attached-properties, and also the whole of the Advanced Properties section there.
-
@JonB, of course the
currentItem
property tells me what is the top-most item. But this is a property of theStackView
. As I said I need to know this inside each item, hence I have no access to theStackView
object.Example:
main.qml
StackView { id: stackView } ... stackView.push(item1) stackView.push(item2) stackView.push(item3)
Where "itemX" are instances of MyItem:
myitem.qml
function doSomething() { // if (I am the top/currentItem) do something... }
@Mark81 said in Know if an Item is the currentItem of a StackView:
@JonB, of course the
currentItem
property tells me what is the top-most item. But this is a property of theStackView
. As I said I need to know this inside each item, hence I have no access to theStackView
object.Example:
main.qml
StackView { id: stackView } ... stackView.push(item1) stackView.push(item2) stackView.push(item3)
Where "itemX" are instances of MyItem:
myitem.qml
function doSomething() { // if (I am the top/currentItem) do something... }
if you push an item it's by default the top /current item. So why don't you call the relating function when you push the item on the stackview?
{ stackView.push(item1) item1.doSomething() } ... { stackView.push(item2) item2.doSomething() } ....
-
@Mark81 said in Know if an Item is the currentItem of a StackView:
@JonB, of course the
currentItem
property tells me what is the top-most item. But this is a property of theStackView
. As I said I need to know this inside each item, hence I have no access to theStackView
object.Example:
main.qml
StackView { id: stackView } ... stackView.push(item1) stackView.push(item2) stackView.push(item3)
Where "itemX" are instances of MyItem:
myitem.qml
function doSomething() { // if (I am the top/currentItem) do something... }
if you push an item it's by default the top /current item. So why don't you call the relating function when you push the item on the stackview?
{ stackView.push(item1) item1.doSomething() } ... { stackView.push(item2) item2.doSomething() } ....
-
@Mark81
So if you are saying neither "visible" not "z" are used to tell you want you want, you must have access to the stackview (by whatever means) in order to glean your information, since I presume they are not aware they are inside a stackview.If that is a problem, what about sub-classing the objects you put into the stackview so that they note their parent stackview in order to search it? Or, look at http://doc.qt.io/qt-5/qml-qtquick-controls-stackview.html#supported-attached-properties, and also the whole of the Advanced Properties section there.
@Mark81
If you need to call doSomething every time a new item comes to top of stack, you can call this function in the transition among thes items.Example:
StackView { pushEnter: Transition { id: pushEnter ScriptAction { script: pushEnter.ViewTransition.item.dosomething(); } } popEnter: Transition { id: popEnter ScriptAction { script: pushEnter.ViewTransition.item.dosomething(); } }
Or when currentItem has changed then call the function
StackView { onCurrentItemChanged: {if(currentItem) currentItem.doSomething();} // check if currentItem is not null }
-
To know if your item is the current item of a StackView, you can indeed use an attached property.
The one you need is
StackView.status
You might use it like so:
function doSomething() { if (StackView.status === StackView.Active) doStuff(); }
If yor doSomething function is not in the direct scope of the stack item (it's in a child scope for example), you can still access the attached property by explicitely specifying the stack item:
function doSomething() { if (someItem.StackView.status === StackView.Active) doStuff(); }
-
Replying to this old thread because it still "Unsolved".
I happened to need the exact same thing as the original poster:
Multiple items in myStackView
had connections to the same rotary encoder button, and I naively thought that the signals would only be processed by the active item.
Debug messages proved otherwise, so I needed to disable the connection when the item is not active.I did it as follows:
SomeStackViewItem { Component.onCompleted: { rotConnect.enabled = Qt.binding(function() {return (StackView.status === StackView.Active)}) } Connections { id: rotConnect target: um //This does not work, since StackView is an attached property, we need to do the binding later. //enabled: StackView.status === StackView.Active } }
Cheers.
-
Replying to this old thread because it still "Unsolved".
I happened to need the exact same thing as the original poster:
Multiple items in myStackView
had connections to the same rotary encoder button, and I naively thought that the signals would only be processed by the active item.
Debug messages proved otherwise, so I needed to disable the connection when the item is not active.I did it as follows:
SomeStackViewItem { Component.onCompleted: { rotConnect.enabled = Qt.binding(function() {return (StackView.status === StackView.Active)}) } Connections { id: rotConnect target: um //This does not work, since StackView is an attached property, we need to do the binding later. //enabled: StackView.status === StackView.Active } }
Cheers.
@Diracsbracket said in Know if an Item is the currentItem of a StackView:
//This does not work, since StackView is an attached property, we need to do the binding later. //enabled: StackView.status === StackView.Active
That's not why it doesn't work. I mentionned why in the post above
If your doSomething function is not in the direct scope of the stack item (it's in a child scope for example), you can still access the attached property by explicitely specifying the stack item
In your case, you would do :
SomeStackViewItem { id: root Connections { target: um enabled: root.StackView.status === StackView.Active } }
-
@Diracsbracket said in Know if an Item is the currentItem of a StackView:
//This does not work, since StackView is an attached property, we need to do the binding later. //enabled: StackView.status === StackView.Active
That's not why it doesn't work. I mentionned why in the post above
If your doSomething function is not in the direct scope of the stack item (it's in a child scope for example), you can still access the attached property by explicitely specifying the stack item
In your case, you would do :
SomeStackViewItem { id: root Connections { target: um enabled: root.StackView.status === StackView.Active } }
@GrecKo said in Know if an Item is the currentItem of a StackView:
That's not why it doesn't work. I mentionned why in the post above
You're right. That indeed simplifies things...
Thanks!