0

When I run the program, the code get to the clear method and my code crash, I do think it maybe a problem with my threading, here is some parts of my code that can cause the issue + the issue.

class client(QtWidgets.QMainWindow):
    def __init__(self):
        super(client, self).__init__()
        self.main_ui = Ui_MainWindow()
        self.main_ui.sendbutton.clicked.connect(self.handle_main)
        self.main_ui.redirectmain.clicked.connect(self.move_to_main)
        self.main_ui.redirectsports.clicked.connect(self.move_to_sports)
        self.main_ui.redirectgaming.clicked.connect(self.move_to_gaming)
        self.main_ui.redirectmusic.clicked.connect(self.move_to_music)

    def go_to_main(self): #called after login or signup 
        self.login_ui.close()
        self.signup_ui.close()
        self.forgotpass_ui.close()
        self.main_ui.show()
        self.message_thread = threading.Thread(target=self.handle_main_actions)
        self.message_thread.start()
        self.move_to_main()

    def handle_main_actions(self):
        display_chat = self.main_ui.chat_to_display
        display_users = self.main_ui.display_users_online
        data = socket.recv(1024).decode()
        print(type(data))
        print(f"Received data: {data}")
        print('3')
        json_data = json.loads(data)
        print(type(json_data))
        if 'list' in json_data:
            display_users.clear()
            list = json_data['list']
            for user in list:
                display_users.append(user)
        elif 'displaytoclient' in json_data:
            message = json_data['message']
            time_sent = json_data['time']
            username = json_data['Username']
            room_in = json_data['room']
            print(room_in)
            print(self.room_user_in)
            if self.room_user_in == room_in:
                display_chat.append("[" + username + "] [" + time_sent + "]" + ": \n" + message)
        elif 'messages' in json_data:
            messages_to_display_in_chat = json_data['messages']
            print(messages_to_display_in_chat)
            print(type(messages_to_display_in_chat))
            print('22')
            **self.main_ui.chat_to_display.clear()**
            print(type(self.main_ui.chat_to_display))
            print('33')
            for item in messages_to_display_in_chat:
                print('1')
                value = item
                username = value['username']
                print(username)
                mes = value['message']
                print(mes)
                time = value['time']
                print(time)
                self.main_ui.chat_to_display.append("[" + username + "] [" + time + "]" + ": \n" + mes)
            self.request_online_users_list()

class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.chat_to_display = self.findChild(QtWidgets.QTextEdit, 'displaychat') 


when I print(type(self.main_ui.chat_to_display)) I get <class 'PyQt5.QtWidgets.QTextEdit'> after the print('22') my code crush and not continue to the next prints.

code error: Process finished with exit code -1073741819 (0xC0000005)

Idan
  • 1
  • Qt does not support GUI operations of any kind outside the main thread. Please read a [basic tutorial on how to use threading in a PyQt GUI](https://realpython.com/python-pyqt-qthread/). – ekhumoro Apr 25 '23 at 10:36

0 Answers0