QTreeView崩溃没有明显原因

时间:2016-02-27 20:01:55

标签: pyqt python-3.4 qt4.8

我在我正在制作的程序的GUI中引入了一个树视图,因为一旦设置完成后我尝试更改它的模型就会崩溃。

行动方针是:

  
      
  • 使用文件对话
  • 加载文件   
  • 清除接口对象(表和树视图)上的模型。树视图第一次没有受到影响,因为没有模型   它。
  •   
  • 填充树视图模型。
  •   
  • 与此问题无关的其他内容。
  •   

有问题的功能是; 文件加载程序:

def open_file(self):
        """
        Open a file
        :return:
        """
        print("actionOpen_file_click")
        # declare the dialog
        # file_dialog = QtGui.QFileDialog(self)
        # declare the allowed file types
        files_types = "Excel 97 (*.xls);;Excel (*.xlsx);;DigSILENT (*.dgs);;MATPOWER (*.m)"
        # call dialog to select the file
        filename, type_selected = QtGui.QFileDialog.getOpenFileNameAndFilter(self, 'Open file',
                                                                       self.project_directory, files_types)

        if len(filename) > 0:
            self.project_directory = os.path.dirname(filename)
            print(filename)
            self.circuit = Circuit(filename, True)

            # set data structures list model
            self.ui.dataStructuresListView.setModel(self.available_data_structures_listModel)
            # set the first index
            index = self.available_data_structures_listModel.index(0, 0, QtCore.QModelIndex())
            self.ui.dataStructuresListView.setCurrentIndex(index)

            # clean
            self.clean_GUI()

            # load table
            self.display_objects_table()

            # draw graph
            self.ui.gridPlot.setTitle(os.path.basename(filename))
            self.re_plot()

            # show times
            if self.circuit.time_series is not None:
                if self.circuit.time_series.is_ready():
                    self.set_time_comboboxes()

            # tree view at the results
            self.set_results_treeview_structure()

            # populate editors
            self.populate_editors_defaults()

树视图模型分配:

def set_results_treeview_structure(self):
        """
        Sets the results treeview data structure
        @return:
        """

        # self.ui.results_treeView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        model = QtGui.QStandardItemModel()
        # model.setHorizontalHeaderLabels(['Elements'])
        self.ui.results_treeView.setModel(model)
        # self.ui.results_treeView.setUniformRowHeights(True)

        def pass_to_QStandardItem_list(list_):
            res = list()
            for elm in list_:
                elm1 = QtGui.QStandardItem(elm)
                elm1.setEditable(False)
                res.append(elm1)
            return res

        bus_results = pass_to_QStandardItem_list(['Voltages (p.u.)', 'Voltages (kV)'])
        per_bus_results = pass_to_QStandardItem_list(['Voltage (p.u.) series', 'Voltage (kV) series',
                                                      'Active power (MW)', 'Reactive power (MVar)',
                                                      'Active and reactive power (MW, MVar)', 'Aparent power (MVA)',
                                                      'S-V curve', 'Q-V curve'])

        branches_results = pass_to_QStandardItem_list(['Loading (%)', 'Current (p.u.)',
                                                       'Current (kA)', 'Losses (MVA)'])
        per_branch_results = pass_to_QStandardItem_list(['Loading (%) series', 'Current (p.u.) series',
                                                         'Current (kA) series', 'Losses (MVA) series'])

        generator_results = pass_to_QStandardItem_list(['Reactive power (p.u.)', 'Reactive power (MVar)'])
        per_generator_results = pass_to_QStandardItem_list(['Reactive power (p.u.) series',
                                                            'Reactive power (MVar) series'])

        self.family_results_per_family = dict()

        # nodes
        buses = QtGui.QStandardItem('Buses')
        buses.setEditable(False)
        buses.appendRows(bus_results)
        self.family_results_per_family[0] = len(bus_results)
        names = self.circuit.bus_names
        for name in names:
            bus = QtGui.QStandardItem(name)
            bus.appendRows(per_bus_results)
            bus.setEditable(False)
            buses.appendRow(bus)

        # branches
        branches = QtGui.QStandardItem('Branches')
        branches.setEditable(False)
        branches.appendRows(branches_results)
        self.family_results_per_family[1] = len(branches_results)
        names = self.circuit.branch_names
        for name in names:
            branch = QtGui.QStandardItem(name)
            branch.appendRows(per_branch_results)
            branch.setEditable(False)
            branches.appendRow(branch)

        # generators
        generators = QtGui.QStandardItem('Generators')
        generators.setEditable(False)
        generators.appendRows(generator_results)
        self.family_results_per_family[2] = len(generator_results)
        names = self.circuit.gen_names
        for name in names:
            gen = QtGui.QStandardItem(name)
            gen.appendRows(per_generator_results)
            gen.setEditable(False)
            generators.appendRow(gen)

        model.appendRow(buses)
        model.appendRow(branches)
        model.appendRow(generators)

GUI" clean":

def clean_GUI(self):
        """
        Initializes the comboboxes and tables
        Returns:

        """
        self.ui.tableView.setModel(None)
        if self.ui.results_treeView.model() is not None:
            self.ui.results_treeView.model().clear()

        self.ui.profile_time_selection_comboBox.clear()
        self.ui.results_time_selection_comboBox.clear()
        self.ui.gridPlot.clear()

可以看到完整的代码here

我已经看到这种行为通常是由GUI线程之外的调用触发的,我不认为是这种情况。 如果有人能够指出问题,我会很感激。同样,测试的complate代码是here

1 个答案:

答案 0 :(得分:0)

在我的案例中解决这个问题的方法如下:

代码中名为model的QStandardItemModel()变量被转换为类全局变量self.tree_model

当我想要替换treeview对象模型时,我会删除带有tree_model的全局del self.tree_model

然后我用self.tree_model = QStandardItemModel()

重新创建全局模型

这样可以有效地替换TreeView对象模型而不会崩溃......