【发布时间】:2018-11-25 09:36:17
【问题描述】:
我正在寻找一种逻辑,以便根据一些不同的条件更改图像源属性。
我创建了一个小示例,其中包含三个条件和三个由这些条件构建的要求。
如果某个需求处于活动状态,我想在图像区域中查看它的图片。 如果一个需求不活跃,我不想在图像区域看到它的图像。 如果两个需求同时处于活动状态,则可以只查看其中一个。
我的代码有问题,因为如果一个需求处于非活动状态,而另一个需求同时处于活动状态,最后一个需求将控制内容。但我希望显示图像比不显示图像具有更高的优先级。
有没有常见的方法来解决这个问题?
我有以下代码:
main.qml:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
property bool condition1: false
property bool condition2: false
property bool condition3: false
Image{
id: image
anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter
width: 120
height: 120
source: ""
}
Row {
anchors.top: image.bottom
anchors.horizontalCenter: parent.horizontalCenter
Button {
id: button1
text: "Condition1 = " + condition1
onClicked: condition1 = !condition1
}
Button {
id: button2
text: "Condition2 = " + condition2
onClicked: condition2 = !condition2
}
Button {
id: button3
text: "Condition3 = " + condition3
onClicked: condition3 = !condition3
}
}
StateObserver{
condition: condition1 && condition2 && condition3
bitmap: "Images/1.png"
target: image
}
StateObserver{
condition: !condition1 && condition2 && condition3
bitmap: "Images/2.png"
target: image
}
StateObserver{
condition: !condition1 && !condition2 && condition3
bitmap: "Images/3.png"
target: image
}
}
StateObserver.qml
import QtQuick 2.0
Item {
property bool condition
property string bitmap
property var target
onConditionChanged: {
if (condition){
target.source = bitmap
}
else {
target.source = ""
}
console.log("Change bitmap: " + bitmap)
}
}
【问题讨论】:
标签: qt qml logic conditional-statements