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. QFileDialog- Using getOpenFile

QFileDialog- Using getOpenFile

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 5 Posters 3.7k 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
    LT-K101
    wrote on last edited by
    #1

    Hi, I'm trying to let user browse and choose pdf files from directory but my application exits as soon as i click on open file instead of displaying the file name in lineEdit. Please i need assistance, Thanks. Below is my code.

     self.ui.Second_Browse_Button.clicked.connect(self.Load_First_Certificate)
    
    
        def Load_First_Certificate(self):
            dialog = QFileDialog()
            dialog.setFileMode(QFileDialog.AnyFile)
            dialog.setFilter(QDir.Files)
    
    
            if dialog.exec_():
                file_name = dialog.selectedFiles()
    
                if file_name[0].endswith('.pdf'):
                    with open(file_name[0], 'r') as f:
                        data = f.read()
                        self.lineEdit_108.setPlainText(data)
                        f.close()
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • L LT-K101

      @JonB @JonB Any help on how to display only file name instead of displaying file full path using the QFileDialog? Thanks in advance. Below is my code.

          
      def Load_First_Certificate(self):
              self.fname1, ok = QFileDialog.getOpenFileName(self, "Upload Pdf File", "", "Pdf Files(*.pdf)")
      
              if ok:
                  defaultPdf = os.path.split(self.fname1)
                  self.ui.Pdf_lineEdit.setText(self.fname1)
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #27

      @LT-K101

      self.ui.Pdf_lineEdit.setText(os.path.basename(self.fname1))

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

      JonBJ L 2 Replies Last reply
      0
      • L LT-K101

        Hi, I'm trying to let user browse and choose pdf files from directory but my application exits as soon as i click on open file instead of displaying the file name in lineEdit. Please i need assistance, Thanks. Below is my code.

         self.ui.Second_Browse_Button.clicked.connect(self.Load_First_Certificate)
        
        
            def Load_First_Certificate(self):
                dialog = QFileDialog()
                dialog.setFileMode(QFileDialog.AnyFile)
                dialog.setFilter(QDir.Files)
        
        
                if dialog.exec_():
                    file_name = dialog.selectedFiles()
        
                    if file_name[0].endswith('.pdf'):
                        with open(file_name[0], 'r') as f:
                            data = f.read()
                            self.lineEdit_108.setPlainText(data)
                            f.close()
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by jsulm
        #2

        @LT-K101 said in QFileDialog- Using getOpenFile:

        but my application exits as soon as i click on open file

        Did you at least run through debugger to see where exactly it crashes?
        And what is the error code/signal?

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

        L 1 Reply Last reply
        0
        • L LT-K101

          Hi, I'm trying to let user browse and choose pdf files from directory but my application exits as soon as i click on open file instead of displaying the file name in lineEdit. Please i need assistance, Thanks. Below is my code.

           self.ui.Second_Browse_Button.clicked.connect(self.Load_First_Certificate)
          
          
              def Load_First_Certificate(self):
                  dialog = QFileDialog()
                  dialog.setFileMode(QFileDialog.AnyFile)
                  dialog.setFilter(QDir.Files)
          
          
                  if dialog.exec_():
                      file_name = dialog.selectedFiles()
          
                      if file_name[0].endswith('.pdf'):
                          with open(file_name[0], 'r') as f:
                              data = f.read()
                              self.lineEdit_108.setPlainText(data)
                              f.close()
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @LT-K101 said in QFileDialog- Using getOpenFile:

          instead of displaying the file name in lineEdit

          In addition to @jsulm. Your code does not attempt to display the selected file name, it attempts to open it and put its content (which will presumably be a large amount of PDF) into the line edit.

          Although I do not think it will cause a crash, you should not be calling f.close() inside a with open ....

          If for whatever reason your open() fails (e.g. non existent file because of your QFileDialog.AnyFile) I believe in Python you will get a runtime error, which if you are not trapping would exit the script. You should also chek the length of your file_name before indexing it.

          artwawA 1 Reply Last reply
          0
          • jsulmJ jsulm

            @LT-K101 said in QFileDialog- Using getOpenFile:

            but my application exits as soon as i click on open file

            Did you at least run through debugger to see where exactly it crashes?
            And what is the error code/signal?

            L Offline
            L Offline
            LT-K101
            wrote on last edited by
            #4

            @jsulm I'm not getting any error message

            jsulmJ 1 Reply Last reply
            0
            • L LT-K101

              @jsulm I'm not getting any error message

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @LT-K101 Please run through debugger - this is the first thing to do in such a situation.
              From the code I don't see why it would crash, so please use debugger...

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mchinand
                wrote on last edited by
                #6

                @LT-K101 said in QFileDialog- Using getOpenFile:

                if file_name[0].endswith('.pdf'):

                Was a file definitely selected? If file_name is empty, this line would cause a crash.

                jsulmJ 1 Reply Last reply
                1
                • M mchinand

                  @LT-K101 said in QFileDialog- Using getOpenFile:

                  if file_name[0].endswith('.pdf'):

                  Was a file definitely selected? If file_name is empty, this line would cause a crash.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @mchinand Agree.
                  But only if "my application exits as soon as i click on open file" is not what really happens :-)

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

                  JonBJ L 2 Replies Last reply
                  3
                  • jsulmJ jsulm

                    @mchinand Agree.
                    But only if "my application exits as soon as i click on open file" is not what really happens :-)

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @jsulm
                    ... plus you do/should get a traceback error message from Python ... :)

                    1 Reply Last reply
                    1
                    • JonBJ JonB

                      @LT-K101 said in QFileDialog- Using getOpenFile:

                      instead of displaying the file name in lineEdit

                      In addition to @jsulm. Your code does not attempt to display the selected file name, it attempts to open it and put its content (which will presumably be a large amount of PDF) into the line edit.

                      Although I do not think it will cause a crash, you should not be calling f.close() inside a with open ....

                      If for whatever reason your open() fails (e.g. non existent file because of your QFileDialog.AnyFile) I believe in Python you will get a runtime error, which if you are not trapping would exit the script. You should also chek the length of your file_name before indexing it.

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #9

                      @JonB said in QFileDialog- Using getOpenFile:

                      Although I do not think it will cause a crash, you should not be calling f.close() inside a with open ....

                      with statement calls close() at the end of the statement. calling f.close() is an obvious error here, however simple calling close() twice on the same object doesn't produce an error. Reading from the closed file does though.

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      JonBJ 1 Reply Last reply
                      0
                      • artwawA artwaw

                        @JonB said in QFileDialog- Using getOpenFile:

                        Although I do not think it will cause a crash, you should not be calling f.close() inside a with open ....

                        with statement calls close() at the end of the statement. calling f.close() is an obvious error here, however simple calling close() twice on the same object doesn't produce an error. Reading from the closed file does though.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #10

                        @artwaw
                        Yes I don't think it matters but since half the point of with open is that it closes the file for you it seems best not to close it. Though as I said I don't think it's getting that far here.

                        1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @mchinand Agree.
                          But only if "my application exits as soon as i click on open file" is not what really happens :-)

                          L Offline
                          L Offline
                          LT-K101
                          wrote on last edited by
                          #11

                          @jsulm When I click on the Browse Button to open the QDialog window for file selection and i click cancel it freezes the application and exit. This is really strange.

                          jsulmJ JonBJ 2 Replies Last reply
                          0
                          • L LT-K101

                            @jsulm When I click on the Browse Button to open the QDialog window for file selection and i click cancel it freezes the application and exit. This is really strange.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by jsulm
                            #12

                            @LT-K101 Then please read what @mchinand wrote.
                            And I already wrote this several times: use debugger!

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

                            1 Reply Last reply
                            3
                            • L LT-K101

                              @jsulm When I click on the Browse Button to open the QDialog window for file selection and i click cancel it freezes the application and exit. This is really strange.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #13

                              @LT-K101 said in QFileDialog- Using getOpenFile:

                              When I click on the Browse Button to open the QDialog window for file selection and i click cancel it freezes the application and exit.

                              1. Use a debugger/print statements to see where execution is going.
                              2. Are there any further code lines in def Load_First_Certificate(self): after what you show?
                              3. Is there some other window in the application currently open other than the file chooser dialog when you close it?
                              L 1 Reply Last reply
                              0
                              • JonBJ JonB

                                @LT-K101 said in QFileDialog- Using getOpenFile:

                                When I click on the Browse Button to open the QDialog window for file selection and i click cancel it freezes the application and exit.

                                1. Use a debugger/print statements to see where execution is going.
                                2. Are there any further code lines in def Load_First_Certificate(self): after what you show?
                                3. Is there some other window in the application currently open other than the file chooser dialog when you close it?
                                L Offline
                                L Offline
                                LT-K101
                                wrote on last edited by
                                #14

                                @JonB I did print in the def Load_First_Certificate(self): function and it worked fine. I'm now using the code below to load the file as image.

                                    def Load_Second_Certificate(self):
                                            fname5, _ = QFileDialog.getOpenFileName(self, "Choose File", "", "All Files(*) ;; Images(*.jpeg)")
                                            pixmap = QPixmap(fname5, _)
                                            self.ui.Display_label_6.setPixmap(QPixmap(pixmap))
                                            self.ui.Display_label_6.repaint()
                                
                                
                                
                                jsulmJ JonBJ L 3 Replies Last reply
                                0
                                • L LT-K101

                                  @JonB I did print in the def Load_First_Certificate(self): function and it worked fine. I'm now using the code below to load the file as image.

                                      def Load_Second_Certificate(self):
                                              fname5, _ = QFileDialog.getOpenFileName(self, "Choose File", "", "All Files(*) ;; Images(*.jpeg)")
                                              pixmap = QPixmap(fname5, _)
                                              self.ui.Display_label_6.setPixmap(QPixmap(pixmap))
                                              self.ui.Display_label_6.repaint()
                                  
                                  
                                  
                                  jsulmJ Offline
                                  jsulmJ Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  @LT-K101 You don't check whether user cancelled the dialog.

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

                                  L 1 Reply Last reply
                                  2
                                  • L LT-K101

                                    @JonB I did print in the def Load_First_Certificate(self): function and it worked fine. I'm now using the code below to load the file as image.

                                        def Load_Second_Certificate(self):
                                                fname5, _ = QFileDialog.getOpenFileName(self, "Choose File", "", "All Files(*) ;; Images(*.jpeg)")
                                                pixmap = QPixmap(fname5, _)
                                                self.ui.Display_label_6.setPixmap(QPixmap(pixmap))
                                                self.ui.Display_label_6.repaint()
                                    
                                    
                                    
                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #16

                                    @LT-K101 said in QFileDialog- Using getOpenFile:

                                    I did print in the def Load_First_Certificate(self): function and it worked fine.

                                    Then you should be able to see how far the code got to in execution when the " application exits as soon as i click on open file".

                                    I suggest you answer my earlier question #3.

                                    You should also make your new code check for cancellation, like your old code did, as @jsulm has remarked.

                                    You have changed your code, and what it does, but said nothing about whatever problem it now has.

                                    You do not check the result from pixmap = QPixmap(fname5, _), and I don't know if that _ is the same variable as on the line above it, which would be wrong if it is.

                                    1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @LT-K101 You don't check whether user cancelled the dialog.

                                      L Offline
                                      L Offline
                                      LT-K101
                                      wrote on last edited by
                                      #17

                                      @jsulm I just did try to cancel and nothing happened, everything works fine. How to save the selected image file in database and display with the other text on the window is what i'm reading on now. Please any assistance?

                                      jsulmJ JonBJ 2 Replies Last reply
                                      0
                                      • L LT-K101

                                        @jsulm I just did try to cancel and nothing happened, everything works fine. How to save the selected image file in database and display with the other text on the window is what i'm reading on now. Please any assistance?

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #18

                                        @LT-K101 said in QFileDialog- Using getOpenFile:

                                        Please any assistance?

                                        What kind of database? SQL? https://doc.qt.io/qt-5/qtsql-index.html

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

                                        L 1 Reply Last reply
                                        0
                                        • L LT-K101

                                          @jsulm I just did try to cancel and nothing happened, everything works fine. How to save the selected image file in database and display with the other text on the window is what i'm reading on now. Please any assistance?

                                          JonBJ Offline
                                          JonBJ Offline
                                          JonB
                                          wrote on last edited by JonB
                                          #19

                                          @LT-K101 said in QFileDialog- Using getOpenFile:

                                          @jsulm I just did try to cancel and nothing happened, everything works fine.

                                          That is truly remarkable, with your existing code.

                                                      fname5, _ = QFileDialog.getOpenFileName(self, "Choose File", "", "All Files(*) ;; Images(*.jpeg)")
                                                      pixmap = QPixmap(fname5, _)
                                          

                                          So after the user clicks Cancel in the dialog, what is returned in fname5 and most importantly what in your code stops so that it does not try to load a QPixmap? Or do you mean it just seems to be OK when you continue ignoring the Cancel and try to load a QPixmap from "nothing"? Try first running this and picking a suitable file to load, then re-running it and pressing Cancel, that leaves the currently loaded pixmap as-is, does it?

                                          L 1 Reply Last reply
                                          0
                                          • jsulmJ jsulm

                                            @LT-K101 said in QFileDialog- Using getOpenFile:

                                            Please any assistance?

                                            What kind of database? SQL? https://doc.qt.io/qt-5/qtsql-index.html

                                            L Offline
                                            L Offline
                                            LT-K101
                                            wrote on last edited by
                                            #20

                                            @jsulm Yes I'm using SQL.

                                            jsulmJ 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