I have a QML element "Button" which has a MouseArea element inside.
What I want to do is to send a function to the item and then pass it to MouseArea:
Example:
Button{
id: smth
...
...
onClicked: console.log("Someone Clicked Me!")
}
And my element could be something like this:
Rectangle{
property var onClicked
...
MouseArea{
onClicked : parent.onClicked
}
}
I found out that I can pass the function like in native js without parentheses, but I think there is a way to implement it like MouseArea element does.
The usual way to handle this situation in QML is to let the Button emit a signal, which is then handled by the user of Button.
Button.qml:
Rectangle {
id: root
signal clicked
MouseArea {
anchors.fill: parent
onClicked: root.clicked();
}
}
Usage:
Button {
onClicked: console.log("Button was clicked!");
}
Related
I have StackView in main.qml.I pushed menu.qml file from main.qml using stackview. I'm trying to access stackview in menu.qml file to open new item. Is there a way with which we can push component/items with properties using stackview? My components are basically.qml files for different views
ApplicationWindow {
id: settingsWindow
StackView {
id: stack
initialItem: view
Component {
id: view
MouseArea {
onClicked: stack.push(view)
}
}
}
Button{
id: button1
onClicked: {
stack.pop(StackView.Immediate)
stack.push (Qt.resolvedUrl("menu.qml"))
}
}
}
menu.qml
Item {
Button{
id: button1
onclicked : { stack.push (Qt.resolvedUrl("new.qml")) }
}
}
Assuming you mean you want to access the StackView object from withing pages you pushed on it.
StackView has an attached property, which lets you obtain a reference to the view that owns the page.
Long story short, in Menu.qml you can do:
Item {
id: root
Button {
id: button1
onClicked: { root.StackView.view.push(Qt.resolvedUrl("new.qml")) }
}
}
https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#view-attached-prop
Finally ended up creating each page in StackView as a component property and then pushing each of them using a signal. Added the signal to every page and connected it to main page where stackview existed . This answer helped
https://stackoverflow.com/a/45354861/11288640
I have a listview with a delegate that has MouseArea covering the whole of a delegate. In that MouseArea's onClick slot I specifically set
mouse.accepted = false
but the Combobox from QtQuick.Controls 1.4 that lives in that delegate still refuses to open its popup on clicks. I've tested that combobox should receive the click with:
ComboBox {
id: cbChapters
model: chapters
MouseArea {
anchors.fill: parent
onClicked: {
mouse.accepted = false
console.log("arrived")
}
}
}
And the click events do arrive into this inner mouse area, just not into the combobox itself it seems... what might be the problem?
Answering my own question:
Problem seems to be that MouseArea automatically accepts Pressed events and in the Combobox code itself there's this:
onPressed: {
if (!Settings.hasTouchScreen)
popup.toggleShow()
}
onClicked: {
if (Settings.hasTouchScreen)
popup.toggleShow()
}
So it seems like Clicked requires touchscreen to open the popup(which is not present on the desktop, obviously)
This leaves only Pressed to open the popup, but it's being suppressed at the uppermost MouseArea as it is not a composed event and propagateComposedEvents does nothing for it.
The solution could be :
1) to go through your mouse area chain and in each one of them set:
onPressed: {
mouse.accepted = false
}
2) call popup directly in "clicked" handler
ComboBox {
id: cbChapters
MouseArea {
anchors.fill: parent
propagateComposedEvents: true
onClicked: {
cbChapters.__popup.toggleShow()
}
}
}
I'm trying to propagate the MouseArea signals to a parent object, but using MouseEvent as the parameter type causes an import error.
import QtQuick 2.0
import org.kde.plasma.core 2.0
Rectangle {
id: linkRect
signal clicked(MouseEvent mouse)
ToolTipArea {
id: tooltip
anchors.fill: parent
MouseArea {
id: mouseArea
anchors.fill: parent
onClicked: linkRect.clicked(mouse)
}
}
}
Use the var type like this:
signal clicked(var mouse)
Use QtObject instead of MouseEvent.
Also this works for KeyEvent parameter in KeyPressed event.
signal clicked(QtObject mouse)
Note: The QtObject is base of these classes.
I want to show a context menu when right-clicking on Qt5.5 qml TreeView item, but it has clicked signal. How to show a context menu on right click?
TreeView {
id: tree_view
anchors.fill: parent
model: tree_model
headerVisible: false
backgroundVisible: false
TableViewColumn {
role: "display"
}
onClicked: {
console.log("clicked", index)
}
onDoubleClicked: isExpanded(index) ? collapse(index) : expand(index)
}
It's actually quite easy, you just need a MouseArea configured to accept only right click events, and it won't interfere with the mouse handling performed by the TreeView itself:
TreeView {
id: tree_view
anchors.fill: parent
model: tree_model
TableViewColumn {
role: "display"
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.RightButton
onClicked: {
var index = parent.indexAt(mouse.x, mouse.y)
if (index.valid) {
console.log("show context menu for row: " + index.row)
}
}
}
}
Can you simply define your menu somewhere and use the popup method to show it? That method open the menu near to the mouse cursor, so to the right position.
Of course, you have to define your itemDelegate as well and let the event flows out of your item if needed (do not consume it).
The documentation for the clicked signal of a TreeView explicitly refers to the item delegate to consume those events, so I guess this is the intended approach.
I have a custom element called MenuButton:
import QtQuick 1.1
import VPlay 1.0
Image {
property alias text: buttontext.text
property alias mouseArea: area
property alias fontBold: buttontext.font.bold
property alias textSize: buttontext.font.pixelSize
id: button
source: "img/cloudButton.png"
opacity: 1
Text {
id: buttontext
color: "black"
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 50
font.bold: true
}
MouseArea {
id: area
anchors.fill: parent
onPressed: button.opacity = 0.7
onReleased: button.opacity = 1.0
onCanceled: button.opacity = 1.0
}
function doStuff {
// do something here
}
width: 200
height: 60
}
Now, in my main View, I have a Column with 5 of those MenuButtons. And I want to iterate over them and call the function doStuff(). How do I do that?
I tried with column.childAt(i) and stuff like that, nothing worked.
MainView.qml
Rectangle {
width: 480; height: 320
// HERE IS MY PROBLEM, how do I iterate over all my elements in the column?
function update() {
for(var i = 0; i < 5; i++) {
column.childAt(i).doStuff(); // THIS IS WHAT I WANT TO DO
}
}
Column {
id: column
spacing: 5
anchors.centerIn: parent
Repeater {
id: repeater
model: 5
MenuButton {
id: levelbutton
text: "Level " + (modelData+1);
source: "img/cloud4.png"
}
}
}
}
The problem is in my update function in the MainView.qml
I dont know how to iterate over the elements and call the doStuff() function.
You may use Component.onCompleted attached signal, like this:
import QtQuick 1.0
Rectangle {
height: 600
width: 600
Repeater {
model: 5
Item {
Component.onCompleted: console.log('Component ' + index + ' completed!')
}
}
}
However, please consider, that this imperative operation is not good, because it will be called all time after model update. Probably you have problem X and asking how to get Y, that (you think) will solve your X?
From what I've seen in the source code in the QDeclarativePositioners class you cannot access the children elements!
But you could turn the way you call your doStuff() method: when do you want it to be called? After some time has passed (then add a Timer element to your MenuButton), or when a signal occurs? In the latter case, you could use the Connections element and listen to the signal beign emitted in your calling qml file where you use the Column and Repeater.
Cheers, Chris
You can access the children of an element via the children property that exists on all QtObjects. It contains an array of child elements and can be freely acccessed in javascript.
eg. element.children[0].doStuff()
Generally speaking, you should avoid actions that require you to manually iterate over the children. However, if you are attempting to write code that generically calls something on each of the children you do not always have a choice.