Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Updating a background image
Forum Update on Monday, May 27th 2025

Updating a background image

Scheduled Pinned Locked Moved QML and Qt Quick
1 Posts 1 Posters 720 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mozeni
    wrote on last edited by
    #1

    Hello,

    I'm working on a project in which a part of it is allowing the user to select a background image.

    my main.qml is:
    @import QtQuick 2.0

    Rectangle {
    id: main
    StartScreen{}
    Component.onCompleted: SetUpScript.createSpriteObjects();
    }@

    SetUpScript.createSpriteObjects() makes the other components of my project which in turn run other qml files

    my StartScreen.qml is:
    @import QtQuick 2.0
    import "SetUp.js" as Logic

    Image {
    id: bgImage
    source: Logic.bgSource
    anchors.centerIn: parent
    anchors.fill: parent
    }@

    where Logic.bgSource is a var that holds the url of the default image to be used.
    The default image loads up fine when I run main.
    I then make a list of images from which the user can pick one
    I would've thought that in the MouseArea portion of the code, where Logic.bgSource is changed, the StartScreen{} would also update to the new source?

    The following is in another qml file: SetUpQ2.qml

    @ListView {
    id: listModel
    spacing: 2
    snapMode: ListView.SnapToItem
    boundsBehavior: Flickable.StopAtBounds
    anchors.top: instructions.bottom
    clip: true
    x: parent.width0.08; y: parent.height0.27
    width: parent.width0.5; height: parent.height0.75
    FolderListModel {
    folder: "../../layout/backgrounds"
    id: folderModel
    nameFilters: [".jpg"]
    sortField: "Name"
    }
    Component {
    id: fileDelegate
    Rectangle {
    id: imgSelect
    width: listModel.width
    0.4; height: listModel.height*0.35

                Image {
                    id: imgName
                    width: parent.width*0.9; height: parent.height*0.9
                    anchors.centerIn: parent
                    fillMode: Image.Stretch
                    source: folderModel.folder + "/" + fileName
                    onSourceChanged: imageModel.append({name: imgName.source})
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            Logic.bgSource = imgName.source
                            console.log(Logic.bgSource)
                        }
                    }
                }
            }
        }
        model: folderModel
        delegate: fileDelegate
        ListModel {
            id: imageModel
        }
        Rectangle {
            id: scrollbar
            x: listModel.width*0.385
            y: listModel.visibleArea.yPosition * listModel.height
            radius: 7
            width: 10
            height: listModel.visibleArea.heightRatio * listModel.height
            opacity: 0.7
            color: "black"
        }
    }@
    

    Any help or clarifications would be awesome!
    Thanks

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved