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 is not being updated or updated incorrectly
QtWS25 Last Chance

QListWidget is not being updated or updated incorrectly

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.3k Views
  • 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
    WhatIf
    wrote on last edited by WhatIf
    #1

    Hi,

    To help you visualize the problem, here is a link:

    http://postimg.org/image/my2cjjm79/

    What I'm trying to accomplish is allow the user to enter spouse first, middle and last name (dead or alive). Once an add button that you can't see in the image is clicked a check box is created and added to the QListWidget to the right. If user notice that the name was typed incorrectly, the user has the ability to select the incorrect name and click delete to remove from the list.

    I'm having 2 problems so far.

    First, the image has three snapshots, I labeled them (1,2,3), in the (1), I added 3 names, a a a, b b b, and ccc. When I selected a a a and clicked the delete button, a a a was deleted but as you can see in (2) it's still using it's space. b b b didn't go up in place to take the place used to be occupied by a a a and c c c didn't take the place of b b b.

    Second, if I select 2 names to delete only one is deleted. For example, in snapshot (2), I selected b b b and c c c to delete. When I clicked the delete button only b b b was deleted and as you can see in (3) c c c is still selected but not deleted. Had to click the delete button again to have c c c deleted.

    In mainwindow.h I declared two QLIst

    private:
        Ui::MainWindow *ui;
    
        QList<QListWidgetItem *> spouseListWidgetItem;
        QList<QCheckBox *> spouseListCheckBox;
    

    The code for the add button from mainwindow.cpp

    void MainWindow::on_addSpouseBtn_clicked()
    {
        QString spouseFirstName = ui->spouseFirstNameLineEdt->text();
        QString spouseMiddleName = ui->spouseMiddleNameLineEdt->text();
        QString spouseLastName = ui->spouseLastNameLineEdt->text();
    
        QString fullName = spouseFirstName;
        fullName.append(" ");
        fullName.append(spouseMiddleName);
        fullName.append(" ");
        fullName.append(spouseLastName);
    
        spouseListWidgetItem.append(new QListWidgetItem);
        spouseListCheckBox.append(new QCheckBox(fullName));
        ui->spouseListWidget->addItem(spouseListWidgetItem.last());
        ui->spouseListWidget->setItemWidget(spouseListWidgetItem.last(), spouseListCheckBox.last());
    }
    

    The code for the delete button from the mainwindow.cpp

    void MainWindow::on_deleteSpouseBtn_clicked()
    {
        for(int i = 0; i < spouseListCheckBox.size(); ++i)
        {
            if(spouseListCheckBox.at(i)->isChecked())
            {
                spouseListCheckBox.removeAt(i);
                ui->spouseListWidget->removeItemWidget(spouseListWidgetItem.at(i));
                spouseListWidgetItem.removeAt(i);
            }
        }
    }
    

    I'm still a newbie so if you would point any improvements to the code or see anything that can cause rare errors please point them out.

    Thanks in advance :)

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

      Hi,

      You are deleting the content of the row but you do not remove the row themselves.

      From a quick look at your code, you don't seem to have taken the easiest way. Since you have a QListWidget why not make use of the Qt::ItemIsUserCheckable flag on the item rather than adding a checkbox by hand ? The QList of QListWidgetItem also looks redundant since they are already contained in the model of the QListWidget.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        WhatIf
        wrote on last edited by WhatIf
        #3

        I did some changes but the program stops working after I delete the first item that was added.

        The new code inside the add button

        void MainWindow::on_addSpouseBtn_clicked()
        {
            QString spouseFirstName = ui->spouseFirstNameLineEdt->text();
            QString spouseMiddleName = ui->spouseMiddleNameLineEdt->text();
            QString spouseLastName = ui->spouseLastNameLineEdt->text();
        
            QString fullName = spouseFirstName;
            fullName.append(" ");
            fullName.append(spouseMiddleName);
            fullName.append(" ");
            fullName.append(spouseLastName);
        
            spouseListWidgetItem.append(new QListWidgetItem(fullName, ui->spouseListWidget));
            spouseListWidgetItem.last()->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable );
            spouseListWidgetItem.last()->setCheckState(Qt::Unchecked);
            ui->spouseListWidget->addItem(spouseListWidgetItem.last());
        }
        

        The code inside the delete button

        void MainWindow::on_deleteSpouseBtn_clicked()
        {
            int rows = ui->spouseListWidget->count();
            for(int i = 0; i < rows ; ++i)
            {
                if(spouseListWidgetItem.at(i)->checkState() == Qt::Checked)
                {
                    delete ui->spouseListWidget->takeItem(i);
                    spouseListWidgetItem.removeAt(i);
                }
            }
        }
        

        Also, your reply confused me "The QList of QListWidgetItem also looks redundant since they are already contained in the model of the QListWidget." But according to the documentation "A QListWidgetItem represents a single item in a QListWidget." Wouldn't I need to contain each single item in a QList?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Because you're deleting it twice:

          • Once when you call delete after takeItem.
          • And again when calling removeAt

          No you don't. When you call addItem, you put the QListWidgetItem in the model of the QListWidget. If you need it again, you can retrieve it from QListWidget.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          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