Hi,
I'm developing a little app where the user can change the design (background and button image).
My QML component 'Button' looks like that:
// Button.qml
import QtQuick 2.4
Rectangle
{
id: button
property alias image: image
property alias mousearea: mousearea
property alias text: text.text
width: 90
height: 30
color: "transparent"
BorderImage
{
id: image
source: mousearea.pressed ? "qrc:/img/buttons/cyan_hover.png" : "qrc:/img/buttons/cyan.png"
width: parent.width
height: parent.height
border { left: 25; top: 25; right: 25; bottom: 25 }
horizontalTileMode: BorderImage.Stretch
verticalTileMode: BorderImage.Round
}
MouseArea
{
id: mousearea
anchors.fill: parent
acceptedButtons: Qt.LeftButton
cursorShape: "PointingHandCursor"
}
Text
{
id: text
anchors.centerIn: parent
text: "Button"
color: "white"
font.bold: true
}
}
// Button.qml
import QtQuick 2.4
Rectangle
{
id: button
property alias image: image
property alias mousearea: mousearea
property alias text: text.text
width: 90
height: 30
color: "transparent"
BorderImage
{
id: image
source: mousearea.pressed ? "qrc:/img/buttons/cyan_hover.png" : "qrc:/img/buttons/cyan.png"
width: parent.width
height: parent.height
border { left: 25; top: 25; right: 25; bottom: 25 }
horizontalTileMode: BorderImage.Stretch
verticalTileMode: BorderImage.Round
}
MouseArea
{
id: mousearea
anchors.fill: parent
acceptedButtons: Qt.LeftButton
cursorShape: "PointingHandCursor"
}
Text
{
id: text
anchors.centerIn: parent
text: "Button"
color: "white"
font.bold: true
}
}
To copy to clipboard, switch view to plain text mode
It has a pre-defined background image (color: cyan) and a pressed-effect (background changes to cyan_hover).
In my main QML file, I create about 15 instances of those buttons ("Button black", "Button gray", "Button brown", "Button blue", "Button cyan" and so on...).
When the user clicks on one of those buttons, every button image should change to the image that was selected, example:
User clicks button "Button black" --> background image of every button should change to "qrc:/img/buttons/black.png".
This is how I create those instances:
//main.qml
import QtQuick 2.4
import QtQuick.Window 2.2
Window
{
id: window
visible: true
width: 350
height: 500
Rectangle
{
id: rectangleLayout
anchors.fill: parent
Image
{
id: imageBackground
source: "qrc:/img/backgrounds/cyan.jpg"
width: parent.width
height: parent.height
fillMode: Image.Stretch
}
Button
{
id: buttonDesignBlack
anchors.top: rectangleHeader.bottom
anchors.left: rectangleLayout.left
anchors.topMargin: 30
anchors.leftMargin: 10
anchors.horizontalCenter: rectangleLayout.horizontalCenter
height: 50
mousearea.onClicked:
{
// there I have to put the code to change design
}
text: "Button black"
}
// another 14 instances of 'Button'
}
//main.qml
import QtQuick 2.4
import QtQuick.Window 2.2
Window
{
id: window
visible: true
width: 350
height: 500
Rectangle
{
id: rectangleLayout
anchors.fill: parent
Image
{
id: imageBackground
source: "qrc:/img/backgrounds/cyan.jpg"
width: parent.width
height: parent.height
fillMode: Image.Stretch
}
Button
{
id: buttonDesignBlack
anchors.top: rectangleHeader.bottom
anchors.left: rectangleLayout.left
anchors.topMargin: 30
anchors.leftMargin: 10
anchors.horizontalCenter: rectangleLayout.horizontalCenter
height: 50
mousearea.onClicked:
{
// there I have to put the code to change design
}
text: "Button black"
}
// another 14 instances of 'Button'
}
To copy to clipboard, switch view to plain text mode
I have two questions about that now:
1. Is there a way to change the background image of all 15 instances with one line of code or do I have to change the source property of each instance by hand, like this:
mousearea.onClicked:
{
buttonDesignBlack.image.source = buttonDesignBlack.mousearea.pressed ? "qrc:/img/buttons/black_hover.png" : "qrc:/img/buttons/black.png"
buttonDesignXX.image.source = buttonDesignXX.mousearea.pressed ? "qrc:/img/buttons/black_hover.png" : "qrc:/img/buttons/black.png"
// ...
}
mousearea.onClicked:
{
buttonDesignBlack.image.source = buttonDesignBlack.mousearea.pressed ? "qrc:/img/buttons/black_hover.png" : "qrc:/img/buttons/black.png"
buttonDesignXX.image.source = buttonDesignXX.mousearea.pressed ? "qrc:/img/buttons/black_hover.png" : "qrc:/img/buttons/black.png"
// ...
}
To copy to clipboard, switch view to plain text mode
2. The way I try it above does apply the changes, hence the new background is set correctly, but the pressed event does not change the background anymore. It works without doing anything in the onClicked scope, so I know the error has to be there.
Maybe the problem is, that the pressed event & changing background image is overwritten immediately by the onClicked signal?? But I also tried to press and hold the mouse button down on the button, so the onClicked signal is not yet emitted, but the background doesn't change anyway!
When running the app, the pressed event + background image change works exactly ONE TIME, after that it doesn't work anymore. So I think the problem is that I overwrite something when executing the code in onClicked scope... Does anyone know where my error is?? 
Thank you in anticipation!
Bookmarks