Congratulations to our 2022 Qt Champions!

Issue with Qt Table widget

  • 0

    I have a table that shows me the articles. The problem is that when I go to insert a new article and go back to the home for the first 2 items it doesn't give me problems, from the third time that I inserts article, the table show the second product again without the while loop inserting it.

    The for loop performs a hard erase every time I go back to the home, as seen also from the console.

    Where am I wrong? can anyone help me?

    void magazzino::populateTable(){
        std::cout<<"number of row"<<view->rowCount()<<std::endl;
        int righe = view->rowCount();
        for(int k=0; k<righe;k++){
            std::cout<<"for time"<<k<<std::endl;
        Cont::iteratore* iter = new Cont::iteratore(contenitore->getFirst());
        int i = 0;
            std::cout<<"while time"<<i<<std::endl;
            QString cod = QString::number(iter->punt->getInfo().getCodice());
            QTableWidgetItem* codice = new QTableWidgetItem(cod);
            QString mar = QString::fromStdString(iter->punt->getInfo().getMarca());
            QTableWidgetItem* marca = new QTableWidgetItem(mar);
            QString mod = QString::fromStdString(iter->punt->getInfo().getModello());
            QTableWidgetItem* modello = new QTableWidgetItem(mod);
            QString pr = QString::number(iter->punt->getInfo().getPrezzo());
            pr.append(" €");
            QTableWidgetItem* prezzo = new QTableWidgetItem(pr);
            //QString at = QString::number(iter->punt->getInfo().getAltezza());
            //QTableWidgetItem* altezzaTacco = new QTableWidgetItem(at);
            view->setItem(i, 0, codice);
            view->setItem(i, 1, marca);
            view->setItem(i, 2, modello);
            view->setItem(i, 3, prezzo);
            iter->punt = iter->punt->next;

    alt text
    alt text

  • @http_edo13
    Please post the actual text of code, not screenshots, so that people can copy & paste.

    int righe = view->rowCount();
    for (int k = 0; k < righe; k++)

    Each time through the loop, print out k and print out view->rowCount() again. Do you see what is happening? Your code is not right, rowCount() is being decremented (and hence changing) as each row is removed.

    If you want to remove all rows, you either want to repeatedly remove row 0, or count downward from view->rowCount() - 1, or why not do it without a loop via setRowCount(0)?

  • With setRowCount(0) I solved the problem. Thank you very much!! @JonB

Log in to reply