How to know the last element in a Qlistwidget?

Asked

Viewed 50 times

1

I have a Qlistwidget called list_toMerge with some items added, then I created 2 buttons to change the order of the items. The up button and the down button. First look at the code in the up button:

void MainWindow::on_btn_Mup_clicked()
{
    int currentRow = ui->list_toMerge->currentRow();
    qDebug() << currentRow;

    if(currentRow != 0){
        QString aux = ui->list_toMerge->item(currentRow - 1)->text();

        ui->list_toMerge->item(currentRow - 1)->setText(ui->list_toMerge->item(currentRow)->text());
        ui->list_toMerge->item(currentRow)->setText(aux);

        ui->list_toMerge->setCurrentRow(currentRow - 1);
    }

    ui->list_toMerge->update();
}

if serves to not execute the code when the selected item is already item 0, which is already at the top as it is already the first item. The problem is with the down button:

void MainWindow::on_btn_Mdown_clicked()
{
    int currentRow = ui->list_toMerge->currentRow();

    qDebug() << currentRow;

    if(currentRow != ????????){
        QString aux = ui->list_toMerge->item(currentRow + 1)->text();

        ui->list_toMerge->item(currentRow + 1)->setText(ui->list_toMerge->item(currentRow)->text());
        ui->list_toMerge->item(currentRow)->setText(aux);

        ui->list_toMerge->setCurrentRow(currentRow + 1);
    }

    ui->list_toMerge->update();
}

Where has "?????????" should be the position of the last item on the list (The last to be added), but I don’t know how to get this value. How do I get the position of the last item in a Qlistwidget?

1 answer

0


I solved the problem using the method count() which returns the number of elements in the Qlistwidget. So I keep the number of elements, and having the number of elements I have the Row of the last element. With that the corrected Code was like this:

void MainWindow::on_btn_Mup_clicked()
{
    int currentRow = ui->list_toMerge->currentRow();

    if(currentRow > 0){
        QString aux = ui->list_toMerge->item(currentRow - 1)->text();

        ui->list_toMerge->item(currentRow - 1)->setText(ui->list_toMerge->item(currentRow)->text());
        ui->list_toMerge->item(currentRow)->setText(aux);

        ui->list_toMerge->setCurrentRow(currentRow - 1);
    }

    ui->list_toMerge->update();
}

void MainWindow::on_btn_Mdown_clicked()
{
    int currentRow = ui->list_toMerge->currentRow();

    if( (currentRow>=0) and (ui->list_toMerge->count()>0) and (ui->list_toMerge->count()!=(currentRow+1)) ){
        QString aux = ui->list_toMerge->item(currentRow + 1)->text();

        ui->list_toMerge->item(currentRow + 1)->setText(ui->list_toMerge->item(currentRow)->text());
        ui->list_toMerge->item(currentRow)->setText(aux);

        ui->list_toMerge->setCurrentRow(currentRow + 1);
    }

    ui->list_toMerge->update();
}
  • 1

    Wouldn’t it be easier to read Count? https://doc.qt.io/qt-5/qlistwidget.html#Count-prop

  • Because nobody warned me that this existed? Thanks saw. If you want to make an answer so I can score as best.

  • 1

    You can update your answer even... Here’s the suggestion to read the manual extensively (and don’t forget to always look at who the classes inherit from. There’s a lot of stuff that’s in "mother class" and not in what you’re wearing.)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.