I am trying to make an application and when the user clicks on the homepage it runs a thread with a While
loop that updates certain things.
(MainWindow(QMainWindow))
def home_page_thread(self): # Home Page Thread
while True:
self.ui.label_home_title.setStyleSheet(f"color: rgb({random.randint(0, 255)}, {random.randint(0, 255)}, {random.randint(0, 255)});")
time_now = datetime.now()
time_now = time_now.strftime("%m:%m:%y %H:%M:%S")
self.ui.label_home_time.setText(time_now) # (QLabel)
self.ui.text_info.setPlainText("Hello") # <----- THIS IS THE ERROR (QTextEdit)
if KILL_THREADS:
return
elif self.viewing != "HOME":
return
time.sleep(1)
The thread would be started from a child class of class MainWindow(QMainWindow)
(MainWindow imports the application from ui_main.py which is made in QtDesigner.
Relevant Code leading up to the Thread...
(MainWindow(QMainWindow))
MainUIFunctions.ui_definitions(self) # This calls the UI Functions class which would start the thread.
If home button is clicked...
(MainUIFunctions(MainWindow))
self.ui.button_home.clicked.connect(lambda: MainUIFunctions.change_page(self, "HOME")) # This calls a method within MainUIFunction which will be below that would switch to the Home Page and start the thread...
And the part that starts the thread...
(MainUIFunctions(MainWindow))
def change_page(self, page): # Changes Viewing Page
if page == "HOME":
self.ui.widget_content.setCurrentWidget(self.ui.home_page)
self.ui.button_home.setStyleSheet("""\
QPushButton {
border-radius: 14px;
border: 2px solid rgb(84, 88, 98);
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(122, 138, 166, 255), stop:1 rgba(49, 59, 72, 255));
}
"""
)
self.home_thread = threading.Thread(target=self.home_page_thread)
self.home_thread.start()
I am getting an error when the code is ran and have located the error to this:
self.ui.text_info.setPlainText("Hello")
(QTextEdit)
ERROR:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x21176a622b0), parent's thread is QThread(0x211760c17b0), current thread is QThread(0x21176b9f520)
Does anyone know how to fix this?
I am using import threading