Important: Please read the Qt Code of Conduct -

QListWidget Help

  • I need a little help on QListWidget, i want to eliminate redundancy in QListWidget by comparing incoming data string from "udpSocket Broadcast" with QListWidget items. Here is code
    QByteArray datagram;
    udpSocket->readDatagram(, datagram.size());
    QString msg= QString(;
    for(int i=0;i<onlineClientWidget->count();i++) //onlineClientWidget is QListWidget
    if( msg == onlineClientWidget->item(i)->text())
    This code is not working, it is not displaying any msg(message). Please help me how to resolve this problem and is this good logic to compare incoming string with QListWidget Items?

  • I would advise you not to treat GUI items like QListWidget as data stores for business-related data. That is just not a good design. Your networking code should not as such rely on the GUI at all. Store your data on the online clients elsewhere, and make your GUI just display that data if and when needed.

  • First: make your code readable. The indenting is a complete mess. Blocks of code at the same level (if, loops) should be aligned at the same level.

    Your code to check whether an item is in the list, is broken. If you have 10 items in the list, you add the incoming string 9 times.

    I would suggest to use QListWidget's findItems() method to check for the existence of an item.

    The construction of your msg QString is suboptimal. You construct a default empty string and assign a newly created string afterwards. This is better:

    QString msg(datagram);

    Note, that QString has a constructor taking a QByteArray, there's no need to use the const char data pointer of the array.

    Did you check that msg contains a proper string?

Log in to reply