QTabWidget - unable to load multiple documents in multiple tabs
-
wrote on 19 Nov 2024, 13:58 last edited by
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
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())
-
wrote on 19 Nov 2024, 19:37 last edited by
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.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())
-
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
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())
wrote on 19 Nov 2024, 14:26 last edited by@blossomsg
I don't see what this should have to do with tabs, those are just created on every call tocreate_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. -
@blossomsg
I don't see what this should have to do with tabs, those are just created on every call tocreate_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. -
wrote on 19 Nov 2024, 18:47 last edited by
Next you could try a standard
QPdfView
instead of yourPdfViewWid
subclass.
Does it show your documents? -
wrote on 19 Nov 2024, 19:37 last edited by
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.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())
-
Next you could try a standard
QPdfView
instead of yourPdfViewWid
subclass.
Does it show your documents?wrote on 19 Nov 2024, 19:43 last edited by@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 ofQPdfView
? instead offrom PySide6.QtPdfWidgets import QPdfView
is there a document page?
the one that i am referring is pdfview -
wrote on 19 Nov 2024, 19:44 last edited by
and guys a silly question, how can i change the title/subject of the topic? so that it is not misleading
-
-
-
@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 ofQPdfView
? instead offrom PySide6.QtPdfWidgets import QPdfView
is there a document page?
the one that i am referring is pdfview@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 -
@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 ofQPdfView
? instead offrom PySide6.QtPdfWidgets import QPdfView
is there a document page?
the one that i am referring is pdfviewwrote on 20 Nov 2024, 08:50 last edited by@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 yourclass 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 :) -
wrote on 20 Nov 2024, 08:54 last edited by
Thank You all for the reverts, will close the ticket for now 😃
-
-
@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 yourclass 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 :)wrote on 20 Nov 2024, 10:28 last edited by@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...
LikeQ[...]Model
derived class without implementingsetData
Good that it works now
1/11