Problem with QListView (PyQt5)
-
hello everyone Currently I am doing a simple chat socket program and I use QlistView to display the conversation but during the running process, I have the following problem:
During the run, I have created a sub-thread inside to receive messages from the server as well as add that message to Qlistview, but when the message is added, the interface side does not update anything.
And only when the client imports the message and adds the message to Qlistview (this process is done in the main thread wrapped outside the sub-thread above), the message from the server will be added and displayed at the interface.
Is there any way I can add an item directly from the sub-thread and the interface is visible immediately? -
@JIN81 Qt is asynchronous, so usually there is no need for any threads when doing network programming. Why do you think you need a thread?
Also, it is not supported to access UI classes outside from the GUI/main thread. If you want to use an additional thread then communicate with main thread via signals/slots instead of direct access to the GUI. -
@JIN81 said in Problem with QListView (PyQt5):
I need a thread to receive messages from the server and I'm a newbie
Why do you think you need a thread for that?
-
@jsulm I use a thread that receives messages from the server and the other thread for sending messages from the client to the server. As the result, the program can receive and send messages at the same time. That reason why I think I need a thread
-
@JIN81 All this can be done without any threads! Qt is asynchronous, means: non blocking. There is really NO need for threads in your case, you're over-engineering things for no reason.
Take a look at Qt networking examples. -
@jsulm said in Problem with QListView (PyQt5):
Qt is asynchronous, means: non blocking.
Could you elaborate , give an example , how "asynchronous" meas "non blocking"?
I believe the OP is asking how to implement two way , full duplex (send / receive same time ) communication.
Not how to run simplex -alternate between send then receive operations.
-
@AnneRanch said in Problem with QListView (PyQt5):
Could you elaborate , give an example , how "asynchronous" meas "non blocking"?
It doesn't. However Qt prefers both asynchronous and non-blocking and is what it usually implements.
I believe the OP is asking how to implement two way , full duplex (send / receive same time ) communication.
Even if it were possible, it isn't enforceable. I'd be the same as requiring two threads to be executing on different cores - it is often the case, but certainly not a guarantee. There's layers upon layers, upon layers ... upon layers of software between you and the steel preventing you from achieving the god of thunder title.
Not how to run simplex -alternate between send then receive operations.
It's all buffered anyway. It's buffered in Qt, it's (usually) buffered in the kernel and/or the driver, and it may be buffered in the actual ethernet controller. So I really don't see how that should work ...