0

I want to insert text items from a Worker Qthread to a QTableWidget UI in Main thread ?.

I want to know the Syntax to create the signal in Main thread , so I can insert the text as well as the row and column from Worker thread by sending via signal

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.myclass2 = myclass2()
        self.myclass2.start()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('web.png'))
        self.show()


class myclass2(QThread):
    def __init__(self, parent=None):
        super(myclass2, self).__init__(parent)

    def run(self):
        while True:
            time.sleep(.1)
            print(" in thread \n")

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

1 Answer 1

2

You already know that you must use a signal, now the question is: what data do you need to send?, You can think that you should send the row, the column and the text, so the signal must send 2 whole and one string, then you connect it to a slot and in it you insert it as if the data was created in the main thread:

import sys
import time
import random
from PyQt5 import QtCore, QtWidgets


class Example(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        lay = QtWidgets.QVBoxLayout(self)

        self.table = QtWidgets.QTableWidget(10, 10)
        lay.addWidget(self.table)

        self.myclass2 = myclass2()
        self.myclass2.new_signal.connect(self.some_function)
        self.myclass2.start()

    @QtCore.pyqtSlot(int, int, str)
    def some_function(self, r, c, text):
        it = self.table.item(r, c)
        if it:
            it.setText(text)
        else:
            it = QtWidgets.QTableWidgetItem(text)
            self.table.setItem(r, c, it)

class myclass2(QtCore.QThread):
    new_signal = QtCore.pyqtSignal(int, int, str)

    def run(self):
        while True:
            time.sleep(.1)
            r = random.randint(0, 9)
            c = random.randint(0, 9)
            text = "some_text: {}".format(random.randint(0, 9))
            self.new_signal.emit(r, c, text)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    ex.showMaximized()
    sys.exit(app.exec_())
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks , Exactly what I needed
@aditya Do you already know how to transmit data from a secondary thread? I think that with the example and the explanation it is no longer necessary to ask: how to modify XXX of the GUI from another thread.
Actually , I understood that we need to do it by signal just confused with the syntax.
@aditya What confuses you?
mostly the signal concepts how to pass the strings or integer data from one particular class to the object of other but with this example, I think I understood how I can create new signals and pass my data as per

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.