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. Change Image source from c++
Forum Updated to NodeBB v4.3 + New Features

Change Image source from c++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 1.2k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    celica
    wrote on last edited by
    #1

    Hi folks
    I found a neat way of altering QML properties from C++, this code sample can change the color property of a rectangle on the fly.
    My question is how do you change the source of an image in the same way for the below QML extract.
    Adopting the same method doesn't quite work generating run-time errors.

    C++
    QQuickItem* objectimage = quickView->rootObject();
    QObject *im = objectimage->findChild<QObject*>("rect");
    im->setProperty("color", "red");
    
    QML
    
    Image 
    {
    objectName: "ima"
    id:pic
    source: "bgnight.png"
    
    Rectangle
    {	 
    objectName: "rect"
    id:redRect1
    width: 100
    height: 100
    x: 130
    y:230
    color:"transparent"
    
    GrecKoG 1 Reply Last reply
    0
    • C celica

      Hi folks
      I found a neat way of altering QML properties from C++, this code sample can change the color property of a rectangle on the fly.
      My question is how do you change the source of an image in the same way for the below QML extract.
      Adopting the same method doesn't quite work generating run-time errors.

      C++
      QQuickItem* objectimage = quickView->rootObject();
      QObject *im = objectimage->findChild<QObject*>("rect");
      im->setProperty("color", "red");
      
      QML
      
      Image 
      {
      objectName: "ima"
      id:pic
      source: "bgnight.png"
      
      Rectangle
      {	 
      objectName: "rect"
      id:redRect1
      width: 100
      height: 100
      x: 130
      y:230
      color:"transparent"
      
      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @celica said in Change Image source from c++:

      I found a neat way of altering QML properties from C++, this code sample can change the color property of a rectangle on the fly.

      C++
      QQuickItem* objectimage = quickView->rootObject();
      QObject *im = objectimage->findChild<QObject*>("rect");
      im->setProperty("color", "red");
      

      That's not a good practice, pasting some links so I don't paraphrase them:
      https://doc.qt.io/qt-5/qtquick-bestpractices.html#interacting-with-qml-from-c
      http://doc.qt.io/qt-5/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
      https://youtu.be/vzs5VPTf4QQ?t=23m20s

      1 Reply Last reply
      3
      • J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        Let me expand @GrecKo 's links, in case this is what you actually want to do:

        https://doc.qt.io/qt-5/qquickimageprovider.html
        https://doc.qt.io/qt-5/qtquick-imageprovider-example.html


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        C 1 Reply Last reply
        1
        • rrlopezR Offline
          rrlopezR Offline
          rrlopez
          wrote on last edited by
          #4

          You can use this in conjunction with the answer from @GrecKo :
          https://doc.qt.io/qt-5/properties.html
          You will create a context property accessible from QML and call for it there. Then on your C++ class you will change the Q_PROPERTY variable and emit the signalChanged from there. On QML, get a property hold this value and every change you do to the variable this way, will reflect instantly on QML.

          Lic-Ing. Rodrigo Lopez Gonzalez
          Embedded Software Engineer
          RidgeRun Engineering Ltd.
          www.ridgerun.com
          Email: rodrigo.lopez@ridgerun.com

          1 Reply Last reply
          0
          • J.HilkJ J.Hilk

            Let me expand @GrecKo 's links, in case this is what you actually want to do:

            https://doc.qt.io/qt-5/qquickimageprovider.html
            https://doc.qt.io/qt-5/qtquick-imageprovider-example.html

            C Offline
            C Offline
            celica
            wrote on last edited by
            #5

            @J-Hilk Thanks all for the replies, quite a lot of info here.
            Apologies but could someone recommend how I go about integrating this into my code sample above. I'm still a bit of a newbie. Just to get me started

            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