跳到內容
  • 版面
  • 最新
  • 標籤
  • 熱門
  • 使用者
  • 群組
  • 搜尋
  • Get Qt Extensions
  • Unsolved
Collapse
品牌標誌
  1. 首頁
  2. Qt Development
  3. General and Desktop
  4. QFileDialog- Using getOpenFile
Forum Updated to NodeBB v4.3 + New Features

QFileDialog- Using getOpenFile

已排程 已置頂 已鎖定 已移動 Solved General and Desktop
34 貼文 5 Posters 4.6k 瀏覽 2 Watching
  • 從舊到新
  • 從新到舊
  • 最多點贊
回覆
  • 在新貼文中回覆
登入後回覆
此主題已被刪除。只有擁有主題管理權限的使用者可以查看。
  • 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 離線
    L 離線
    LT-K101
    寫於 最後由 編輯
    #20

    @jsulm Yes I'm using SQL.

    jsulmJ 1 條回覆 最後回覆
    0
    • L LT-K101

      @jsulm Yes I'm using SQL.

      jsulmJ 離線
      jsulmJ 離線
      jsulm
      Lifetime Qt Champion
      寫於 最後由 編輯
      #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 條回覆 最後回覆
      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 離線
        L 離線
        LT-K101
        寫於 最後由 編輯
        #22

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

        JonBJ 1 條回覆 最後回覆
        0
        • L LT-K101

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

          JonBJ 離線
          JonBJ 離線
          JonB
          寫於 最後由 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 條回覆 最後回覆
          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 離線
            L 離線
            LT-K101
            寫於 最後由 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 條回覆 最後回覆
            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 離線
              artwawA 離線
              artwaw
              寫於 最後由 編輯
              #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 條回覆 最後回覆
              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 離線
                JonBJ 離線
                JonB
                寫於 最後由 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 條回覆 最後回覆
                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 離線
                  jsulmJ 離線
                  jsulm
                  Lifetime Qt Champion
                  寫於 最後由 編輯
                  #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 條回覆 最後回覆
                  0
                  • jsulmJ jsulm

                    @LT-K101

                    self.ui.Pdf_lineEdit.setText(os.path.basename(self.fname1))
                    JonBJ 離線
                    JonBJ 離線
                    JonB
                    寫於 最後由 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 條回覆 最後回覆
                    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 離線
                      L 離線
                      LT-K101
                      寫於 最後由 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 條回覆 最後回覆
                      0
                      • jsulmJ jsulm

                        @LT-K101

                        self.ui.Pdf_lineEdit.setText(os.path.basename(self.fname1))
                        L 離線
                        L 離線
                        LT-K101
                        寫於 最後由 編輯
                        #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 條回覆 最後回覆
                        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 離線
                          jsulmJ 離線
                          jsulm
                          Lifetime Qt Champion
                          寫於 最後由 編輯
                          #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 條回覆 最後回覆
                          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 離線
                            L 離線
                            LT-K101
                            寫於 最後由 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 條回覆 最後回覆
                            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 離線
                              jsulmJ 離線
                              jsulm
                              Lifetime Qt Champion
                              寫於 最後由 編輯
                              #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 條回覆 最後回覆
                              0
                              • jsulmJ jsulm

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

                                L 離線
                                L 離線
                                LT-K101
                                寫於 最後由 編輯
                                #34

                                @jsulm Thanks

                                1 條回覆 最後回覆
                                0

                                • 登入

                                • Login or register to search.
                                • 第一個貼文
                                  最後的貼文
                                0
                                • 版面
                                • 最新
                                • 標籤
                                • 熱門
                                • 使用者
                                • 群組
                                • 搜尋
                                • Get Qt Extensions
                                • Unsolved