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. QTabWidget - unable to load multiple documents in multiple tabs
Forum Updated to NodeBB v4.3 + New Features

QTabWidget - unable to load multiple documents in multiple tabs

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 564 Views 1 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.
  • B Offline
    B Offline
    blossomsg
    wrote on last edited by
    #1

    Hello All,

    I am loading multiple pdf files and it should open in multiple tabs - but instead the tab gets created but the files does not load

    Seeking for some suggestions and inputs for the tool.

    tool gif for reference - let me know if you are unable to see the gif from the link

    just for reference uploading images as well
    af958943-9378-4ee1-9c4a-050011f33913-image.png
    45dbceb6-9bc5-4beb-9819-996b4ac33911-image.png

    reference - https://doc.qt.io/qtforpython-6/examples/example_pdfwidgets_pdfviewer.html

    import os
    import sys
    
    from PySide6.QtCore import QUrl, Qt, QStandardPaths, Signal, Slot, QSettings
    from PySide6.QtGui import QAction
    from PySide6.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, QVBoxLayout, QPushButton, QSizePolicy, \
        QFileDialog
    
    from PySide6.QtPdf import QPdfDocument
    from PySide6.QtPdfWidgets import QPdfView
    
    
    class PdfViewWid(QPdfView):
        def __init__(self, pdf_document: QPdfDocument, parent=None) -> None:
            super().__init__()
    
            self.setPageMode(self.PageMode.MultiPage)
            self.setDocument(pdf_document)
    
    
    class PdfDialogWid(QFileDialog):
        def __init__(self, parent=None) -> None:
            super().__init__()
    
            self.settings = QSettings("Test", "Test_Settings")
    
            self.setNameFilter("Adobe PDF Files (*.pdf)")
            self.setViewMode(QFileDialog.ViewMode.Detail)
            self.setAcceptMode(self.AcceptMode.AcceptOpen)
    
    
    class Imp(QMainWindow):
        def __init__(self):
            super().__init__(parent=None)
    
            self.open_action = QAction("Open")
            self.open_action.setShortcut("Ctrl+O")
            self.quit_action = QAction("Exit")
            self.quit_action.setShortcut("Ctrl+Q")
            self.quit_action.triggered.connect(self.close)
    
            self.file_menu = self.menuBar().addMenu("File")
            self.file_menu.addAction(self.open_action)
            self.file_menu.addAction(self.quit_action)
    
            self.tab = QTabWidget()
            self.tab.setTabShape(self.tab.TabShape.Triangular)
            self.tab.setTabsClosable(True)
            self.tab.setMovable(True)
            self.tab.setDocumentMode(True)
    
            self.dialog = PdfDialogWid()
            self.open_action.triggered.connect(self.launch_browser)
    
            self.setCentralWidget(self.tab)
    
        def launch_browser(self):
            documents_path = QStandardPaths.writableLocation(
                QStandardPaths.StandardLocation.DocumentsLocation
            )
            default_path = self.dialog.settings.value(
                "dialog_last_directory", documents_path
            )
            file_path, _ = QFileDialog.getOpenFileName(
                self, "Open PDF File", str(default_path), "PDF Files (*.pdf)"
            )
            print(file_path)
            if file_path:
                self.create_tab(file_path)
                self.dialog.settings.setValue(
                    "dialog_last_directory", os.path.dirname(file_path)
                )
    
        def create_tab(self, file_path) -> None:
            pdf_document = QPdfDocument()
            pdf_view = PdfViewWid(pdf_document)
            wid = QWidget()
            self.tab.addTab(wid, "New Tab")
            vlayout = QVBoxLayout(wid)
            vlayout.addWidget(pdf_view)
            wid.setLayout(vlayout)
            pdf_document.load(QUrl.fromLocalFile(file_path).toLocalFile())
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Imp()
        window.show()
        sys.exit(app.exec())
    
    JonBJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      blossomsg
      wrote on last edited by
      #5

      Hello @JonB ,
      thanks for the status command, i actually got to know the issue from there, you were correct it was not tab, in the earlier tests it worked fine without tabs and then after adding tab it failed and so i got confused. But thanks for the suggestion.

      test link

      6170c93e-7cd9-4cd7-bc5d-c32cee3e42b6-image.png
      bfc53db9-fd96-4931-8b6d-896feaedd85e-image.png

      below is the refactored code, and a rough code

      import os
      import sys
      
      from PySide6.QtCore import QUrl, Qt, QStandardPaths, Signal, Slot, QSettings
      from PySide6.QtGui import QAction
      from PySide6.QtPdf import QPdfDocument
      from PySide6.QtPdfWidgets import QPdfView
      from PySide6.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, QVBoxLayout, QPushButton, QSizePolicy, \
          QFileDialog
      
      
      class PdfDialogWid(QFileDialog):
          def __init__(self, parent=None) -> None:
              super().__init__()
      
              self.settings = QSettings("Test", "Test_Settings")
      
              self.setNameFilter("Adobe PDF Files (*.pdf)")
              self.setViewMode(QFileDialog.ViewMode.Detail)
              self.setAcceptMode(self.AcceptMode.AcceptOpen)
      
      
      class Imp(QMainWindow):
          def __init__(self):
              super().__init__(parent=None)
      
              self.open_action = QAction("Open")
              self.open_action.setShortcut("Ctrl+O")
              self.quit_action = QAction("Exit Absorb")
              self.quit_action.setShortcut("Ctrl+Q")
              self.quit_action.triggered.connect(self.close)
      
              self.file_menu = self.menuBar().addMenu("File")
              self.file_menu.addAction(self.open_action)
              self.file_menu.addAction(self.quit_action)
      
              self.dialog = PdfDialogWid()
      
              self.tab = QTabWidget()
              self.tab.setTabsClosable(True)
              self.tab.setMovable(True)
              self.tab.setDocumentMode(True)
      
              self.setCentralWidget(self.tab)
      
              self.open_action.triggered.connect(self.load)
              self.tab.tabCloseRequested.connect(self.remove_tab)
      
          def load(self):
              documents_path = QStandardPaths.writableLocation(
                  QStandardPaths.StandardLocation.DocumentsLocation
              )
              default_path = self.dialog.settings.value(
                  "dialog_last_directory", documents_path
              )
              file_path, _ = QFileDialog.getOpenFileName(
                  self, "Open PDF File", str(default_path), "PDF Files (*.pdf)"
              )
              if file_path:
                  wid = QWidget()
                  pdf_view = QPdfView()
                  pdf_doc = QPdfDocument(pdf_view)
                  pdf_view.setDocument(pdf_doc)
                  pdf_view.setPageMode(pdf_view.PageMode.MultiPage)
                  pdf_doc.load(QUrl.fromLocalFile(file_path).toLocalFile())
                  self.tab.addTab(wid, "Trial")
                  vl = QVBoxLayout(wid)
                  vl.addWidget(pdf_view)
                  wid.setLayout(vl)
                  print(pdf_doc.status())
      
          def remove_tab(self, index: int) -> None:
              self.tab.removeTab(index)
      
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          window = Imp()
          window.show()
          sys.exit(app.exec())
      
      
      1 Reply Last reply
      0
      • B blossomsg

        Hello All,

        I am loading multiple pdf files and it should open in multiple tabs - but instead the tab gets created but the files does not load

        Seeking for some suggestions and inputs for the tool.

        tool gif for reference - let me know if you are unable to see the gif from the link

        just for reference uploading images as well
        af958943-9378-4ee1-9c4a-050011f33913-image.png
        45dbceb6-9bc5-4beb-9819-996b4ac33911-image.png

        reference - https://doc.qt.io/qtforpython-6/examples/example_pdfwidgets_pdfviewer.html

        import os
        import sys
        
        from PySide6.QtCore import QUrl, Qt, QStandardPaths, Signal, Slot, QSettings
        from PySide6.QtGui import QAction
        from PySide6.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, QVBoxLayout, QPushButton, QSizePolicy, \
            QFileDialog
        
        from PySide6.QtPdf import QPdfDocument
        from PySide6.QtPdfWidgets import QPdfView
        
        
        class PdfViewWid(QPdfView):
            def __init__(self, pdf_document: QPdfDocument, parent=None) -> None:
                super().__init__()
        
                self.setPageMode(self.PageMode.MultiPage)
                self.setDocument(pdf_document)
        
        
        class PdfDialogWid(QFileDialog):
            def __init__(self, parent=None) -> None:
                super().__init__()
        
                self.settings = QSettings("Test", "Test_Settings")
        
                self.setNameFilter("Adobe PDF Files (*.pdf)")
                self.setViewMode(QFileDialog.ViewMode.Detail)
                self.setAcceptMode(self.AcceptMode.AcceptOpen)
        
        
        class Imp(QMainWindow):
            def __init__(self):
                super().__init__(parent=None)
        
                self.open_action = QAction("Open")
                self.open_action.setShortcut("Ctrl+O")
                self.quit_action = QAction("Exit")
                self.quit_action.setShortcut("Ctrl+Q")
                self.quit_action.triggered.connect(self.close)
        
                self.file_menu = self.menuBar().addMenu("File")
                self.file_menu.addAction(self.open_action)
                self.file_menu.addAction(self.quit_action)
        
                self.tab = QTabWidget()
                self.tab.setTabShape(self.tab.TabShape.Triangular)
                self.tab.setTabsClosable(True)
                self.tab.setMovable(True)
                self.tab.setDocumentMode(True)
        
                self.dialog = PdfDialogWid()
                self.open_action.triggered.connect(self.launch_browser)
        
                self.setCentralWidget(self.tab)
        
            def launch_browser(self):
                documents_path = QStandardPaths.writableLocation(
                    QStandardPaths.StandardLocation.DocumentsLocation
                )
                default_path = self.dialog.settings.value(
                    "dialog_last_directory", documents_path
                )
                file_path, _ = QFileDialog.getOpenFileName(
                    self, "Open PDF File", str(default_path), "PDF Files (*.pdf)"
                )
                print(file_path)
                if file_path:
                    self.create_tab(file_path)
                    self.dialog.settings.setValue(
                        "dialog_last_directory", os.path.dirname(file_path)
                    )
        
            def create_tab(self, file_path) -> None:
                pdf_document = QPdfDocument()
                pdf_view = PdfViewWid(pdf_document)
                wid = QWidget()
                self.tab.addTab(wid, "New Tab")
                vlayout = QVBoxLayout(wid)
                vlayout.addWidget(pdf_view)
                wid.setLayout(vlayout)
                pdf_document.load(QUrl.fromLocalFile(file_path).toLocalFile())
        
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = Imp()
            window.show()
            sys.exit(app.exec())
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @blossomsg
        I don't see what this should have to do with tabs, those are just created on every call to create_tab(). You would get empty tabs if either the PDF document can't be parsed properly or can't be rendered. So have you tried loading the PDF files and showing them in QPdfView quite outside or using any tabs? Then you would know where the problem might lie.

        Attach slot to QPdfDocument::statusChanged(QPdfDocument::Status status) and verify it goes through states till QPdfDocument::Status::Ready. Then we know whether there is an issue in reading the PDF or displaying it.

        B 1 Reply Last reply
        0
        • JonBJ JonB

          @blossomsg
          I don't see what this should have to do with tabs, those are just created on every call to create_tab(). You would get empty tabs if either the PDF document can't be parsed properly or can't be rendered. So have you tried loading the PDF files and showing them in QPdfView quite outside or using any tabs? Then you would know where the problem might lie.

          Attach slot to QPdfDocument::statusChanged(QPdfDocument::Status status) and verify it goes through states till QPdfDocument::Status::Ready. Then we know whether there is an issue in reading the PDF or displaying it.

          B Offline
          B Offline
          blossomsg
          wrote on last edited by
          #3

          okay noted, I'll give it a try and revert, Thanks @JonB

          Pl45m4P 1 Reply Last reply
          0
          • B blossomsg

            okay noted, I'll give it a try and revert, Thanks @JonB

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #4

            @blossomsg

            Next you could try a standard QPdfView instead of your PdfViewWid subclass.
            Does it show your documents?


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            B 1 Reply Last reply
            0
            • B Offline
              B Offline
              blossomsg
              wrote on last edited by
              #5

              Hello @JonB ,
              thanks for the status command, i actually got to know the issue from there, you were correct it was not tab, in the earlier tests it worked fine without tabs and then after adding tab it failed and so i got confused. But thanks for the suggestion.

              test link

              6170c93e-7cd9-4cd7-bc5d-c32cee3e42b6-image.png
              bfc53db9-fd96-4931-8b6d-896feaedd85e-image.png

              below is the refactored code, and a rough code

              import os
              import sys
              
              from PySide6.QtCore import QUrl, Qt, QStandardPaths, Signal, Slot, QSettings
              from PySide6.QtGui import QAction
              from PySide6.QtPdf import QPdfDocument
              from PySide6.QtPdfWidgets import QPdfView
              from PySide6.QtWidgets import QTabWidget, QWidget, QMainWindow, QApplication, QVBoxLayout, QPushButton, QSizePolicy, \
                  QFileDialog
              
              
              class PdfDialogWid(QFileDialog):
                  def __init__(self, parent=None) -> None:
                      super().__init__()
              
                      self.settings = QSettings("Test", "Test_Settings")
              
                      self.setNameFilter("Adobe PDF Files (*.pdf)")
                      self.setViewMode(QFileDialog.ViewMode.Detail)
                      self.setAcceptMode(self.AcceptMode.AcceptOpen)
              
              
              class Imp(QMainWindow):
                  def __init__(self):
                      super().__init__(parent=None)
              
                      self.open_action = QAction("Open")
                      self.open_action.setShortcut("Ctrl+O")
                      self.quit_action = QAction("Exit Absorb")
                      self.quit_action.setShortcut("Ctrl+Q")
                      self.quit_action.triggered.connect(self.close)
              
                      self.file_menu = self.menuBar().addMenu("File")
                      self.file_menu.addAction(self.open_action)
                      self.file_menu.addAction(self.quit_action)
              
                      self.dialog = PdfDialogWid()
              
                      self.tab = QTabWidget()
                      self.tab.setTabsClosable(True)
                      self.tab.setMovable(True)
                      self.tab.setDocumentMode(True)
              
                      self.setCentralWidget(self.tab)
              
                      self.open_action.triggered.connect(self.load)
                      self.tab.tabCloseRequested.connect(self.remove_tab)
              
                  def load(self):
                      documents_path = QStandardPaths.writableLocation(
                          QStandardPaths.StandardLocation.DocumentsLocation
                      )
                      default_path = self.dialog.settings.value(
                          "dialog_last_directory", documents_path
                      )
                      file_path, _ = QFileDialog.getOpenFileName(
                          self, "Open PDF File", str(default_path), "PDF Files (*.pdf)"
                      )
                      if file_path:
                          wid = QWidget()
                          pdf_view = QPdfView()
                          pdf_doc = QPdfDocument(pdf_view)
                          pdf_view.setDocument(pdf_doc)
                          pdf_view.setPageMode(pdf_view.PageMode.MultiPage)
                          pdf_doc.load(QUrl.fromLocalFile(file_path).toLocalFile())
                          self.tab.addTab(wid, "Trial")
                          vl = QVBoxLayout(wid)
                          vl.addWidget(pdf_view)
                          wid.setLayout(vl)
                          print(pdf_doc.status())
              
                  def remove_tab(self, index: int) -> None:
                      self.tab.removeTab(index)
              
              
              if __name__ == "__main__":
                  app = QApplication(sys.argv)
                  window = Imp()
                  window.show()
                  sys.exit(app.exec())
              
              
              1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @blossomsg

                Next you could try a standard QPdfView instead of your PdfViewWid subclass.
                Does it show your documents?

                B Offline
                B Offline
                blossomsg
                wrote on last edited by
                #6

                @Pl45m4 said in QTabWidget - unable to load multiple documents in multiple tabs:

                Next you could try a standard QPdfView instead of your PdfViewWid subclass.

                Hey @Pl45m4 ,
                thanks for jumping in,
                you mean there is a direct module of QPdfView ? instead of from PySide6.QtPdfWidgets import QPdfView
                is there a document page?
                the one that i am referring is pdfview

                jsulmJ JonBJ 2 Replies Last reply
                0
                • B Offline
                  B Offline
                  blossomsg
                  wrote on last edited by
                  #7

                  and guys a silly question, how can i change the title/subject of the topic? so that it is not misleading

                  1 Reply Last reply
                  0
                  • B blossomsg has marked this topic as solved on
                  • B blossomsg has marked this topic as unsolved on
                  • B blossomsg

                    @Pl45m4 said in QTabWidget - unable to load multiple documents in multiple tabs:

                    Next you could try a standard QPdfView instead of your PdfViewWid subclass.

                    Hey @Pl45m4 ,
                    thanks for jumping in,
                    you mean there is a direct module of QPdfView ? instead of from PySide6.QtPdfWidgets import QPdfView
                    is there a document page?
                    the one that i am referring is pdfview

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

                    @blossomsg said in QTabWidget - unable to load multiple documents in multiple tabs:

                    is there a document page?

                    Just search for QPdfView.
                    https://doc.qt.io/qt-6/qpdfview.html

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

                    1 Reply Last reply
                    1
                    • B blossomsg

                      @Pl45m4 said in QTabWidget - unable to load multiple documents in multiple tabs:

                      Next you could try a standard QPdfView instead of your PdfViewWid subclass.

                      Hey @Pl45m4 ,
                      thanks for jumping in,
                      you mean there is a direct module of QPdfView ? instead of from PySide6.QtPdfWidgets import QPdfView
                      is there a document page?
                      the one that i am referring is pdfview

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

                      @blossomsg said in QTabWidget - unable to load multiple documents in multiple tabs:

                      you mean there is a direct module of QPdfView ? instead of from PySide6.QtPdfWidgets import QPdfView

                      No, he meant try using just the base QPdfView rather than your class PdfViewWid(QPdfView) subclass. In case that made a difference, which I don't think it will in this case. Especially since you say you got it working anyway :)

                      Pl45m4P 1 Reply Last reply
                      2
                      • B Offline
                        B Offline
                        blossomsg
                        wrote on last edited by
                        #10

                        Thank You all for the reverts, will close the ticket for now 😃

                        1 Reply Last reply
                        0
                        • B blossomsg has marked this topic as solved on
                        • JonBJ JonB

                          @blossomsg said in QTabWidget - unable to load multiple documents in multiple tabs:

                          you mean there is a direct module of QPdfView ? instead of from PySide6.QtPdfWidgets import QPdfView

                          No, he meant try using just the base QPdfView rather than your class PdfViewWid(QPdfView) subclass. In case that made a difference, which I don't think it will in this case. Especially since you say you got it working anyway :)

                          Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by
                          #11

                          @JonB said in QTabWidget - unable to load multiple documents in multiple tabs:

                          In case that made a difference

                          In case @blossomsg forgot to implement some function which is needed for QPdfView or so...
                          Like Q[...]Model derived class without implementing setData

                          Good that it works now


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          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