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. Appending Items to QListView
Forum Updated to NodeBB v4.3 + New Features

Appending Items to QListView

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 18.1k 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.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    I am currently trying to append items to a list view. I found a code snippet online that appends a single Item to a list view by using
    @
    QStandardItem* Items = new QStandardItem(myString);
    QStandardItemModel* ListModel = new QStandardItemModel();
    ListModel->appendRow(Items);

    ui->lvconnect->setModel(ListModel);
    

    @

    The thing is, every time through, myString is going to change, and I just want to add the new Item to the list without removing any items from the list view. I figured appendRow would do this, but it seems it wont. I have it connected to a button, so everytime I push the button, myString should be added to the list view. There isn't much documentation on how to add items to the list view and this is the only helpful code snippet I could find. I have read the listView documentation as well.

    Any help would be great.

    EDIT: It seems I am over writing the model every time. That's why I am having issue with displaying a different item every time. I still havent found a solution yet though...I am trying to make things global currently.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      QListView is only a representation for your data stored in model. So question itself is a bit wrong. You shouldn't think that you add items to view, you add them to model and view only shows items that are stored in model.

      And of course you don't need to create model each time you add item. All you need is to get model() from view (or store it somewhere else, it depends on your arch) and add row to it.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vezprog
        wrote on last edited by
        #3

        is there a way to check current items already displayed in the listview? I would like to check for duplicate items already in the listbox.

        p.s. I fixed the issue previously my making the model global. Therefore, not overwriting the current model.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DenisKormalev
          wrote on last edited by
          #4

          The best way to do it is to subclass QAbstractItemModel and do checks on items adding.

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

            or use "QStandardItemModel::findItems() ":http://doc.qt.nokia.com/4.7/qstandarditemmodel.html#findItems method to check if it's already in the model.

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

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vezprog
              wrote on last edited by
              #6

              Thank you very much for the input, I am going check it out now!

              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