使用嵌套的QStandardItems

时间:2017-03-22 22:07:11

标签: python qt model-view-controller pyqt pyside

我正在进行设置,我需要通过QStandardItemModel显示嵌套QStandardItems的{​​{1}}。我需要添加和删除我似乎无法弄清楚如何正确执行的列。

  • 要插入初始嵌套行,我使用QTreeView并始终使用上一项作为新父项。
  • 我在记录输出中可以看到最后一个appendRow([item, item, ...])的意外行和列数为0.
  • QStandardItem之后,项目的列数不会从3增加到4.而是在第0行和第1列的索引处插入新项目。

appendColumn()之前 - 列数3

Before appendColumn()

appendColumn()后 - 出现新列但列数仍为3

After appendColumn()

示例代码

appendColumn()

问题

  • 我在日志记录输出中可以看到最后一个# -*- coding: utf-8 -*- from __future__ import unicode_literals import logging import sys import uuid from PySide import QtCore from PySide import QtGui logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) class TestModel(QtGui.QStandardItemModel): def __init__(self, *args, **kwargs): super(TestModel, self).__init__(*args, **kwargs) self.logger = logging.getLogger(type(self).__name__) def set_data(self): """Fill with 3 nested rows with 3 columns each for testing.""" # cleanup self.clear() self.setHorizontalHeaderLabels(["header_1", "header_2", "header_3"]) # fill nested rows parent = None for index in range(3): row = [QtGui.QStandardItem(str(uuid.uuid4().hex[:4])) for _ in range(3)] self.appendRow(row) if (parent is None) else parent.appendRow(row) parent = row[0] def print_rows(self): """Print information about all rows of the model.""" for index in self.all_indices(): item = self.itemFromIndex(index) msg = "QModelIndex: row {0} column {1} " msg += "QStandardItem: row count {2} column count {3} " msg += "value: {4}" msg = msg.format( index.row(), index.column(), item.rowCount(), item.columnCount(), item.text()) self.logger.info(msg) def all_indices(self, indices=None, index=None): """Recurse all indices under index in the first column and return them as flat list. """ indices = indices if (isinstance(indices, list)) else [] index = index if (isinstance(index, QtCore.QModelIndex)) else QtCore.QModelIndex() for row in range(self.rowCount(index)): current_index = self.index(row, 0, index) indices.append(current_index) if (self.hasChildren(current_index)): self.all_indices(indices, current_index) return indices def append_column(self): self.logger.info("appendColumn()") self.appendColumn([QtGui.QStandardItem(str(uuid.uuid4().hex[:4])),]) # test if (__name__ == "__main__"): app = QtGui.QApplication(sys.argv) # widget widget = QtGui.QWidget() layout = QtGui.QVBoxLayout() widget.setLayout(layout) widget.show() # model model = TestModel() model.set_data() # view view = QtGui.QTreeView() view.setModel(model) layout.addWidget(view) # btn_print_rows btn_print_rows = QtGui.QPushButton("Prints rows") btn_print_rows.clicked.connect(model.print_rows) layout.addWidget(btn_print_rows) # btn_append_column btn_append_column = QtGui.QPushButton("Append column") btn_append_column.clicked.connect(model.append_column) layout.addWidget(btn_append_column) sys.exit(app.exec_()) 的意外行和列数。当模型清楚地显示似乎正确的数据时,为什么最后一项的行数和列数为0?
  • 如何使用嵌套QStandardItem (显示在QStandardItemModel 中)QStandardItems正确添加/删除列?
  • 我应该QTreeView列数等于标题项数吗? (每个新列的de / incrementing item.columnCount())。或者所有QStandardItems的列数是否为1?

0 个答案:

没有答案