Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make QFileDialog return a directory or a filename in one method
Qt 6.12 LTS is out! See what's new in the release blog

How to make QFileDialog return a directory or a filename in one method

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 4.0k 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
    Code_ReaQtor
    wrote on last edited by
    #1

    Hi,

    Just got into a problem wherein I need to select a file or a directory using QFileDialog. There are existing static methods like getExistingDirectory() and getOpenFileName(), however, they work separately. How can I make QFileDialog support both behaviors in one method?

    Thanks!

    Please visit my open-source projects at https://github.com/Code-ReaQtor.

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

      Hi,

      AFAIK, you can't do that without hacking Qt's sources yourself. Building the dialog yourself would not help since there's no mode (see setFileMode) that correspond to what you need.

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

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

        This is an unusual thing to do.
        What's your usecase for that, because I don't think it would be intuitive for a user. Maybe something else would be better here?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Code_ReaQtor
          wrote on last edited by
          #4

          I'm creating an IDE for a work "stuff" (let's call is stuff).
          In here, a project can be a file or a folder [the usecase].

          I made a simple workaround using pyside:
          @#!/usr/bin/env python
          from PySide import QtCore, QtGui

          class FileDialog(QtGui.QFileDialog):

          def __init__(self, *args):
              super(FileDialog, self).__init__(*args)
              self._selected = ""
              self.currentChanged.connect(self._onCurrentChanged)
          
          @QtCore.Slot(unicode)
          def _onCurrentChanged(self, path):
              # path becomes empty when file is selected
              # triggered twice
              if path:
                  self._selected = path
              self.selectFile(path)
          
          def accept(self):
              self.done(1)
              super(FileDialog, self).accept()
          
          def getSelected(self):
              return self._selected
          
          @staticmethod
          def getFileOrDirectory(parent):
              dialog = FileDialog(parent)
              result = dialog.exec_()
              if result:
                  return dialog.getSelected()
              return ""
          

          @

          Could you pls verify it?
          Maybe, I'll try to rewrite in C++ later.

          Thanks

          Please visit my open-source projects at https://github.com/Code-ReaQtor.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            I had to do something very similar recently. The user could choose either a directory or a single file (importing maps into a situational awareness application). Some maps (OSM tiles for example) are most easily imported by choosing the directory they are in. Others (GeoTIFFs), by picking the map file.

            I didn't want to have separate "Import GeoTIFF", "Import OSM tiles", "Import ADRG", "Import CADRG", etc commands.

            I used a TreeView with a QFileSystemModel.

            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
            • Unsolved