Problem with using .qml files in .cpp file
-
=> Vass :
I have put qml project into qrc...
and then I use this code...
@this->view->setSource(QUrl(QString::fromUtf8("qrc:/new/prefix1/scrollbar/scrollbar.qmlproject")));@Beside that, I try to use two .qml files in .cpp file :
=> in .cpp file :
@this->view->setSource(QUrl(QString::fromUtf8("qrc:/new/prefix1/DetailMap.qml")));@
and then
this is in DetailMap.qml that use item in ScrollBar.qml@...
ScrollBar {
id: verticalScrollBar
width: 12; height: view.height-12
anchors.right: view.right
opacity: 0
orientation: Qt.Vertical
position: view.visibleArea.yPosition
pageSize: view.visibleArea.heightRatio
}ScrollBar { id: horizontalScrollBar width: view.width-12; height: 12 anchors.bottom: view.bottom opacity: 0 orientation: Qt.Horizontal position: view.visibleArea.xPosition pageSize: view.visibleArea.widthRatio }
}@
in ScrollBar.qml
@...
Item {
id: scrollBar
property real position
property real pageSize
property variant orientation : Qt.VerticalRectangle { id: background anchors.fill: parent radius: orientation == Qt.Vertical ? (width/2 - 1) : (height/2 - 1) color: "white" opacity: 0.3 } // Size the bar to the required size, depending upon the orientation. Rectangle { x: orientation == Qt.Vertical ? 1 : (scrollBar.position * (scrollBar.width-2) + 1) y: orientation == Qt.Vertical ? (scrollBar.position * (scrollBar.height-2) + 1) : 1 width: orientation == Qt.Vertical ? (parent.width-2) : (scrollBar.pageSize * (scrollBar.width-2)) height: orientation == Qt.Vertical ? (scrollBar.pageSize * (scrollBar.height-2)) : (parent.height-2) radius: orientation == Qt.Vertical ? (width/2 - 1) : (height/2 - 1) color: "black" opacity: 0.7 }
}
@ -
I don't think you can pass a .qmlproject file to QDeclarativeView::setSource(), just .qml files. In addition, you cannot pass multiple files to QDeclarativeView::setSource(). As soon as you set a new source the old one is discarded.
However, the declarative engine automatically loads any referenced QML elements, independent of they are local or in resources files. So if you set DetailMap.qml as your source and you create a ScrollBar element in it, the engine automatically looks for a ScrollBar.qml file at the location DetailMap.qml was loaded from and creates your object.
-
I have just tried to set DetailMap.qml as my source and then in the rectangle, I put in ScrollBar element like this :
@
//...
ScrollBar {
id: verticalScrollBar
width: 12; height: view.height-12
anchors.right: view.right
opacity: 0
orientation: Qt.Vertical
position: view.visibleArea.yPosition
pageSize: view.visibleArea.heightRatio
}ScrollBar { id: horizontalScrollBar width: view.width-12; height: 12 anchors.bottom: view.bottom opacity: 0 orientation: Qt.Horizontal position: view.visibleArea.xPosition pageSize: view.visibleArea.widthRatio }
}@
and then I put ScrollBar.qml in the same directory with DetailMap.qml.
But it doesn't work. Should I put something in the import ? -
I have a similar problem, except I'm not using resources. I'm loading a QML source from the file system, but if it references other QML files it fails to load.
Any idea?