Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Help to use QFileDialog.
Qt 6.11 is out! See what's new in the release blog

Help to use QFileDialog.

Scheduled Pinned Locked Moved Solved Qt for Python
4 Posts 3 Posters 2.2k 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.
  • L Offline
    L Offline
    luguecos
    wrote on last edited by luguecos
    #1

    Hello, i'm still learning how to use pyqt5 and i'm trying to make a plot application for myself. To plot my graphs I have to load data from a file. I did make a button for this purpose. The goal is to click the button to open a fileDialog window.

    Here is a sample of the code in this regard:

    from PyQt5 import QtWidgets, QtCore, QtGui, Qt
    
    class Ui_MainWindow(QtWidgets.QWidget):    
        def setupUi(self, MainWindow):
            
            MainWindow.setObjectName("MainWindow")
            MainWindow.setWindowTitle("L Plot Beta v1.0")
            MainWindow.resize(800,600)
           
            Ui_MainWindow().loadDataButton()
    
        def loadDataButton(self):
            self.load_data_button = QtWidgets.QPushButton(MainWindow)
            self.load_data_button.setGeometry(QtCore.QRect(430, 100, 100, 30))
            self.load_data_button.setText("Load Data")
            
            self.load_data_button.clicked.connect(self.openFile())
    
        def openFile(self):
            fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', ' ', 'All Files (*);;Text Files (*.txt)')
            if fileName:
                print(fileName)
    
    if __name__ == "__main__":
    
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        Ui_MainWindow().setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())
    

    When I run this code the File Dialog box pops up before the main window. It is suppose to open the main window, then when I click the button it shows de File dialog box.

    What I'm doing wrong?

    Thanks in advance!

    1 Reply Last reply
    0
    • L Offline
      L Offline
      luguecos
      wrote on last edited by
      #2

      I got it. The problem is that a lambda function was necessary when calling the method clicked.connect

      just change the line

      self.load_data_button.clicked.connect(self.openFile())
      

      to

      self.load_data_button.clicked.connect(lambda: self.openFile())`
      
      jsulmJ 1 Reply Last reply
      0
      • L luguecos

        I got it. The problem is that a lambda function was necessary when calling the method clicked.connect

        just change the line

        self.load_data_button.clicked.connect(self.openFile())
        

        to

        self.load_data_button.clicked.connect(lambda: self.openFile())`
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @luguecos said in Help to use QFileDialog.:

        self.load_data_button.clicked.connect(self.openFile())

        Here you're calling the slot. It should be:

        self.load_data_button.clicked.connect(self.openFile)
        

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        L 1 Reply Last reply
        1
        • jsulmJ jsulm

          @luguecos said in Help to use QFileDialog.:

          self.load_data_button.clicked.connect(self.openFile())

          Here you're calling the slot. It should be:

          self.load_data_button.clicked.connect(self.openFile)
          
          L Offline
          L Offline
          luguecos
          wrote on last edited by
          #4

          @jsulm When I call that way the button doesn't do anything

          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