I recently start to learn Qt and QML for prototyping some UI and experienced some issue.
Basically, I have a Rectangle (id: myItem) in TestB.qml which is considered as a button. I also have another Rectangle (id:changedrect) in TestA.qml.
The functionalities I want to implement is when myItem is clicked, the width and height of changedrect in TestA.qml will change.
Here are the hierarchy of directories
My TestA.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import Qt5Compat.GraphicalEffects
import QtQuick.Shapes 2.15
import QtQml 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 2.15
import QtMultimedia
//import "../TestB" as TestB
import "resizeRect.js" as ResizeRectScript
Item {
id: testA
property alias changeRect: changedrect
/** expose the boolean*/
property bool ifSlected: false
Rectangle{
anchors.fill: parent
id: changedrect
x: 10
y: 10
width:200
height: 200
color:"red"
Component.onCompleted:{
ResizeRectScript.resize(testA.ifSlected,changedrect); // pass boolean flag && id of rect
}
}
// Loader {
// id: myLoader
// source: "../TestB/TestB.qml"
// }
// Connections {
// target: myLoader.item
// function onMessage(msg) { console.log(msg) }
// }
}
resizeRect.js
function resize(selectFlag, rectId){
if(selectFlag = true){
rectId.width = 100;
console.log("yes, resize is excuted");
}
}
TestB.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import Qt5Compat.GraphicalEffects
import QtQuick.Shapes 2.15
import QtQml 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 2.15
import QtMultimedia
import "../TestA" as TestA
Item{
TestA.TestA{
id: testA
}
Rectangle {
id: myItem
signal message()
width: 100; height: 100
color: "pink"
MouseArea {
anchors.fill: parent
onClicked: {
testA.ifSlected = true;
}
}
}
}
And I instantiated(created object) TestA and TestB on test2.qml
test2.qml
import QtQuick 2.15
import QtQuick.Window 2.15
import Qt5Compat.GraphicalEffects
import QtQuick.Shapes 2.15
import QtQml 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 2.15
import QtMultimedia
import "TestA" as TestA
import "TestB" as TestB
Window {
width: Screen.width
height:Screen.height
flags: Qt.FramelessWindowHint | Qt.BypassWindowManagerHint |Qt.WindowStaysOnBottomHint |
Qt.NoDropShadowWindowHint /** hide the native menu bar so that the windows takes over whole screen */
visibility: "FullScreen"
visible: true
color: "black"
TestA.TestA{
id:test_A
}
TestB.TestB{
id: test_B
}
}
The problem is
- the console message is printed, which means the resize() function from js file is excuted(see below), however the size of rectangle in TestA.qml is not updated. I tried to use Signal from Qt documentation, but the examples did not work either.
Here is the terminal output:
Can someone helps me with this? Thank you so much!!