ok now i'v been searching alot until i have finally understood how QThread works with signals to change the GUI from the main thread and actually it is pretty easy and simple.....
first we know that QT have modules we need to use QtCore that have Qt objects like thread and signals and QtGui that have GUI objects
now we have this code :
u can pass argument like char,int,bool
i dont know how to pass strings so you can use my way :)
if you have any question am glad to answer :)
to ok so to send signals we use emit function#! /usr/bin/python# -*- coding : utf-8 -*-from PyQt4 import QtGui, QtCoreimport time, sysgui=QtGui.QApplication.processEvents #a function we use to make sure that the gui wont freezeclass display():#first we make a class that handle the gui objecttexttoset=""app=QtGui.QApplication(sys.argv) #we make a QT Application objectdialog=QtGui.QDialog() #we make a dialog objectdialog.resize(200, 150) #set sizedialog.setMaximumSize(200, 150) #set maximum sizedialog.setMinimumSize(200, 150) #set minimum sizedialog.setWindowTitle("new dialog") #set titlelabel=QtGui.QLabel(dialog) #make a label object child of dialoglabel.setText("am a text label") #set label textlabel.setGeometry(1, 20,200, 50)#set geometry x,y,width,highbutton=QtGui.QPushButton(dialog)#now we make a button objectbutton.setText("click me !")#set its textbutton.setGeometry(1, 60, 200, 50)#set geometrydef changetext(self, value):#we make a function that change the label textif value==True:text=str(self.texttoset)#to make sure it will be a stringself.label.setText(text)#set the textui=display()#we make an object from the display class#now it is time to make a QThread classclass mythread(QtCore.QThread):#inherite from QtCore.QThread#this thread will change the text 10 times from "i have changed by a QThread" to "back all over again for the x time" then when finished changes text to "QThread has finished"def __init__(self):QtCore.QThread.__init__(self)#we construct the classdef run(self):#built in function contain the code that the thread will excutex=0while x<=10:s=str(x)display.texttoset="i have changed by a QThread"#set the text wich the changetext function will displayself.emit(QtCore.SIGNAL("anysignalname(bool)"), True) #we send the signal called "anysignalname" and send a bool argumenttime.sleep(1)#sleep for 1 secgui()#refresh the guidisplay.texttoset="back all over again for "+s+" time"#set the text wich the changetext function will displayself.emit(QtCore.SIGNAL("anysignalname(bool)"), True) #we send the signal called "anysignalname" and send a bool argumenttime.sleep(1)#sleep for 1 secgui()#refresh the guix=x+1 #increase x valuedisplay.texttoset="QThread has finished!!"#set the text wich the changetext function will displayself.emit(QtCore.SIGNAL("anysignalname(bool)"), True) #we send the signal called "anysignalname" and send a bool argumenttime.sleep(1)#sleep for 1 secgui()#refresh the guithread1=mythread()#we make a thread objectdef start():thread1.start()def connecter():#a function that will connect signalsQtCore.QObject.connect(ui.button, QtCore.SIGNAL("clicked()"), start)#connect the button when clicked with the start functionQtCore.QObject.connect(thread1, QtCore.SIGNAL("anysignalname(bool)"), ui.changetext)#connect the thread1 object with the changetext function with the "anysignalname" signal that sends a bool valueui.dialog.show()#show the dialogconnecter()#connect signalsui.app.exec_()#start the app main loop
u can pass argument like char,int,bool
i dont know how to pass strings so you can use my way :)
if you have any question am glad to answer :)
0 comments:
Post a Comment
gotta some thing to say ?
Note: Only a member of this blog may post a comment.