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?
Wouldn’t it be easier to read Count? https://doc.qt.io/qt-5/qlistwidget.html#Count-prop
– Bacco
Because nobody warned me that this existed? Thanks saw. If you want to make an answer so I can score as best.
– silash35
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.)
– Bacco