I have an application, where I want to apply an animation to a button/ rectangle and after this executes the animation, it should display a dialog. The problem is that when the dialog.visible: true is called on onClicked, the animation is not executed. I have created a test application here are the two qml files:
main.qml
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
FileDialogs
{
id:fileDialog
visible:false
width:800
height:800
//modality: Qt.WindowModal
z:10
}
Rectangle
{
id:rect
width:200
height:200
color:"blue"
Text
{
id:simpleText
text:"This is a button"
font.pixelSize: 20
color:"white"
width:200
height:200
}
states: [
State {
name: "PRESSED"
},
State {
name: "RELEASED"
}
]
transitions: [
Transition {
from: "PRESSED"
to: "RELEASED"
PropertyAnimation { id: selectAnim; target: rect; properties: "scale"; easing.type: Easing.OutBack; easing.amplitude: 3.0; easing.period: 1.5; from: 0; to: 1; duration: 400 }
}
]
MouseArea {
anchors.fill: parent
onPressed:
{
parent.state = "PRESSED"
}
onReleased:
{
parent.state = "RELEASED"
}
onClicked: {
fileDialog.visible=true;
}
}
}
}
FileDialogs.qml, from Qt examples
import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Dialogs 1.1
import QtQuick.Layouts 1.1
import QtQuick.Window 2.0
Item {
width: 580
height: 400
SystemPalette { id: palette }
clip: true
//! [filedialog]
FileDialog {
id: fileDialog
visible: fileDialogVisible.checked
modality: Qt.WindowModal
title: fileDialogSelectFolder.checked ? "Choose a folder" :
(fileDialogSelectMultiple.checked ? "Choose some files" : "Choose a file")
selectExisting: fileDialogSelectExisting.checked
selectMultiple: fileDialogSelectMultiple.checked
selectFolder: fileDialogSelectFolder.checked
nameFilters: [ "Image files (*.png *.jpg)", "All files (*)" ]
selectedNameFilter: "All files (*)"
sidebarVisible: fileDialogSidebarVisible.checked
onAccepted: {
console.log("Accepted: " + fileUrls)
if (fileDialogOpenFiles.checked)
for (var i = 0; i < fileUrls.length; ++i)
Qt.openUrlExternally(fileUrls[i])
}
onRejected: { console.log("Rejected") }
}
//! [filedialog]
ScrollView {
id: scrollView
anchors {
left: parent.left
right: parent.right
top: parent.top
bottom: bottomBar.top
leftMargin: 12
}
ColumnLayout {
spacing: 8
Item { Layout.preferredHeight: 4 } // padding
Label {
font.bold: true
text: "File dialog properties:"
}
CheckBox {
id: fileDialogModal
text: "Modal"
checked: true
Binding on checked { value: fileDialog.modality != Qt.NonModal }
}
CheckBox {
id: fileDialogSelectFolder
text: "Select Folder"
Binding on checked { value: fileDialog.selectFolder }
}
CheckBox {
id: fileDialogSelectExisting
text: "Select Existing Files"
checked: true
Binding on checked { value: fileDialog.selectExisting }
}
CheckBox {
id: fileDialogSelectMultiple
text: "Select Multiple Files"
Binding on checked { value: fileDialog.selectMultiple }
}
CheckBox {
id: fileDialogOpenFiles
text: "Open Files After Accepting"
}
CheckBox {
id: fileDialogSidebarVisible
text: "Show Sidebar"
checked: fileDialog.sidebarVisible
Binding on checked { value: fileDialog.sidebarVisible }
}
CheckBox {
id: fileDialogVisible
text: "Visible"
Binding on checked { value: fileDialog.visible }
}
Label {
text: "<b>current view folder:</b> " + fileDialog.folder
}
Label {
text: "<b>name filters:</b> {" + fileDialog.nameFilters + "}"
}
Label {
text: "<b>current filter:</b>" + fileDialog.selectedNameFilter
}
Label {
text: "<b>chosen files:</b> " + fileDialog.fileUrls
}
Label {
text: "<b>chosen single path:</b> " + fileDialog.fileUrl
}
}
}
Rectangle {
id: bottomBar
anchors {
left: parent.left
right: parent.right
bottom: parent.bottom
}
height: buttonRow.height * 1.2
color: Qt.darker(palette.window, 1.1)
border.color: Qt.darker(palette.window, 1.3)
Row {
id: buttonRow
spacing: 6
anchors.verticalCenter: parent.verticalCenter
anchors.left: parent.left
anchors.leftMargin: 12
height: implicitHeight
width: parent.width
Button {
text: "Open"
anchors.verticalCenter: parent.verticalCenter
onClicked: fileDialog.open()
}
Button {
text: "Pictures"
tooltip: "go to my Pictures directory"
anchors.verticalCenter: parent.verticalCenter
enabled: fileDialog.shortcuts.hasOwnProperty("pictures")
onClicked: fileDialog.folder = fileDialog.shortcuts.pictures
}
Button {
text: "Home"
tooltip: "go to my home directory"
anchors.verticalCenter: parent.verticalCenter
enabled: fileDialog.shortcuts.hasOwnProperty("home")
onClicked: fileDialog.folder = fileDialog.shortcuts.home
}
}
}
}
The animation is happening. It's just the FileDialog is covering the button before you can see it animate. Release the mouse click outside the button area and you'll see the animation (this way onClicked is not triggered).
A way to show the dialog after the animation:
states: [
State {
name: "PRESSED"
},
State {
name: "RELEASED"
}
]
transitions: [
Transition {
from: "PRESSED"
to: "RELEASED"
PropertyAnimation { id: selectAnim; target: rect; properties: "scale"; easing.type: Easing.OutBack; easing.amplitude: 3.0; easing.period: 1.5; from: 0; to: 1; duration: 400 }
onRunningChanged: {
if (!running) {
fileDialog.visible = true
}
}
}
]
MouseArea {
anchors.fill: parent
onPressed:
{
parent.state = "PRESSED"
}
onClicked: {
parent.state = "RELEASED"
}
}
Related
I think I'm missing something here, but I can't figure out how to set a AudioOutput device from MediaDevices audioOutputs list with the QML Components
audioOutput: AudioOutput {
id: playerOutput
device: mediaDevices.defaultAudioOutput
onDeviceChanged: {
console.log("Output device changed " + device)
}
Component.onCompleted: {
console.log(mediaDevices.audioOutputs)
}
}
MediaDevices {
id: mediaDevices
}
The 2 devices are showing:
QAudioDevice(2, Built in earpiece (IN2023), false, Output),
QAudioDevice(3, Built in speaker (IN2023), false, Output)
but I don't understand how to change it, I've tried playerOutput.setDevice() description and id but it wants a QAudioDevice (it only provides description, id and mode, the documentation for both is very vague:
This property can be used to select an output device from the
QtMultimedia::MediaDevices::audioOutputs() list.
https://doc.qt.io/qt-6/qml-qtmultimedia-mediadevices.html
https://doc.qt.io/qt-6/qml-qtmultimedia-audiooutput.html#device-prop
Any help would be appreciated!
Thanks
You can assign an object from mediaDevices.audioOutputs to the devices property of your AudioOutput.
playerOutput.device = mediaDevices.audioOutputs[index]
where index is a valid index in the audioOutputs list.
import QtQuick
import QtQuick.Window
import QtMultimedia
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello AudioOutput")
MediaDevices {
id: mediaDevices
}
MediaPlayer {
id: mediaPlayer
source: "qrc:/Test_ogg_mp3_48kbps.wav"
audioOutput: AudioOutput {
id: playerOutput
device: mediaDevices.defaultAudioOutput
onDeviceChanged: { console.log("Output device changed " + device) }
Component.onCompleted: { console.log(mediaDevices.audioOutputs) }
}
}
component MediaPlayerButton : Rectangle {
id: button
property alias text: label.text
property bool active: false
signal clicked
width: 100; height: 40; radius: 10
color: button.active ? "tomato" : "ghostwhite"
border.color: "gainsboro"
border.width: buttonMouseArea.containsMouse ? 4 : 2
Text {
id: label
anchors.centerIn: parent
font.pointSize: 18
}
MouseArea {
id: buttonMouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: button.clicked()
}
}
Column {
spacing: 10
anchors.centerIn: parent
Row {
spacing: 10
MediaPlayerButton {
text: qsTr("Play")
active: mediaPlayer.playbackState === MediaPlayer.PlayingState
onClicked: { mediaPlayer.play() }
}
MediaPlayerButton {
text: qsTr("Pause")
active: mediaPlayer.playbackState === MediaPlayer.PausedState
onClicked: { mediaPlayer.pause() }
}
MediaPlayerButton {
text: qsTr("Stop")
active: mediaPlayer.playbackState === MediaPlayer.StoppedState
onClicked: { mediaPlayer.stop() }
}
}
Repeater {
model: mediaDevices.audioOutputs
Row {
spacing: 10
Rectangle {
id: r
width: 40; height: 40; radius: 20
color: mediaPlayer.audioOutput.device === mediaDevices.audioOutputs[index] ? "tomato" : "ghostwhite"
border.color: "gainsboro"
border.width: jackMouseArea.containsMouse ? 4 : 1
MouseArea {
id: jackMouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: playerOutput.device = mediaDevices.audioOutputs[index]
}
}
Text {
anchors.verticalCenter: r.verticalCenter
font.pointSize: 12
text: mediaDevices.audioOutputs[index].description
}
}
}
}
}
I have a problem with the usage qml components from another qml file. I have 2 files, main.qml and menu.qml (menu.qml is in another folder called menu). In the menu.qml I have my menu, which I want to use in main.qml
import QtQuick 2.9
import QtQuick.Controls 2.4
Menu {
id: routingMenu
width: maximumWidth
height: 200
y: 20
cascade: true
Rectangle {
Label {
x: (app_window.width / 8)
text: "FROM:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 7
text: "Country:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 2.5
text: "City:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 1.6
text: "Street:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 1.2
text: "Postal Code:"
font.pixelSize: 22
font.italic: true
color: "black"
}
} //Rectangle
Rectangle {
x: app_window.width / 2
Label {
x: (app_window.width / 4)
text: "TO:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 7
text: "Country:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 2.5
text: "City:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 1.6
text: "Street:"
font.pixelSize: 22
font.italic: true
color: "black"
}
Label {
y: routingMenu.height / 1.2
text: "Postal Code:"
font.pixelSize: 22
font.italic: true
color: "black"
}
} //Rectangle
Button {
id: sendDataToItem
width: 100
height: 40
x: app_window.width / 2.7
text: "Send"
onClicked: {
//fromAddress.country: "";
//fromAddress.city: "";
//fromAddress.street: "";
//fromAddress.postalCode: "";
//toAddress.country: "";
//toAddress.city: "";
//toAddress.street: "";
//toAddress.postalCode: "";
}
} //sendDataToItem
} //Menu
I tried to use a Component, but it didn't helped me. That is code of my main.qml
import QtQuick 2.10
import QtQuick.Controls 2.2
import QtQuick.Window 2.0
import QtLocation 5.6
import QtPositioning 5.6
import "menu"
ApplicationWindow {
id: app_window
visible: true
width: maximumWidth
height: maximumHeight
title: qsTr("Navigation")
PositionSource {
active: true
onPositionChanged: {
//map_id.center = position.coordinate;
}
}
Plugin {
id: mapPlugin_
name: "osm"
}
/*Loader {
id: loadered
focus: true
source: "menu/menu.qml"
active: true
}*/
Rectangle {
id: mapRectangleID
width: 800
height: 800
x:0
y:20
Map {
id: map_
anchors.fill: parent
plugin: mapPlugin_
center: QtPositioning.coordinate(51.320729390711975,12.280097007751465)
zoomLevel: 15
MapQuickItem {
id: marker_id
coordinate: QtPositioning.coordinate(59.91, 10.75)
sourceItem: Image {
id: endPointImage
source: "assets/marker.png"
width: 100
height: 100
} //size and position of maker
anchorPoint.x: endPointImage.width / 2
anchorPoint.y: endPointImage.height
} //marker
RouteModel {
id: routeBetweenPoints
plugin: Plugin { name: "osm" }
query: RouteQuery {id: routeQuery }
Component.onCompleted: {
routeQuery.addWaypoint(QtPositioning.coordinate(51.318784,12.2773504 ));
routeQuery.addWaypoint(QtPositioning.coordinate(51.3117764,12.280909000000065 ));
//routeQuery.addWaypoint(endPointGeaocodeModel)
update();
}
} //start and end point
MapItemView {
model: routeBetweenPoints
delegate: Component {
MapRoute {
route: routeData
line.color: "red"
line.width: 10
}
} //Component
}//linie, die beide punkte verbindet
GeocodeModel{
id: endPointGeaocodeModel
plugin: Plugin { name: "osm" }
query: "Sandakerveien 116, Oslo"
onLocationsChanged: {
if (count> 0){
marker_id.coordinate = get(0).coordinate
map_id.center = get(0).coordinate
}
}
Component.onCompleted: update()
} //suche den platz mit strasse und stadt
//! [geocode0]
Address {
id: fromAddress
city: ""
country: ""
street: ""
postalCode: ""
} //fromAddress
//! [geocode0]
Address {
id: toAddress
country: ""
city: ""
street: ""
postalCode: ""
} //toAddress
} //Map
} //mapRectangleID
Button {
id: btn_close
width: 100
height: 20
text: "Close"
onClicked: {
Qt.quit();
}
}
Button {
id: btn_routing
width: 100
height: 20
x:100
text: "Routing"
onClicked: {
routingMenu.open();
}
}
Button {
id: btn_oldWay
width: 100
height: 20
x:200
text: "Old way"
onClicked: {
oldWayMenu.open();
}
}
Button {
id: rest
width: parent.width - x
height: 20
x:300
text: ""
onClicked: {
}
}
} //ApplicationWindow
I tried 2 ways to resolve the problem. One with the Loader, but I can't open my menu, and the another with example of QT which is in MapViewer Example. They import only the order and use the id of the component. But it doesn't work. I think, I do something wrong in my code, can someone tell me what I'm doing wrong or show me the right way to resolve my problem.
I think that the most important part of the code is:
Loader {
id: loadered
focus: true
source: "menu/menu.qml"
active: true
}
And then, how I'm using it:
Button {
id: btn_routing
width: 100
height: 20
x:100
text: "Routing"
onClicked: {
routingMenu.open();
}
}
By the button I'm using the component name, because I imported menu. But if I want to use the loader, I would set the visible in loader as false and then set onClicked as true. But I tried this and it didn't work.
Thanks for help
DC
The id is only valid within the .qml outside it has no meaning, for example let's say you're going to create 4 menus then if you would use the id routingMenu, which of the menus would you refer to? because that would cause confusion. So if you want to use the item loaded by Loader you must do it using the item property.
Loader {
id: loadered
focus: true
source: "menu/menu.qml"
active: true
}
// ...
Button {
id: btn_routing
width: 100
height: 20
x:100
text: "Routing"
onClicked: loadered.item.open()
}
I am working on qml, found found an error during moving mouse wheel scrolling i.e.
"TypeError: Cannot read property 'name' of undefined".
It only appear when either mouse wheel scrolling or close the application while during execution and beginning of the application didn't show this error.
One more thing, I am getting accurate data/result whatever I want but due to this error the performance of application get affected.
Please let me know how can I get rid of this issue?
also find below example to see error.
My code is..
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
TableView {
anchors.fill: parent
visible: true
backgroundVisible: false
alternatingRowColors: false
sortIndicatorVisible: true
clip: true
highlightOnFocus: false
width: parent.width
height: parent.height
TableViewColumn{ role: "code" ; title: "cde" ; width: 200;
delegate: Component {
id: codeDelegate
Item{
Text {
color: "lightgray"
elide: styleData.elideMode
text: styleData.value //modelSettingData.get(styleData.row).name
font.family: "Arial"
font.pixelSize: 18
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
Text {
id: metaData
color: "red"
width: parent.width
// height: 20
anchors.bottom: parent.bottom
anchors.bottomMargin: -parent.height/1.5
font.weight: Font.ExtraLight
//elide: Text.ElideMiddle
text: "Time: " + modelSettingData.get(styleData.row).name //Error comes when this part added but I need to use it with the same form
}
}
}
}
}
TableViewColumn{ role: "name" ; title: "nme" ; width: 200
delegate: Component {
id: nameDelegate
Text {
color: "yellow"
elide: styleData.elideMode
text: styleData.value
font.family: "Arial"
font.pixelSize: 18
}
}
}
model: ListModel {
id: modelSettingData
ListElement {
name: "aaaaaaaaaa"
code: "3042666666666"
}
ListElement {
name: "bbbbbb"
code: "32235"
}
ListElement {
name: "ccccccc"
code: "32638"
}
ListElement {
name: "ddddddddddd"
code: "00000000000"
}
ListElement {
name: "eeeeeeeeeeee"
code: "111111111111"
}
ListElement {
name: "ffffffffffff"
code: "222222222222"
}
ListElement {
name: "ggggggggggggg"
code: "3333333333333"
}
ListElement {
name: "hhhhhhhhhhhhh"
code: "4444444444444"
}
ListElement {
name: "iiiiiiiiiiiii"
code: "5555555555555"
}
}
rowDelegate: Rectangle {
property bool selected : styleData.selected
width: parent.width-2
height: 100
color: styleData.selected? "black" : "black"
Rectangle {
width: parent.width
height: 1
anchors.bottom: parent.bottom
visible: parent.selected
color: "yellow"
}
}
}
}
I have a ListView that has many rows , each row has few Items, i want to select one row by up and down keys and then select highlighted row by space or enter key and navigate on its items from first button by up and down keys, how is it possible?
here is my code:
import QtQuick 2.6
import QtQuick.Controls 1.5
import QtQuick.Controls.Styles 1.4
ApplicationWindow {
visible: true
width: 400
height: 480
title: qsTr("Hello World")
Rectangle {
anchors.fill: parent
ListModel
{
id:myModel
ListElement
{
text1:"btn1"
text2:"btn2"
text3:"btn3"
}
ListElement
{
text1:"btn1"
text2:"btn2"
text3:"btn3"
}
ListElement
{
text1:"btn1"
text2:"btn2"
text3:"btn3"
}
}
ListView {
id: list
anchors.fill: parent;
model: myModel
currentIndex: 0
focus: true
delegate: Rectangle {
id: delegateItem
height: 100
width: parent.width;
color: "blue"
Row{
anchors.fill: parent
Button
{
text: model.text1
height: parent.height
onFocusChanged:
{
if(focus)
text="selected"
else
text= model.text1
}
}
Button
{
text: model.text1
height: parent.height
onFocusChanged:
{
if(focus)
text="selected"
else
text= model.text3
}
}
Button
{
text: model.text1
height: parent.height
onFocusChanged:
{
if(focus)
text="selected"
else
text= model.text3
}
}
}
onFocusChanged:
{
if(focus)
delegateItem.color="red"
else
delegateItem.color="blue"
}
}
Keys.onDownPressed: {
if (list.currentIndex + 1 < list.count )
list.currentIndex += 1;
}
Keys.onUpPressed: {
if (list.currentIndex >= 0)
list.currentIndex -= 1;
}
Keys.onEnterPressed:
{
list.currentItem.forceActiveFocus()
}
}
}
}
Using your example, you can select rows by navigating with the arrow keys, and give focus to an individual button by pressing tab until you get to the one you're interested in. Space activates a button once it has focus.
Comparing your answer with the information you added in your third edit, the cleaner way would be to give each button an id and refer to that. That is, rather than do this:
KeyNavigation.down: list.currentItem.children[0].children[1]
which is kinda fragile and difficult to read, do this:
KeyNavigation.down: button2
For navigating on first button of selected row, you should use list.currentItem.children here is modified codes
import QtQuick 2.6
import QtQuick.Controls 1.5
ApplicationWindow {
visible: true
width: 400
height: 480
title: qsTr("Hello World")
Rectangle {
anchors.fill: parent
ListModel {
id: myModel
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
ListElement {
text1: "btn1"
text2: "btn2"
text3: "btn3"
}
}
ListView {
id: list
anchors.fill: parent
model: myModel
currentIndex: 0
focus: true
delegate: Rectangle {
id: delegateItem
height: 100
width: parent.width
color: "blue"
Row {
anchors.fill: parent
Button {
text: model.text1
height: parent.height
onFocusChanged: {
if (focus)
text = "selected"
else
text = model.text1
}
KeyNavigation.down: list.currentItem.children[0].children[1]
}
Button {
text: model.text1
height: parent.height
onFocusChanged: {
if (focus)
text = "selected"
else
text = model.text3
}
KeyNavigation.down: list.currentItem.children[0].children[2]
KeyNavigation.up: list.currentItem.children[0].children[0]
}
Button {
text: model.text1
height: parent.height
onFocusChanged: {
if (focus)
text = "selected"
else
text = model.text3
}
KeyNavigation.up: list.currentItem.children[0].children[1]
}
}
onFocusChanged: {
if (focus)
delegateItem.color = "red"
else
delegateItem.color = "blue"
console.log(list.currentItem.children[0].children[0].focus)
}
Keys.onDownPressed:
{
if (list.currentIndex + 1 < list.count)
list.currentIndex += 1
}
Keys.onUpPressed:
{
if (list.currentIndex -1 > -1)
list.currentIndex -= 1
}
Keys.onSpacePressed: {
var focus=list.currentItem.children[0].children[children_index].focus
list.currentItem.children[0].children[children_index].focus
= !focus
if(focus)
list.currentItem.focus=true
console.log("entered: "+list.currentItem.children[0].children[0].focus)
}
property int children_index: 0
}
}
}
}
I want connect one signal from QObject to various pages, loaded by the "Loader" qml element. My problem similar Dead QML elements receiving signals? but loaded items destroyed before calling the "onDestruction" method.
For example below, if switch from page1 to page2 in console writed:
"QML: Loading status: 1 Item: QDeclarativeRectangle(0x8dcd408, "page2")
QML Item: Loaded QDeclarativeRectangle(0x8dcd408, "page2") 1
qrc:/page1.qml:12: TypeError: Result of expression 'parent' [null] is not an object.
qrc:/page1.qml:15: ReferenceError: Can't find variable: page1text"
every second. So there can't disconnect from signal because parent object is destroyed.
How to handle signals from QObject (root) in loaded items? or How to disconnect signal from unloaded page?
main.qml
import QtQuick 1.1
Rectangle {
id: root
objectName: "root"
width: 360
height: 360
state: "page1"
color: "white"
Item {
width: parent.width
height: parent.height/2
anchors.top: parent.top
Loader {
id: pageLoader
objectName: "pageLoader"
anchors.fill: parent
anchors.centerIn: parent
signal textMsg(variant params)
onStatusChanged: console.log("QML: Loading status: ", status, " Item: ", item)
onLoaded: { console.log("QML Item: Loaded",item,status); }
}
}
states: [
State {
name: "page1"
PropertyChanges { target: pageLoader; source: "qrc:/page1.qml"}
}
,State {
name: "page2"
PropertyChanges { target: pageLoader; source: "qrc:/page2.qml"}
}
]
Timer {
// simulate signals from QObject
interval: 1000; running: true; repeat: true
onTriggered: pageLoader.textMsg({"msg2page1":"test","msg2page2":"test"})
}
Rectangle {
anchors.left: parent.left
anchors.bottom: parent.bottom
width: parent.width/2
height: parent.height/2
border {
color: "black"
width: 1
}
color: "yellow"
Text{
anchors.fill: parent
anchors.centerIn: parent
text: "Set Page 1"
}
MouseArea {
anchors.fill: parent
onClicked: {
root.state = "page1";
}
}
}
Rectangle {
anchors.right: parent.right
anchors.bottom: parent.bottom
width: parent.width/2
height: parent.height/2
border {
color: "black"
width: 1
}
color: "red"
Text{
anchors.fill: parent
anchors.centerIn: parent
text: "Set Page 2"
}
MouseArea {
anchors.fill: parent
onClicked: {
root.state = "page2";
}
}
}
}
page1.qml
import QtQuick 1.1
Rectangle {
id: page1
objectName: "page1"
color: "yellow"
Component.onCompleted: {
parent.textMsg.connect(msgHandler);
}
Component.onDestruction: {
parent.textMsg.disconnect(msgHandler);
}
function msgHandler(params) {
page1text.text += " "+params.msg2page1;
}
Text {
id: page1text
anchors.fill: parent
wrapMode: Text.WordWrap
text: "page1"
}
}
page2.qml
import QtQuick 1.1
Rectangle {
id: page2
objectName: "page2"
color: "red"
}
That's nicely described in Loader documenation. It reads:
Any signals emitted from the loaded item can be received using the Connections element.
There is also an example, I copy it below for the sake of clarity:
// Application.qml
import QtQuick 1.0
Item {
width: 100; height: 100
Loader {
id: myLoader
source: "MyItem.qml"
}
Connections {
target: myLoader.item
onMessage: console.log(msg)
}
}
// MyItem.qml
import QtQuick 1.0
Rectangle {
id: myItem
signal message(string msg)
width: 100; height: 100
MouseArea {
anchors.fill: parent
onClicked: myItem.message("clicked!")
}
}
Clearly, if item is destroyed, any signal handlers are ignored until the target is recreated again.
My answer is: Don't use the "Loader", create child object by JS and destroy it as no needed, for example:
main.qml
import QtQuick 1.1
import "qrc:/pageloader.js" as Pageloader
Rectangle {
id: root
objectName: "root"
width: 360
height: 360
state: "page1"
color: "white"
signal textMsg (variant params)
states: [
State {
name: "page1"
StateChangeScript{ script: Pageloader.createPageObject("qrc:/page1.qml");}
}
,State {
name: "page2"
StateChangeScript{ script: Pageloader.createPageObject("qrc:/page2.qml");}
}
]
Timer {
// simulate signals from QObject
interval: 1000; running: true; repeat: true
onTriggered: textMsg({"msg2page1":"test","msg2page2":"test"})
}
Rectangle {
anchors.left: parent.left
anchors.bottom: parent.bottom
width: parent.width/2
height: parent.height/2
border {
color: "black"
width: 1
}
color: "yellow"
Text{
anchors.fill: parent
anchors.centerIn: parent
text: "Set Page 1"
}
MouseArea {
anchors.fill: parent
onClicked: {
root.state = "page1";
}
}
}
Rectangle {
anchors.right: parent.right
anchors.bottom: parent.bottom
width: parent.width/2
height: parent.height/2
border {
color: "black"
width: 1
}
color: "red"
Text{
anchors.fill: parent
anchors.centerIn: parent
text: "Set Page 2"
}
MouseArea {
anchors.fill: parent
onClicked: {
root.state = "page2";
}
}
}
}
pageloader.js
var component;
var sprite;
function createPageObject(path) {
if(sprite){
console.log("sprite.destroy() ",typeof sprite);
sprite.destroy();
console.log("component.destroy() ",typeof component);
component.destroy();
}
component = Qt.createComponent(path);
if (component.status === Component.Ready)
finishCreation();
else
component.statusChanged.connect(finishCreation);
}
function finishCreation() {
if (component.status == Component.Ready) {
sprite = component.createObject(root);
if (sprite == null) {
// Error Handling
console.log("Error creating object");
}
} else{
if (component.status === Component.Error) {
// Error Handling
console.log("Error loading component:", component.errorString());
}else{
console.log("Component status changed:", component.status);
}
}
}
page1.qml and page2.qml not changed.
I got it. My setup:
qml file to display ListViews
Several qml files defining Listviews, each takes a different column of different SQL tables. The model comes from C++
So here is the shortened code:
Dialog {
id: dialog
width: 1000; height: 400
property Component listViewItem
signal newDatabaseEntry( string text ) [1]
contentItem: Rectangle {
[...]
TextInputWithButton { [3]
id: newRecords
onInputAccepted: { newDatabaseEntry( text ) } [1]
}
}
[...]
Loader {
id: listViewPlaceholder
anchors.fill: parent
sourceComponent: dialog.listViewItem
onLoaded: {
if( typeof listViewPlaceholder.item.insertRecord === "function" )
// newRecords.inputAccepted.connect( listViewPlaceholder.item.insertRecord ) [1]
dialog.newDatabaseEntry.connect( listViewPlaceholder.item.insertRecord ) [2]
}
The above code is the general view of ListViews. The signal roundtrip [1] is necessary, otherwise no data is passed. How to chain signals is described here:
http://doc.qt.io/qt-5/qtqml-syntax-signals.html#connecting-signals-to-methods-and-signals
The input button [3] delivers the confirmed data to be inserted into the db.
A ListView passed to the above function looks like this:
DialogSqlSingleColumnEdit {
listViewItem: ListView {
function insertRecord( text ) {
console.log( "done:" + text )
sqlModel.insertRecord( text )
}
[...]
The insertRecord is called forwards the text to the sql-C++ model.