Issue
I'm trying to send a string from one class to a qtextbrowser located in another class, my gui is built in pyqt and the code i wrote it in python 2.7.
Here is my test code:
class print_text():
def __init__(self):
text1 = "Acesta este un text de proba"
self.classMyWindow = MyWindow()
self.classMyWindow.statusText_updater("Merge ok ")
class systemValues(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def cpuRunValue(self):
text1 = "Acesta este un text de proba"
self.classMyWindow = MyWindow()
self.classMyWindow.statusText_updater("Merge ok ")
def run(self):
self.cpuRunValue()
class MyWindow(QtGui.QMainWindow):
def __init__(self):
#QtGui.QDialog.__init__(self)
super(MyWindow, self).__init__()
file_path = os.path.abspath("im-manager.ui")
uic.loadUi(file_path, self)
self.myThread = systemValues()
self.myThread.start()
def statusText_updater(self,text):
time_print = time.strftime("%d/%m/%Y-%H:%M:%S")
read1 = self.status.toPlainText()
self.status.setText(text+" >>> "+time_print+" \n"+ read1+" ")
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = MyWindow()
window.show()
# app.exec_()
sys.exit(app.exec_())
I get this error:
QPixmap: It is not safe to use pixmaps outside the GUI thread
What is the correct way to read strings or send strings to a qtextbrowser from another class?
I need this because my app needs to read some cpu and ram values on a different thread to keep my app from freezing and display a text message when the job is done.
Solution
To show data from a QThread in a window it is not necessary to create an instance, since the one you are creating will be different from the original window.
To display data from the thread to the GUI you must do it through signals and connect it to a slot that updates the data each time the signal is emitted.
In this case we will create the newMessage signal and emit it:
newMessage = QtCore.pyqtSignal(str)
#emit signal
self.newMessage.emit("Merge ok ")
Then we connect it to the statusText_updater slot
self.myThread.newMessage.connect(self.statusText_updater)
Complete code:
class systemValues(QtCore.QThread):
newMessage = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
QtCore.QThread.__init__(self, parent)
def __del__(self):
self.wait()
def cpuRunValue(self):
text1 = "Acesta este un text de proba"
self.newMessage.emit("Merge ok ")
def run(self):
self.cpuRunValue()
class MyWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent=parent)
file_path = os.path.abspath("im-manager.ui")
uic.loadUi(file_path, self)
self.myThread = systemValues()
self.myThread.newMessage.connect(self.statusText_updater)
self.myThread.start()
def statusText_updater(self,text):
time = datetime.datetime.now()
time_print = time.strftime("%d/%m/%Y-%H:%M:%S")
read1 = self.status.toPlainText()
self.status.setText(text+" >>> "+time_print+" \n"+ read1+" ")
As this is now your QThread
class will only display once the message, if you want the message to be shown from time to time you must modify the run method:
def run(self):
while True:
self.cpuRunValue()
time.sleep(1)
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.