简体   繁体   中英

insert dynamic data into QTableWidget

I want to populate a QTableWidget with incoming data from user input

this function should insert each incoming input row by row in "real time", so far its inserts the input in all rows at the same time what am I missing here?

 @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

full code

#!/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_())
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

Data doesn't change in this contest so cell = qtw.QTableWidgetItem(str(data)) would remain the same for all iterations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM