Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget Help
Forum Updated to NodeBB v4.3 + New Features

QListWidget Help

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wasim3s
    wrote on last edited by
    #1

    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
    @while(udpSocket->hasPendingDatagrams())
    {
    QByteArray datagram;
    datagram.resize(udpSocket->pendingDatagramSize());
    udpSocket->readDatagram(datagram.data(), datagram.size());
    QString msg= QString(datagram.data());
    for(int i=0;i<onlineClientWidget->count();i++) //onlineClientWidget is QListWidget
    {
    if( msg == onlineClientWidget->item(i)->text())
    {
    continue;
    }
    else
    onlineClientWidget->addItem(msg);
    }
    }@
    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?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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?

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved