Search code examples
c++qtqt5qlistwidget

How can fetch the row which has been recently added in the qlistwidget


How to fetch the last inserted item.

An example

QSqlQuery qry;
qry.prepare("SELECT * FROM users");
qry.exec();
while(qry.next()){
   ui->listWidget->addItem(qry.value("username").toString());
}

As you seen in the previous example specifically this line ui->listWidget->addItem(qry.value("username").toString());

This line to add every database row in as an item in qlistwidget.

I have tried to use the following but does not work .

QSqlQuery qry;
qry.prepare("SELECT * FROM users");
qry.exec();
while(qry.next()){
   ui->listWidget->addItem(qry.value("username").toString());
   ui->listWidget->currentItem()->setData(Qt::UserRole, qry.value("id").toString());
}

I want to get last inserted item, to apply the setData() method, on each item was added.


Edit

I have an error, when delete an item Deletes the item that before.

What is the problem in the following code:

ui->listWidget->takeItem(ui->listWidget->row(ui->listWidget->currentItem()));
QSqlQuery qry;
qry.prepare("DELETE FROM users WHERE id=:id");
qry.bindValue(":id",ui->listWidget->currentItem()->data(Qt::UserRole).toString());
if(qry.exec()){
     qDebug()<< "Ok";
}else{
     qDebug()<< "Errro";
}

Solution

  • You can do next (it is just example) Your code doesn't work because currentItem() return item selected by user or by programmer, but you don't provide selection, so this code doesn't work.

    for(int i = 0; i < 10; i++)
    {//this code works because
        ui->listWidget->addItem(QString::number(i));//here you add item to the end
        //here you use last item by count()-1
        ui->listWidget->item(ui->listWidget->count()-1)->setData(Qt::UserRole, "id"+QString::number(i));//set data to last item
    }
    for(int i = 0; i < 10; i++)
    {
        qDebug() << ui->listWidget->item(i)->data(Qt::UserRole).toString();
    }
    

    Output:

    "id0" 
    "id1" 
    "id2" 
    "id3" 
    "id4" 
    "id5" 
    "id6" 
    "id7" 
    "id8" 
    "id9" 
    

    This code works identical but it is really bad approach

    ui->listWidget->setCurrentItem(ui->listWidget->item(ui->listWidget->count()-1));
    ui->listWidget->currentItem()->setData(Qt::UserRole, "id"+QString::number(i));
    

    Edit:

    First of all takeItem remove item from widget but doesn't delete it from memory. So if you don't want get memory leak you should use delete, gor example

    delete ui->listWidget->takeItem(ui->listWidget->row(ui->listWidget->currentItem()));
    

    Back to your problem. Try firstly delete data in database and after that delete it from widget. Something like this

        QSqlQuery qry;
        qry.prepare("DELETE FROM users WHERE id=:id");
    //qDebug() << ui->listWidget->currentItem()->data(Qt::UserRole).toString();//now you can see id which you want to delete
        qry.bindValue(":id",ui->listWidget->currentItem()->data(Qt::UserRole).toString());//make sure that margin id in your app is relly margin id in your database
        if(qry.exec()){
             qDebug()<< "Ok";
        }else{
             qDebug()<< "Errro";
    }
    
    delete ui->listWidget->takeItem(ui->listWidget->row(ui->listWidget->currentItem()));