python-3.x pyside6更新webview url只能在www.example.com中工作app.py,但不能作为导入的类

mbyulnm0  于 2022-12-01  发布在  Python
关注(0)|答案(1)|浏览(288)

我想点击QListView“widget_list_files”中的一个项目(这是一个pdf的文件路径),并更新QWebEngineView“视图”以显示pdf。如果我将所有内容放在同一个www.example.com中,它会正常工作app.py,但如果我将其移动到一个单独的文件中并导入类,QListView会被填充,但点击事件会被忽略。一个有趣的行为是,如果设置一些断点并启动vscode调试,它也可以与导入O.o
app.py

from modm.functions.cms import cms

class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.file_path = '/home/user/Downloads'

        layout_cms = QHBoxLayout()

        # Files Layout
        layout_files = QVBoxLayout()

        layout_files_search = QHBoxLayout()

        search_input_text = QLineEdit()
        search_input_text.setMaxLength(10)
        search_input_text.setPlaceholderText("Enter your text")
        layout_files_search.addWidget(search_input_text)

        search_button_search = QPushButton("Search")
        layout_files_search.addWidget(search_button_search)

        self.widget_list_files = QListView()
        self.widget_list_files.clicked.connect(self.open_document)
        self.model_files = QFileSystemModel()
        self.model_files.setNameFilterDisables(False)
        self.model_files.setFilter(QDir.Files)
        self.model_files.setNameFilters(["*.pdf"])

        layout_files.addLayout(layout_files_search)
        layout_files.addWidget(self.widget_list_files)

        layout_cms.addLayout( layout_files,stretch=2)

        # Document Layout
        self.view = QWebEngineView()
        self.view.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        self.view.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)
        layout_document = QVBoxLayout()
        layout_document.addWidget(self.view)

        # Document Layout -> buttons
        layout_document_buttons = QHBoxLayout()
        document_button_merge = QPushButton("Merge")
        layout_document_buttons.addWidget(document_button_merge)

        layout_document.addLayout(layout_document_buttons)
        layout_cms.addLayout(layout_document,stretch=3)

        self.model_files.setRootPath(self.file_path)
        self.widget_list_files.setModel(self.model_files)
        self.widget_list_files.setRootIndex(self.model_files.index(self.file_path))
        self.widget_cms = QWidget()
        self.widget_cms.setLayout(layout_cms)

        content_cms = cms()
        # Tab Menu
        tab_menu = QTabWidget()
        tab_menu.setTabPosition(QTabWidget.West)
        tab_menu.setMovable(True)
        # here I use the imported version
        tab_menu.addTab(content_cms.widget_cms, 'CMS')
        # here I use code from this file
        tab_menu.addTab(self.widget_cms, 'Merge')
        self.setCentralWidget(tab_menu)

    def open_document(self,index):
        self.view.setUrl(f"file://{self.model_files.filePath(index)}")
def run():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    app.exec()

if __name__ == '__main__':
    run()

cms.py

class cms():

    def __init__(self):
        self.file_path = '/home/user/Downloads'

        layout_cms = QHBoxLayout()

        # Files Layout
        layout_files = QVBoxLayout()

        layout_files_search = QHBoxLayout()

        search_input_text = QLineEdit()
        search_input_text.setMaxLength(10)
        search_input_text.setPlaceholderText("Enter your text")
        layout_files_search.addWidget(search_input_text)

        search_button_search = QPushButton("Search")
        layout_files_search.addWidget(search_button_search)

        self.widget_list_files = QListView()
        self.widget_list_files.clicked.connect(self.open_document)
        self.model_files = QFileSystemModel()
        self.model_files.setNameFilterDisables(False)
        self.model_files.setFilter(QDir.Files)
        self.model_files.setNameFilters(["*.pdf"])

        layout_files.addLayout(layout_files_search)
        layout_files.addWidget(self.widget_list_files)

        layout_cms.addLayout( layout_files,stretch=2)

        # Document Layout
        self.view = QWebEngineView()
        self.view.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
        self.view.settings().setAttribute(QWebEngineSettings.PdfViewerEnabled, True)
        layout_document = QVBoxLayout()
        layout_document.addWidget(self.view)

        # Document Layout -> buttons
        layout_document_buttons = QHBoxLayout()
        document_button_merge = QPushButton("Merge")

        layout_document.addLayout(layout_document_buttons)
        layout_cms.addLayout(layout_document,stretch=3)

        self.model_files.setRootPath(self.file_path)
        self.widget_list_files.setModel(self.model_files)
        self.widget_list_files.setRootIndex(self.model_files.index(self.file_path))
        self.widget_cms = QWidget()
        self.widget_cms.setLayout(layout_cms)

    def open_document(self,index):
        # if I set the breakpointn in the line below and randomly somewhere in __init__ it also works in this version in debug mode
        self.view.setUrl(f"file://{self.model_files.filePath(index)}")

我将完全相同的代码从www.example.com移动app.py到cms.py,我希望它能像在www.example.com中一样工作app.py,这意味着PDF文件(QListView“widget_list_files”中的一个项目)将显示在QWebEngineView“视图”中。

oknwwptz

oknwwptz1#

这就是解决方案musicamante
对www.example.com的更改app.py

# wrong and completely removed:
# content_cms = cms()
# Tab Menu
tab_menu = QTabWidget()
tab_menu.setTabPosition(QTabWidget.West)
tab_menu.setMovable(True)
# wrong:
# tab_menu.addTab(content_cms.widget_cms, 'CMS')
# right:
tab_menu.addTab(cms(), 'CMS')
self.setCentralWidget(tab_menu)

对www.example.com的更改cms.py

# wrong:
#class cms():
# right: inherit QWidget
class cms(QWidget):

    def __init__(self):
        # init QWidget 
        super(cms, self).__init__()
        ...
        # wrong
        #self.widget_cms = QWidget()
        #self.widget_cms.setLayout(layout_cms)
        # right:
        self.setLayout(layout_cms)

相关问题