MRE:
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5 import QtCore
import PyQt5
import sys, types
class OrgItemModel( QStandardItemModel ):
def __init__( self, tree_view ):
super().__init__()
self.tree_view = tree_view
self.setHorizontalHeaderLabels( [ 'Tasks', 'Due date', 'Notes' ] )
def configure( tree_view, main_window ):
tree_view.setModel( OrgItemModel( tree_view ) )
def load_from_list( self ):
new_model = OrgItemModel( self )
root_item = new_model.invisibleRootItem()
lines = [
'Europe ||| 2020-10-26 ||| some notes',
'==France ||| 2020-10-27 ||| some France notes',
'====Paris ||| --- ||| blah blah',
'==Germany ||| ||| ',
'==Italy ||| 2020-10-28 ||| some Italy notes',
'====Rome ||| ||| ',
'==Ireland ||| 2020-10-30 ||| blah blah',
]
new_model.ptr = 0
def read_child_rows( item, depth = 0 ):
while new_model.ptr < len( lines ):
line = lines[ new_model.ptr ]
new_model.ptr += 1
n_equals = len( line ) - len( line.lstrip('=') )
item_text = line.lstrip('=').strip()
read_depth = n_equals / 2
if read_depth == depth:
item_col0 = QStandardItem( item_text )
# split up the line by the substring ' ||| '
item_texts = line.split( ' ||| ' )
text_col0 = item_texts[ 0 ].lstrip( '=' ).strip()
text_col1 = item_texts[ 1 ].strip()
text_col2 = item_texts[ 2 ].strip()
item_col0 = QStandardItem( text_col0 )
if text_col1 == '':
print( f'col1 blank for {text_col0}')
item_col1 = None
else:
item_col1 = QStandardItem( text_col1 )
if text_col2 == '':
print( f'col2 blank for {text_col0}')
item_col2 = None
else:
item_col2 = QStandardItem( text_col2 )
if item_col1 == None or item_col2 == None:
print( 'inserting just first item...')
item.appendRow( item_col0 )
else:
item.appendRow( [ item_col0, item_col1, item_col2 ] )
# NB here I'm trying to examine the row which has just been added.
# But this is now not giving the right value for rowCount: this
# may be because previously I was experimenting with new_model.appendRow
# rather than item.appendRow: the central problem still arises...
print( f'row count {new_model.rowCount()}')
for j_col in range( 3 ):
index = new_model.index( new_model.rowCount() - 1, j_col )
data_at_coord = new_model.data( index )
print( data_at_coord, index.isValid() )
else:
new_model.ptr -= 1
if read_depth > depth:
read_child_rows( item_col0, depth + 1 )
else:
break
read_child_rows( root_item )
del new_model.ptr
self.setModel( new_model )
self.expandAll()
self.resizeColumnToContents( 0 )
tree_view.load_from_list = types.MethodType( load_from_list, tree_view )
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(1100, 580)
self.centralwidget = QWidget(MainWindow)
self.treeView = QTreeView(self.centralwidget)
self.treeView.setGeometry(QtCore.QRect(20, 20, 1000, 320 ))
MainWindow.setCentralWidget(self.centralwidget)
class MyWindow( QMainWindow):
def __init__(self):
super( MyWindow, self ).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
configure( self.ui.treeView, self )
self.ui.treeView.load_from_list()
app = QApplication([])
application = MyWindow()
application.show()
sys.exit(app.exec())
我对QStandardItemModel
方法insertRow
和appendRow
感到困惑。目前,我有一个模型中有3列的QTreeView
。我正在遍历一个列表,根据列表中的缩进(在每一深度级别的行开始处使用“==”)来表示树的深度,重新构建树
列表中的每一行用字符串“| | | |”(我计划在某个时候实现JSON序列化)分隔给定行上三个项目的文本
当我在“| | |”上拆分行文本时,将生成3个字符串。如果第二个或第三个是空白字符串,这意味着不应将此项添加到模型中,即在树视图中,只应看到树节点,其行的其余部分不应包含任何项(即无效索引)。
... 但是我发现,即使我只插入1个项目(而不是3个项目的列表),在QTreeView
的模型中的行中(有时)也会创建3个项目。上面检查行中的index.isValid()
总是返回True
,即使它只是插入了一项(在col0中)。在这种情况下,col1和col2的data_at_coord
被打印为“无”
因此,在显示的视图中,我可以单击col1或col2,找到一个空白的可编辑项,检查后发现这些是有效的索引。它们不应该是这样的:我的意图是添加一个新行,其中在col1和col2中没有项目,只有在col0中
当我运行上面的MRE时,“Rome”行的行为应该是这样的:您可以尝试单击col1和col2,但什么也没有发生但“德国”行的行为似乎很奇怪:当我单击col1或col2时,我发现我正在编辑一个空白字符串
注意:如果事实上您使用了new_model.appendRow
而不是item.appendRow
,则结果会有所不同:
问题不是由appendRow方法引起的,而是由未记录的行为引起的。看起来,对于树型模型,除了没有生成此意外行为的子节点的节点外,所有节点的列数始终相同。一种解决方法是在文本项中创建带有Qt::NoItemFlags标志的QStandardItem,这样用户就无法与它们交互:
相关问题 更多 >
编程相关推荐