代码之家  ›  专栏  ›  技术社区  ›  F0AX

当我在QTreeView中打开一个包含扩展名为.json或.js的文件的文件夹时,PyQt5崩溃

  •  1
  • F0AX  · 技术社区  · 1 年前

    我在Ubuntu LTS 24.04上使用VSCode。 我使用PyQt5。

    我的项目根路径中有以下文件夹,其中包含文件:

    Folder structure with files which cause problems

    我使用以下代码:

    from pathlib import Path
    from PyQt5 import QtWidgets, uic
    from PyQt5.QtWidgets import QTreeView, QFileSystemModel
    
    
    DATA_PATH = Path('data')
    
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
        
            uic.loadUi('windows/basic.ui', self)
            self.tree_view = self.findChild(QTreeView, 'treeView')
            model = QFileSystemModel(self)
    
            model.setRootPath(str(DATA_PATH))
            self.tree_view.setModel(model)
            self.tree_view.setRootIndex(model.index(str(DATA_PATH)))
    
            self.show()
    
    
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    app.exec_()
    

    这是basic.ui文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>497</width>
        <height>372</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="central_widget">
       <property name="styleSheet">
        <string notr="true"/>
       </property>
       <widget class="QTreeView" name="treeView">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>20</y>
          <width>461</width>
          <height>291</height>
         </rect>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>497</width>
         <height>24</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuFiles">
        <property name="title">
         <string>Files</string>
        </property>
        <addaction name="action_Create_File"/>
        <addaction name="actionLoad_File"/>
        <addaction name="separator"/>
        <addaction name="action_Import_File"/>
        <addaction name="action_Export_File"/>
        <addaction name="separator"/>
       </widget>
       <addaction name="menuFiles"/>
      </widget>
      <action name="actionOpen_file">
       <property name="text">
        <string>Open file</string>
       </property>
      </action>
      <action name="actionOpen_recent">
       <property name="text">
        <string>Open recent</string>
       </property>
      </action>
      <action name="action_Create_File">
       <property name="text">
        <string>&amp;New File</string>
       </property>
      </action>
      <action name="action_Import_File">
       <property name="text">
        <string>&amp;Import File</string>
       </property>
      </action>
      <action name="action_Export_File">
       <property name="text">
        <string>&amp;Export File</string>
       </property>
      </action>
      <action name="actionLoad_File">
       <property name="text">
        <string>Load File</string>
       </property>
      </action>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    现在,当我试图展开包含所有JSON文件的文件夹“2024”时,程序崩溃了。 但是当我展开包含python和文本文件的文件夹“2025”时,它工作得很好。 当文件夹中有一个扩展名为Javascript的文件时,我也试图展开该文件夹,但该文件也崩溃了。

    我试图将扩展名从.json更改为.json,这很有效。 我试图打开项目外的文件夹,其行为与在项目文件夹中的行为相同。

    我希望QTreeView能够展开文件夹,无论它包含哪个文件。 在图片上,你可以看到我想要的行为: Image of the QTreeView Widget with expanded folder 2025文件夹会展开,但当我尝试展开2024文件夹时,程序会崩溃。

    我通过VSCode中的集成终端启动程序 python __main__.py . QT调试环境时,PyQt5没有调试日志: export QT_DEBUG_PLUGINS=1 程序只是停止响应,如下图所示: Visual Studio Code is not responing

    编辑

    在VSCode之外执行时,该程序运行良好。

    0 回复  |  直到 1 年前