Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how can i drag a file from windows explorer or desktop to a Qt Widget(eg. QTreeWidget) ?
Forum Updated to NodeBB v4.3 + New Features

how can i drag a file from windows explorer or desktop to a Qt Widget(eg. QTreeWidget) ?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 3.8k Views 2 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by opengpu2
    #1

    how can i drag a file from windows explorer or desktop to a Qt Widget(eg. QTreeWidget) ?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What is a Windows Resource Manager? I don't think you mean this cause it's a server feature and doesn't have any files in it.

      1 Reply Last reply
      0
      • O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        modified, thank you !

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          There are basically 3 steps:

          Enable drag&drop suport on your widget:

          treeWidget->setAcceptDrops(true);
          

          Override drag enter event and check if it carries urls:

          void YourTreeWidget::dragEnterEvent(QDragEnterEvent * evt) {
              if(evt->mimeData()->hasUrls())
                  evt->accept();
          }
          

          NOTE:The above accepts any kind of urls. If you want to be more specific you can inspect the urls to check if they are file/directory/web urls and accept only what you want.

          Last step is to override drop event and act on the urls:

          void YourTreeWidget::dropEvent(QDropEvent * evt) {
              auto urls = evt->mimeData()->urls();
              foreach(auto url, urls) {
                  //do stuff to tree widget based on the file url
              }
          }
          
          1 Reply Last reply
          1
          • O Offline
            O Offline
            opengpu2
            wrote on last edited by
            #5

            thank you very much.
            but how can i inspect the urls to check if they are file/directory/web urls ?
            is there any examples or docs ?

            MayEnjoyM 1 Reply Last reply
            0
            • O Offline
              O Offline
              opengpu2
              wrote on last edited by
              #6

              and i found QString s1 = urls.at(i).path() return a path with "/" at the beginning.
              but, QString s2 = urls.at(i).toLocalFile() return a path that fit the normal file path.
              so should i use toLocalFile() ?

              1 Reply Last reply
              0
              • O opengpu2

                thank you very much.
                but how can i inspect the urls to check if they are file/directory/web urls ?
                is there any examples or docs ?

                MayEnjoyM Offline
                MayEnjoyM Offline
                MayEnjoy
                wrote on last edited by
                #7

                @opengpu2 said:

                file/directory/web urls ?

                You can use QUrl and QFileInfo

                I am a engineer.

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  opengpu2
                  wrote on last edited by
                  #8

                  thank you, mainly file/directory/ :)

                  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