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. Default image from QRC resource, but customized image from filesystem?

Default image from QRC resource, but customized image from filesystem?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 4 Posters 1.0k Views 3 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.
  • B Offline
    B Offline
    bitbang
    wrote on last edited by
    #1

    Or, put another way, how to check file existence in QML?
    It easy to bind the image source property to a either a qrc: or filesystem path; but how to make it use the qrc: if and only if the file in the filesystem is missing?

    This is to allow users to customize an image, whose source defaults to a qrc: path unless they upload an alternative file.

    I'm aware Googling suggests resorting to C++ for this, but I'm unable to find an example.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      How can your users select the custom image ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply
      0
      • fcarneyF Offline
        fcarneyF Offline
        fcarney
        wrote on last edited by
        #3

        QFileInfo can check for the existence of a file. You could have it verify a path and add a command to qml via setContextObject. This would allow you to add utility commands to qml.

        C++ is a perfectly valid school of magic.

        B 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          How can your users select the custom image ?

          B Offline
          B Offline
          bitbang
          wrote on last edited by bitbang
          #4

          @SGaist
          In my specific case, users upload files externally trough ssh prior to application launch.
          I was wondering about an hypotetic File object:

          Image {
              File {
                  id: file
                  source:  "file:///bitmaps/custom.png"
              }
              source:  file.exists ? file.source : "qrc:///bitmaps/default.png"
          }
          
          1 Reply Last reply
          0
          • fcarneyF fcarney

            QFileInfo can check for the existence of a file. You could have it verify a path and add a command to qml via setContextObject. This would allow you to add utility commands to qml.

            B Offline
            B Offline
            bitbang
            wrote on last edited by bitbang
            #5

            @fcarney
            First steps in C++-QML integration. So if I understand correctly:

            • Create a QObject with two properties, QString source and bool exists
            • In code (via signal/slot), when source changes, set exists accordingly via QFileInfo
            • Expose the object to QML via setContextObject

            I will give it a try, thank you!
            Cannot figure out why I was'nt able to find a ready-made code snippet, checking files seems a common need to me.
            I'm learning the declarative approach coming from an imperative background: am I doing something wrong or in a non-QML way?

            1 Reply Last reply
            0
            • fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              I would create a slot function called:

              bool fileExists(QString path){}
              

              The setContextObject will place functions on the object in the QML namespace. So you will be able to call fileExists from QML.

              C++ is a perfectly valid school of magic.

              1 Reply Last reply
              2
              • ODБOïO Offline
                ODБOïO Offline
                ODБOï
                wrote on last edited by ODБOï
                #7

                hi,
                this seems to work also

                 Image {
                        source: "file:///c:/Users/user/Pictures/img.png"
                        onStatusChanged : if(status == Image.Error) source = "qrc:///img.png"                   
                    }
                
                B 1 Reply Last reply
                5
                • fcarneyF Offline
                  fcarneyF Offline
                  fcarney
                  wrote on last edited by fcarney
                  #8

                  @LeLev
                  Wow, that is a really nice way to check that. Thanks!

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  1
                  • ODБOïO ODБOï

                    hi,
                    this seems to work also

                     Image {
                            source: "file:///c:/Users/user/Pictures/img.png"
                            onStatusChanged : if(status == Image.Error) source = "qrc:///img.png"                   
                        }
                    
                    B Offline
                    B Offline
                    bitbang
                    wrote on last edited by
                    #9

                    @LeLev @fcarney
                    Thank you! This solves my problem!

                    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