[newbie] how to iterate over QListView entries?



  • 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?


  • Qt Champions 2016

    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.



  • 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);
    

  • Qt Champions 2016

    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 ?



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



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


  • Qt Champions 2016

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.