gpt4 book ai didi

python - 将动态数据添加到子类 QAbstractTableModel

转载 作者:行者123 更新时间:2023-12-04 10:11:27 25 4
gpt4 key购买 nike

出于测试目的,有一个函数要求输入并将其显示在 QListView 中。
display data .
这适用于一个 dim.data
我想扩展脚本显示二维数据

传入的数据应该插入第一行直到第 3 列,然后继续下一行(相同的操作),因此模型包含这种形式的数据

 input_values = [[1,2],
[1,2]]....

到目前为止,我实现了将 input_data 添加到第一行,但脚本在开始将数据添加到第二行之前停止运行。我刚得到这个信息
Process finished with exit code -1073740791 (0xC0000409)
这种行为是什么以及如何解决这个问题?
    def thread_data_add(self, data):
if len(self.model.input_data[0]) <= 1:
self.model.input_data[0].append(data)
self.model.layoutChanged.emit()

elif len(self.model.input_data[0]) > 1:
self.model.input_data.append([])
self.model.input_data[1].append(data) #crashes here !!! need crash report
self.model.layoutChanged.emit()


完整代码
#!/usr/bin/env python

"""


"""


import threading
import sys
import logging

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg


class ViewModel(qtc.QAbstractTableModel):


def __init__(self, input_data=None):
super().__init__()
self.input_data = input_data or [[]]
#


def data(self, index, role): # parameter index, role are needed !
"""

:param index: Instance of QModelIndex Class / set automatically
Model indexes refer to items in models,
contain all the information required to specify their locations in those models

:param role: what type should be returned
:return: data as a string
"""
if role == qtc.Qt.DisplayRole:
row = index.row()
column = index.column()
text = self.input_data[row][column]
return text



def rowCount(self, index):
return len(self.input_data)

def columnCount(self, index):
return len(self.input_data[0])


def getData(self):
print(self.input_data)
# todo how to show list data





class MainWindow(qtw.QWidget):

core_signal = qtc.pyqtSignal(str)


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

# View
table_view = qtw.QTableView()
self.model = ViewModel()
table_view.setModel(self.model)

# widgets
self.start_thread_button = qtw.QPushButton("start Thread")
self.test_button = qtw.QPushButton("display data")

# layout
qvboxlayout = qtw.QVBoxLayout()
qvboxlayout.addWidget(table_view)

qvboxlayout.addWidget(self.start_thread_button)
qvboxlayout.addWidget(self.test_button)
self.setLayout(qvboxlayout)
self.show()


# self.logger = self.default_logger()


# function

self.start_thread_button.clicked.connect(self.start)
self.test_button.clicked.connect(self.model.getData)


def lambda_thread_data_add(self, data):
if len(self.model.input_data[0]) <= 1:
self.model.input_data[0].append(data)
self.model.layoutChanged.emit()
# self.model.input_data.append([])
elif len(self.model.input_data[0]) > 1:
self.model.input_data.append([])
self.model.input_data[1].append(data) #crashes here !!! need crash report
self.model.layoutChanged.emit()



def thread_data_add(self, data):
if len(self.model.input_data[0]) <= 1:
self.model.input_data[0].append(data)
self.model.layoutChanged.emit()
# self.model.input_data.append([])
elif len(self.model.input_data[0]) > 1:
self.model.input_data.append([])
self.model.input_data[1].append(data) #crashes here !!! need crash report
self.model.layoutChanged.emit()



def start(self):
# starts thread
# Setting thread.daemon = True will allow the main program to exit before thread is killed.
threading.Thread(target=self._execute, daemon=True).start()
self.core_signal.connect(self.thread_data_add)


def _execute(self):
while True:
user_input = input("type in: ")
self.core_signal.emit(user_input) # transmit data


def default_logger(self,level=logging.DEBUG, save_path='beispiel.log'):
# create logger
custom_logger = logging.getLogger(__name__)
custom_logger.setLevel(level)

# was reingegeben werden soll
formatter = logging.Formatter('%(asctime)s;%(message)s;%(filename)s;%(lineno)d', "%Y-%m-%d %H:%M:%S")
file_handler = logging.FileHandler(save_path)
file_handler.setFormatter(formatter)
custom_logger.addHandler(file_handler)

stream_handler = logging.StreamHandler()
custom_logger.addHandler(stream_handler)

return custom_logger

if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())






最佳答案

您遇到的问题是 IndexError,因为数据没有被统一添加(一次整行),而是一次添加一列或单元格。因此,当从第 3 个输入开始下一行时,该行中的第 2 列还没有数据。您不必更改插入数据的方式,只需捕获错误并提供空字符串或 None 值即可。

def data(self, index, role):
if role == qtc.Qt.DisplayRole:
try:
text = self.input_data[index.row()][index.column()]
except IndexError:
text = None
return text

此外,请确保您正在访问正确的索引 input_datathread_data_add() ,这将是最后一个索引 [-1],因为您要附加数据。
def thread_data_add(self, data):
if len(self.model.input_data[-1]) < 2:
self.model.input_data[-1].append(data)
else:
self.model.input_data.append([data])
self.model.layoutChanged.emit()

关于python - 将动态数据添加到子类 QAbstractTableModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61320830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com