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. [newbie] how to iterate over QListView entries?
Forum Updated to NodeBB v4.3 + New Features

[newbie] how to iterate over QListView entries?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 5.7k Views 2 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.
  • cerrC Offline
    cerrC Offline
    cerr
    wrote on last edited by cerr
    #1

    Hi,

    I load entries from a text file into a QListView for display reasons and now i need to work with the entries in the list. I'm not sure on how to iterate over QListView items but I think I know how to iterate over QListWidge items:

    int Widget::DataIterate(QListWidget *pLWidget)
    {
        int count = pLWidget->count();
        for(int index = 0;
            index < count;
            index++)
        {
            QListWidgetItem * item = listWidget->item(index);
        }
        return OK;
    }
    

    Now how do I feed the data from my QListView to this function, i.e. how can I convert QListView to QListWidget?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      QListView uses a model for the data.
      a QListWidget uses items.

      What model did you use for the QListView ?

      You should iterate over the data in the model.

      1 Reply Last reply
      2
      • cerrC Offline
        cerrC Offline
        cerr
        wrote on last edited by
        #3

        Ah okay, Thanks!

        I'm using a QStringList like:

        // Populate list view with hosts from file
            QStringList* stringList = new QStringList();
            rv = ReadHosts(HostFile, stringList);
            if (rv != OK) {
                QMessageBox msgBox;
                msgBox.setText("Error in ReadHosts!");
                msgBox.exec();
            }
        
            QStringListModel* listModel = new QStringListModel(*stringList, NULL);
            ui->HostList->setModel(listModel);
        
        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Ok
          but why not just loop over the stringlist and alter what you want?
          Then set it back to show the altered data?

          Maybe i dont understand what you really want to do with the data :)

          Do you want to alter all of the strings or is it more like you will click on some in the list and alter them ?

          cerrC 1 Reply Last reply
          0
          • cerrC Offline
            cerrC Offline
            cerr
            wrote on last edited by cerr
            #5

            The idea is to iterate over the list and read the string, perform some action and then alter the color of the entry accordingly.

            I've come up with something here:

            int JFTTools::DataIterate(QAbstractItemModel *model)
            {
                int count = model->rowCount();
            
                for(int row = 0;
                    row < count;
                    row++)
                {
                    model->setData(row, QBrush(Qt::red), Qt::ForegroundRole);
                }
                return OK;
            }
            

            but this returns:

            /home/ron/src/JPAXTroubleFinder/jfttools.cpp:41: error: no matching function for call to 'QAbstractItemModel::setData(int&, QBrush, Qt::ItemDataRole)'
                     model->setData(row, QBrush(Qt::red), Qt::ForegroundRole);
                                                                            ^
            

            Of course the coloring would depend on the result of above mentioned action (the entry should either turn red or green).

            1 Reply Last reply
            0
            • mrjjM mrjj

              Ok
              but why not just loop over the stringlist and alter what you want?
              Then set it back to show the altered data?

              Maybe i dont understand what you really want to do with the data :)

              Do you want to alter all of the strings or is it more like you will click on some in the list and alter them ?

              cerrC Offline
              cerrC Offline
              cerr
              wrote on last edited by
              #6

              @mrjj said in [newbie] how to iterate over QListView entries?:

              Ok
              but why not just loop over the stringlist and alter what you want?
              Then set it back to show the altered data?

              Maybe i dont understand what you really want to do with the data :)

              I read data (IPs) from a text file, line-by-line & display them in a QListView to the user.
              I then want to connect to each IP (i.e. I'll need to iterate through all the IPs that are now ibeing displayed n the QListView), execute some function(s), analyze the return value(s) and change the text color to either red or green in the QListView that serves to display each host's IP to display success or failure.

              In a second iteration, after the tests have run, one could hover over the host IPs and a pop up would display foirmatted results of the above tests.
              Doable?

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                you give it an int. its not a row, its a QModelIndex

                QModelIndex vIndex = model()->index(row,0);
                model->setData(vIndex, QBrush(Qt::red), Qt::ForegroundRole);

                Yes sounds doable, if you use tooltip for the hover info.

                cerrC 1 Reply Last reply
                0
                • mrjjM mrjj

                  Hi
                  you give it an int. its not a row, its a QModelIndex

                  QModelIndex vIndex = model()->index(row,0);
                  model->setData(vIndex, QBrush(Qt::red), Qt::ForegroundRole);

                  Yes sounds doable, if you use tooltip for the hover info.

                  cerrC Offline
                  cerrC Offline
                  cerr
                  wrote on last edited by
                  #8

                  @mrjj said in [newbie] how to iterate over QListView entries?:

                  you give it an int. its not a row, its a QModelIndex

                  Oh yes, makes sense! Thanks!

                  QModelIndex vIndex = model()->index(row,0);
                  model->setData(vIndex, QBrush(Qt::red), Qt::ForegroundRole);

                  but that still doesn't work for me, the color of the entries is not changing. I now have this:

                  //-------------------------------------------------------------------------------------------------
                  
                  Widget::Widget(QWidget *parent) :
                      QWidget(parent),
                      ui(new Ui::Widget)
                  {
                      int rv = OK;
                      ui->setupUi(this);
                  
                      HostFile = "/home/ron/src/JPAXTroubleFinder/HostList";
                  
                      Tools = new JTFTools();
                  
                      // Populate list view with hosts from file
                      QStringList* stringList = new QStringList();
                      rv = Tools->ReadHosts(HostFile, stringList);
                      if (rv != OK) {
                          QMessageBox msgBox;
                          msgBox.setText("Error in ReadHosts!");
                          msgBox.exec();
                          return;
                      }
                      rv = Tools->ListViewPopulate(ui->HostList, stringList);
                      if (rv != OK) {
                          QMessageBox msgBox;
                          msgBox.setText("Error in ListViewPopulate!");
                          msgBox.exec();
                          return;
                      }
                  }
                  //-------------------------------------------------------------------------------------------------
                  
                  int JTFTools::ListViewPopulate(QListView *listView, QStringList *stringList)
                  {
                      int rv = OK;
                  
                      QStringListModel* listModel = new QStringListModel(*stringList, NULL);
                      QItemSelectionModel *oldModel = listView->selectionModel();
                      listView->setModel(listModel);
                  
                      delete oldModel;
                  
                      return rv;
                  }
                  //-------------------------------------------------------------------------------------------------
                  void Widget::on_BtnGo_clicked()
                  {
                      int rv = OK;
                      QAbstractItemModel *listModel = ui->HostList->model();
                      rv = Tools->DataIterate(listModel);
                  }
                  //-------------------------------------------------------------------------------------------------
                  
                  int JTFTools::DataIterate(QAbstractItemModel *model)
                  {
                      int count = model->rowCount();
                  
                      for(int row = 0;
                          row < count;
                          row++)
                      {
                          QModelIndex vIndex = model->index(row,0);
                          model->setData(vIndex, QBrush(Qt::red), Qt::ForegroundRole);
                      }
                      return OK;
                  }
                  

                  I think this should give a somewhat complete picture of what I have...

                  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