简体   繁体   中英

Get variables emitted from thread by one signal to the main window in pyqt5?

I want to get values emitted by signal on thread (worker), the signal takes two arguments ( two lists, or list and int).It's work and the signal takes 2 arguments. But when I'm trying to get the values of this two list in the main window, I get only one list. So how can get the two lists in the main window from one signal?

class Worker(QThread):
    authResult = QtCore.pyqtSignal((list,list))
    def __init__(self):
       super(Worker, self).__init__()
       self.flag = True
       self.s="vide"
def run(self):
    self.auth()

def auth(self):
    c=0
    Malist=[]
    k=c+600
    while c<10:
        c+=1
        time.sleep(0.3)
        print(c)

        Malist.append(k)
        k=k+1

    Malist2=['a','b','c']
    self.authResult.emit( Malist,Malist2)

And this is the main window contains button, i want only to print values of two lists Malist and Malist2

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)

        self.setMinimumSize(QSize(300, 200))    
        self.setWindowTitle("PyQt button example ") 


        pybutton = QPushButton('Click me', self)
        pybutton.clicked.connect(self.clickMethod)
        pybutton.resize(100,32)
        pybutton.move(50, 50)


        pybutton2 = QPushButton('Afficher !!', self)
        pybutton2.clicked.connect(self.afficher)
        pybutton2.resize(60,32)
        pybutton2.move(160, 50)   

  def clickMethod(self):
    self.thread=Worker()
    self.thread.authResult.connect(self.handl)
    self.thread.start()
    print(self.thread.s)


  def handl(self,result):
    print(type(result))
    print(result)
    print("Cc!!")

The result is: [600, 601, 602, 603, 604, 605, 606, 607, 608, 609] Only the list 1 is showing. Any help please?

You might think that using the parentheses will allow you to have a signal with a singular argument signature, but that's not what's happening. Signals can be declared with multiple overloads (as in "different argument signatures"), each one contained in a sequence like a list or a tuple.

So, what you're doing is exactly the same as:

authResult = QtCore.pyqtSignal([list, list])

which will result in being the same as:

authResult = QtCore.pyqtSignal(list, list)

since there's only one signature.

Then, you're receiving the first list only because the function is only accepting one argument. If you add the second argument, the second list is received too:

    def handl(self, result, result2):
        print(type(result))
        print(result, result2)
        print("Cc!!")

# which will print:
<class 'list'>
[600, 601, 602, 603, 604, 605, 606, 607, 608, 609] ['a', 'b', 'c']
Cc!!

A slightly different solution could be to use a single object argument instead:

authResult = QtCore.pyqtSignal(object)

With this, the handl function will work with the previous, single argument based version.

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