Я хочу заполнить QTableWidget
входящими данными от пользовательского ввода
, эта функция должна вставлять каждую входящую строку ввода строк за строкой в режиме "реального времени", пока она вставляет входные данные во все строки на в то же время, что я здесь упускаю?
@qtc.pyqtSlot(str)
def add_dynamicdata(self, data):
row = 0
col = 0
for i in range(self.table_widget.rowCount()):
# insert inputdata in all cells at the same time !
cell = qtw.QTableWidgetItem(str(data))
self.table_widget.setItem(row, col, cell)
row += 1
полный код
#!/usr/bin/env python
"""
"""
import sys
import threading
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
class LambdaTableWidget(qtw.QWidget):
# signals
core_signal = qtc.pyqtSignal(str)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# position
qtRectangle = self.frameGeometry()
centerPoint = qtw.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
# size
self.resize(1400, 710)
# frame title
self.setWindowTitle("QTableWidget Test")
# heading
heading_label = qtw.QLabel('better then excel')
heading_label.setAlignment(qtc.Qt.AlignHCenter | qtc.Qt.AlignTop)
# add Button
self.setdata_button = qtw.QPushButton("insert data")
self.test_button = qtw.QPushButton("test feature")
# table Widget
self.table_widget = qtw.QTableWidget(5, 1)
# name colums, rows
colum_label = ["Weight"]
row_label = ["row 1", "row 2", "row 3", "row 4", "row 5"]
self.table_widget.setHorizontalHeaderLabels(colum_label)
self.table_widget.setVerticalHeaderLabels(row_label)
# layout
self.main_layout = qtw.QGridLayout()
self.main_layout.addWidget(heading_label, 0, 0)
self.main_layout.addWidget(self.table_widget, 1, 0)
self.main_layout.addWidget(self.setdata_button, 2, 0)
self.setLayout(self.main_layout)
self.show()
# functionality
self.setdata_button.clicked.connect(self.start)
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.add_dynamicdata)
def _execute(self):
while True:
user_input = input("type in: ")
self.core_signal.emit(user_input) # transmit data
@qtc.pyqtSlot(str)
def add_dynamicdata(self, data):
row = 0
col = 0
for i in range(self.table_widget.rowCount()):
# todo fix the bug !!!
# insert inputdata in all cells at the same time !
cell = qtw.QTableWidgetItem(str(data))
self.table_widget.setItem(row, col, cell)
row += 1
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = LambdaTableWidget()
sys.exit(app.exec_())