Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.
    2020-12-18_100039.png
    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.
    2020-12-18_110828.png
    Is there any way I can add an item directly from the sub-thread and the interface is visible immediately?


  • Lifetime Qt Champion

    @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.


  • Lifetime Qt Champion

    @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.



  • @jsulm Thanks for your reply. I need a thread to receive messages from the server and I'm a newbie, so I don't know that Qt is not supported to access UI from other thread :((


  • Lifetime Qt Champion

    @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


  • Lifetime Qt Champion

    @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 oh I got it. I wish I knew it earlier. Thank you !!!!



  • @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.


  • Qt Champions 2017

    @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 ...


Log in to reply