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. Inner widget doesn't closes in a modified QFileDialog
Forum Updated to NodeBB v4.3 + New Features

Inner widget doesn't closes in a modified QFileDialog

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 424 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.
  • M Offline
    M Offline
    MrAWD
    wrote on last edited by MrAWD
    #1

    I am trying to get QFIleDialog to return folders along with files. I have used a code from Stack Overflow example and with some help from this thread it is in usable state. But, when drop box is opened and Enter clicked, drop box stays on the screen. Here is a complete code:

    from PySide2.QtWidgets import (QFileDialog, QApplication, QDialog,
                                   QStackedWidget, QListView, QLineEdit,
                                   QMessageBox, QWidget, QPushButton, QVBoxLayout)
    import sys
    from PySide2.QtCore import QFileInfo
    
    
    class Widget(QWidget):
    
        def __init__(self):
            super().__init__(None)
            self.button = QPushButton("Open")
            self.button.clicked.connect(self.onButtonCLicked)
    
            self.mainLayout = QVBoxLayout()
            self.mainLayout.addWidget(self.button)
            self.setLayout(self.mainLayout)
            self.resize(300, 100)
    
        def onButtonCLicked(self):
            openFilesAndDirs(parent=self, caption="Hi")
    
    
    def openFilesAndDirs(parent=None,
                         caption='',
                         directory='',
                         filters='',
                         initialFilter='',
                         options=None):
    
        dialog = QFileDialog(parent, windowTitle=caption)
        dialog.setFileMode(dialog.ExistingFile)
    
        if options:
            dialog.setOptions(options)
        dialog.setOption(dialog.DontUseNativeDialog, True)
    
        if directory:
            dialog.setDirectory(directory)
    
        if filters:
            dialog.setNameFilter(filters)
            if initialFilter:
                dialog.selectNameFilter(initialFilter)
    
        dialog.accept = lambda: QDialog.accept(dialog)
    
        stackedWidget = dialog.findChild(QStackedWidget)
        dialogItemView = stackedWidget.findChild(QListView)
    
        def updateText():
            selected = []
            for index in dialogItemView.selectionModel().selectedRows():
                selected.append('{}'.format(index.data()))
    
            lineEdit = dialog.findChild(QLineEdit)
            lineEdit.blockSignals(True)
            lineEdit.setText(' '.join(selected) if selected else '')
            lineEdit.blockSignals(False)
    
        dialogItemView.selectionModel().selectionChanged.connect(updateText)
    
        lineEdit = dialog.findChild(QLineEdit)
        dialog.directoryEntered.connect(lambda: lineEdit.setText(''))
    
        filePath = ""
        fileExt = ""
    
        if dialog.exec_() == QDialog.Accepted:
    
            files = dialog.selectedFiles()
            if len(files) > 0:
                finfo = QFileInfo(files[0])
                filePath = finfo.absoluteFilePath()
                fileExt = finfo.completeSuffix()
    
        return filePath, fileExt
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        w = Widget()
        w.show()
        sys.exit(app.exec_())
    
    

    When code is executed, when one letter is typed in the edit box popup box appears under the edit widget, and with Down Arrow, one of the entries is selected, it looks like this:
    Zana.png
    Pressing on Enter key selects and returns selected entry, but drop box stays on the screen:
    ZanaEnd.png

    What could I do here to remove this widget?

    It does go away as soon as you click elsewhere, btw.

    Pl45m4P 1 Reply Last reply
    0
    • M MrAWD referenced this topic on
    • M MrAWD

      I am trying to get QFIleDialog to return folders along with files. I have used a code from Stack Overflow example and with some help from this thread it is in usable state. But, when drop box is opened and Enter clicked, drop box stays on the screen. Here is a complete code:

      from PySide2.QtWidgets import (QFileDialog, QApplication, QDialog,
                                     QStackedWidget, QListView, QLineEdit,
                                     QMessageBox, QWidget, QPushButton, QVBoxLayout)
      import sys
      from PySide2.QtCore import QFileInfo
      
      
      class Widget(QWidget):
      
          def __init__(self):
              super().__init__(None)
              self.button = QPushButton("Open")
              self.button.clicked.connect(self.onButtonCLicked)
      
              self.mainLayout = QVBoxLayout()
              self.mainLayout.addWidget(self.button)
              self.setLayout(self.mainLayout)
              self.resize(300, 100)
      
          def onButtonCLicked(self):
              openFilesAndDirs(parent=self, caption="Hi")
      
      
      def openFilesAndDirs(parent=None,
                           caption='',
                           directory='',
                           filters='',
                           initialFilter='',
                           options=None):
      
          dialog = QFileDialog(parent, windowTitle=caption)
          dialog.setFileMode(dialog.ExistingFile)
      
          if options:
              dialog.setOptions(options)
          dialog.setOption(dialog.DontUseNativeDialog, True)
      
          if directory:
              dialog.setDirectory(directory)
      
          if filters:
              dialog.setNameFilter(filters)
              if initialFilter:
                  dialog.selectNameFilter(initialFilter)
      
          dialog.accept = lambda: QDialog.accept(dialog)
      
          stackedWidget = dialog.findChild(QStackedWidget)
          dialogItemView = stackedWidget.findChild(QListView)
      
          def updateText():
              selected = []
              for index in dialogItemView.selectionModel().selectedRows():
                  selected.append('{}'.format(index.data()))
      
              lineEdit = dialog.findChild(QLineEdit)
              lineEdit.blockSignals(True)
              lineEdit.setText(' '.join(selected) if selected else '')
              lineEdit.blockSignals(False)
      
          dialogItemView.selectionModel().selectionChanged.connect(updateText)
      
          lineEdit = dialog.findChild(QLineEdit)
          dialog.directoryEntered.connect(lambda: lineEdit.setText(''))
      
          filePath = ""
          fileExt = ""
      
          if dialog.exec_() == QDialog.Accepted:
      
              files = dialog.selectedFiles()
              if len(files) > 0:
                  finfo = QFileInfo(files[0])
                  filePath = finfo.absoluteFilePath()
                  fileExt = finfo.completeSuffix()
      
          return filePath, fileExt
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          w = Widget()
          w.show()
          sys.exit(app.exec_())
      
      

      When code is executed, when one letter is typed in the edit box popup box appears under the edit widget, and with Down Arrow, one of the entries is selected, it looks like this:
      Zana.png
      Pressing on Enter key selects and returns selected entry, but drop box stays on the screen:
      ZanaEnd.png

      What could I do here to remove this widget?

      It does go away as soon as you click elsewhere, btw.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @MrAWD said in Inner widget doesn't closes in a modified QFileDialog:

      What could I do here to remove this widget?

      I assume this is some part of your dialogItemView because you obviously use a modified QLineEdit there...
      Are these all childs of your dialog? If not, they remain open in position when you close the dialog.
      They should close/hide together with it


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      M 1 Reply Last reply
      0
      • Pl45m4P Pl45m4

        @MrAWD said in Inner widget doesn't closes in a modified QFileDialog:

        What could I do here to remove this widget?

        I assume this is some part of your dialogItemView because you obviously use a modified QLineEdit there...
        Are these all childs of your dialog? If not, they remain open in position when you close the dialog.
        They should close/hide together with it

        M Offline
        M Offline
        MrAWD
        wrote on last edited by
        #3

        @Pl45m4 That is the whole problem. I am using a non native file dialog and don't have a direct access to that code.

        If I don't find solution for this, I will have to write that dialog myself...

        Pl45m4P 1 Reply Last reply
        0
        • M MrAWD

          @Pl45m4 That is the whole problem. I am using a non native file dialog and don't have a direct access to that code.

          If I don't find solution for this, I will have to write that dialog myself...

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by
          #4

          @MrAWD

          How do you handle the view/model?! Is it part of your dialog? Either close it manually by code when your dialog is accepted or make it a child of your dialog if possible.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          M 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @MrAWD

            How do you handle the view/model?! Is it part of your dialog? Either close it manually by code when your dialog is accepted or make it a child of your dialog if possible.

            M Offline
            M Offline
            MrAWD
            wrote on last edited by
            #5

            @Pl45m4 That is the whole issue - I am using standard Qt file dialog in a non native mode, and I don't have its implementation code. If I did, we probably wouldn't have to do any of this

            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