Skip to content
  • 0 Votes
    5 Posts
    890 Views
    JonBJ

    @Yash001
    When items are removed from a model the view will update to reflect the new state. Widgets you have added via setIndexWidget() which are no longer present will be deleted by Qt infrastructure without you needing to delete them or disconnect from signals.

  • 0 Votes
    9 Posts
    5k Views
    S

    @JonB
    I added comments in my code. I think I should explain it clearly. Sorry... but please help.

  • 1 Votes
    7 Posts
    31k Views
    VRoninV

    @kishore_hemmady said in QListWidget delete selected item:

    how can i achieve this by selecting the check Box rather than selecting the list content?

    for(int i=ui->listWidget->model()->rowCount()-1;i>=0;--i){ if(ui->listWidget->model()->index(i,0).data(Qt::CheckStateRole).toInt()==Qt::Checked) ui->listWidgetMy->model()->removeRow(i); }
  • 0 Votes
    4 Posts
    2k Views
    SGaistS

    That wasn't really clear from your original description.

    In that case, use a filter. QSortFilterProxyModel for example

  • 0 Votes
    3 Posts
    2k Views
    M

    @Olivier-Ronat Thanks for checking, Olivier. Sorry to detain you to do that when I found it was my own fault. However, I learned some things and got a little better at debugging in Qt Creator.

    I found the cause. I had a signal attached to the selectionModel of each row. Here's how I found out (note, I'm a newbie). On my Mac, I got the crash report and clicked a button to see what it was. This showed a callstack. I was able to see that it was complaining about setting text() from on_SelRow, and then I recalled that I had a signal connected on rows. So, I changed my code like so:

    ui->myTable->selectionModel()->disconnect(); ui->myTable->model()->removeRows(0,ui->myTable->rowCount());

    I was then able to remove rows without a crash. I just need to remember to add the signal back to the table when I add rows again.

    P.S.

    You may also be wondering why I was intercepting a selected row. The reason was because then I could show a box above the table that showed the item detail in an easier to read format than having to scroll horizontally in the table to see all the columns. I was doing it like so:

    connect(ui->myTable->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)), this, SLOT(on_SelRow(QModelIndex,QModelIndex)));

    ...and then I had a class method like so:

    void MainWindow::on_SelRow(QModelIndex oCurrRow, QModelIndex oPrevRow) { QTableWidget *t = dynamic_cast<QTableWidget *>(QObject::sender()->parent()); int y = oCurrRow.row(); // do something with t (table) and y (row index) variables }