Problem with using .qml files in .cpp file



  • I have just tried using two .qml files in .cpp file, but it doesn't work.
    Can you help me how to merge the two .qml files so it can work and be used in .cpp file?



  • Can you show your code and detalize your problem?



  • => 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.Vertical

    Rectangle {
        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 ?



  • Have you added ScrollBar.qml to your resource file too?
    There has to be an error or warning message that ScrollBar.qml couldn't be resolved. Which path does it contain?



  • Yeah, I have added ScrollBar.qml to my resource file.
    in the resource file, I added ScrollBar.qml and DetailMap.qml.
    But, when I run the app in the simulator, there is error "qrc:/new/prefix1/DetailMap.qml:2:1: "scrollbar": no such directory"



  • 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?



  • you can use applicationviewer from qt qml.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.