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
Forum Updated to NodeBB v4.3 + New Features

QFileDialog- Using getOpenFile

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 5 Posters 4.6k 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.
  • 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 Online
        JonBJ Online
        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 Online
              JonBJ Online
              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
                • L LT-K101

                  @jsulm Yes I'm using SQL.

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

                  @LT-K101 Start here: https://doc.qt.io/qt-5/qtsql-index.html

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

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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 Offline
                    L Offline
                    LT-K101
                    wrote on last edited by
                    #22

                    @JonB Oh! my bad. I meant when user clicks on the cancel the dialog window exits.

                    JonBJ 1 Reply Last reply
                    0
                    • L LT-K101

                      @JonB Oh! my bad. I meant when user clicks on the cancel the dialog window exits.

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #23

                      @LT-K101
                      Now I'm really lost. From the QFileDialog clicking either Cancel or selecting a file and clicking OK should close/exit that dialog window. Originally you said

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

                      If you want help can you please state clearly whether this is still the case and/or when your application exits upon closing the file dialog.

                      1 Reply 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()
                        
                        
                        
                        L Offline
                        L Offline
                        LT-K101
                        wrote on last edited by LT-K101
                        #24

                        @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)
                        
                        artwawA JonBJ jsulmJ 3 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)
                          
                          artwawA Offline
                          artwawA Offline
                          artwaw
                          wrote on last edited by
                          #25

                          @LT-K101 If you look at the QString (or str in Python) documentation you will surely notice that there is split() method. So self.fname1.split('/').last() should do the trick. Why don't read documentation on the basic types you use?

                          For more information please re-read.

                          Kind Regards,
                          Artur

                          1 Reply 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)
                            
                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by JonB
                            #26

                            @LT-K101

                            print(QFile(self.fname1).fileName())
                            print(QFileInfo(self.fname1).fileName())
                            

                            Your code:

                                    self.fname1, ok = QFileDialog.getOpenFileName(self, "Upload Pdf File", "", "Pdf Files(*.pdf)")
                                    if ok:
                            

                            Where do you get the idea that the second result is any kind of ok parameter, and that you should be looking at it for success?

                            L 1 Reply Last reply
                            1
                            • 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
                              • jsulmJ jsulm

                                @LT-K101

                                self.ui.Pdf_lineEdit.setText(os.path.basename(self.fname1))
                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by JonB
                                #28

                                @jsulm
                                You are encouraging OP to mix native Python calls with Qt calls when no need to, grrr! Since QFileDialog.getOpenFileName() returns a Qt-type-path (right?), he must now go check up how os.path.... handles e.g. /s under Windows....

                                1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @LT-K101

                                  print(QFile(self.fname1).fileName())
                                  print(QFileInfo(self.fname1).fileName())
                                  

                                  Your code:

                                          self.fname1, ok = QFileDialog.getOpenFileName(self, "Upload Pdf File", "", "Pdf Files(*.pdf)")
                                          if ok:
                                  

                                  Where do you get the idea that the second result is any kind of ok parameter, and that you should be looking at it for success?

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

                                  @JonB Sorry that was a function i wrote for my image upload function, if user does not upload an image then the default image should be an avatar. I copied it and forgot to delete the if statement. Thanks

                                  1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @LT-K101

                                    self.ui.Pdf_lineEdit.setText(os.path.basename(self.fname1))
                                    L Offline
                                    L Offline
                                    LT-K101
                                    wrote on last edited by
                                    #30

                                    @jsulm Thanks it's working! After selecting the pdf file saving to a location in my working directory is the problem now. Any assistance please?

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • L LT-K101

                                      @jsulm Thanks it's working! After selecting the pdf file saving to a location in my working directory is the problem now. Any assistance please?

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

                                      @LT-K101 said in QFileDialog- Using getOpenFile:

                                      Any assistance please?

                                      Please ask a concrete question. What is the concrete problem you're having now?

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

                                      L 1 Reply Last reply
                                      0
                                      • jsulmJ jsulm

                                        @LT-K101 said in QFileDialog- Using getOpenFile:

                                        Any assistance please?

                                        Please ask a concrete question. What is the concrete problem you're having now?

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

                                        @jsulm I want to save the uploaded file itself to a specific directory not just the filename. Example when the user click on open button to select file , after user select file. The file should be saved in a directory.

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • L LT-K101

                                          @jsulm I want to save the uploaded file itself to a specific directory not just the filename. Example when the user click on open button to select file , after user select file. The file should be saved in a directory.

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

                                          @LT-K101 Then do so: https://doc.qt.io/qt-5/qfile.html

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

                                          L 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