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 3.8k 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.
  • 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 Offline
      JonBJ Offline
      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 Offline
          JonBJ Offline
          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
                    • jsulmJ jsulm

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

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

                      @jsulm Thanks

                      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