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. DropArea: How can I set allowed exensions of external files using the "keys" property?
QtWS25 Last Chance

DropArea: How can I set allowed exensions of external files using the "keys" property?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 971 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.
  • R Offline
    R Offline
    robro
    wrote on last edited by
    #1

    Hello,

    I would like to use the keys property of a DropArea to accept only files with the extensions .bin and .hdr.

    I tried first to make it work with .jpg, but I have no clue how it works.

    This is one example, how it does not work:

    import QtQuick 2.9
    import QtQuick.Controls 2.2
    
    ApplicationWindow {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        DropArea {
            id: drop
            anchors.fill: parent
            enabled: true
            keys: ["image/jpg"] //No .jpg allowed on execution
            onEntered: {
                console.log("entered")
            }
            onExited: {
                console.log("excited")
            }
            onDropped: {
                console.log("dropped")
                console.log(drop.text)
            }
        }
    }
    

    How is the keys property used correct?

    Thank you very much.

    raven-worxR 1 Reply Last reply
    0
    • R robro

      Hello,

      I would like to use the keys property of a DropArea to accept only files with the extensions .bin and .hdr.

      I tried first to make it work with .jpg, but I have no clue how it works.

      This is one example, how it does not work:

      import QtQuick 2.9
      import QtQuick.Controls 2.2
      
      ApplicationWindow {
          visible: true
          width: 640
          height: 480
          title: qsTr("Hello World")
      
          DropArea {
              id: drop
              anchors.fill: parent
              enabled: true
              keys: ["image/jpg"] //No .jpg allowed on execution
              onEntered: {
                  console.log("entered")
              }
              onExited: {
                  console.log("excited")
              }
              onDropped: {
                  console.log("dropped")
                  console.log(drop.text)
              }
          }
      }
      

      How is the keys property used correct?

      Thank you very much.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @robro
      the keys property isn't right here. You need rather to do something like this:

      DropArea {
              id: drop
              anchors.fill: parent
      
              onEntered: {
                  for(var i = 0; i < drag.urls.length; i++)
                  {
                      if(validateFileExtension(drag.urls[i]))
                      {
                          drag.accept()
                          return // drag accepted
                       }
                  }
              }
      
              function checkFileExtension(filePath) {
                  return filePath.split('.').pop() == "jpg"
              }
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        Yaswanth
        wrote on last edited by
        #3

        As per my observation keys works as following,
        What ever the keys you specified in the drop area, only those keys can be accessed in droparea.

        import QtQuick 2.9
        import QtQuick.Window 2.2
        
        Window {
            visible: true
            width: 640
            height: 480
            title: qsTr("Hello World")
        
            DropArea
            {
                width : 100
                height: 100
                anchors.right: parent.right
                keys:["textComp","imageComp"]
                onEntered:
                {
                    console.log("Entered into the droparea")
                }
            }
        
            Text
            {
                id : textSample
                text : "This is sample Text"
                Drag.active: true
                Drag.keys:["textComp"]
                MouseArea
                {
                    anchors.fill: parent
                    drag.target: parent
                }
            }
        
            Rectangle
            {
                id : rect
                width: 50
                height: 50
                color: "RED"
                Drag.active: true
                Drag.keys:["rectComp"]
                MouseArea
                {
                    anchors.fill: parent
                    drag.target: parent
                }
            }
        
            Image
            {
                id : _image
                width: 50
                height: 50
                Drag.active: true
                Drag.keys:["imageComp"]
                source: "file:///D:/Sample.PNG"
                MouseArea
                {
                    anchors.fill: parent
                    drag.target: parent
                }
            }
        }
        

        What ever the key you specify for the QML elements only those which are matching in the list of the keys in drop area component can be detected. In the above only Image and text can be detected. In your case based on the file type have a flag and specify that flag in the keys in dropArea.

        1 Reply Last reply
        0
        • R Offline
          R Offline
          robro
          wrote on last edited by
          #4

          @raven-worx : Thank you very much!
          Your suggestion works fine :-)
          I just used the drop event instead of the drag event.

          @Yaswanth : Thanks for the information on how the keys property works! :-)

          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