title: How can fetch the row which has been recently added in the qlistwidget
body: <p>How to fetch the last inserted item.  </p>
  
  <p>An example  </p>
  
  <pre><code>QSqlQuery qry;
  qry.prepare("SELECT * FROM users");
  qry.exec();
  while(qry.next()){
     ui-&gt;listWidget-&gt;addItem(qry.value("username").toString());
  }
  </code></pre>
  
  <p>As you seen in the previous example specifically this line <code>ui-&gt;listWidget-&gt;addItem(qry.value("username").toString());</code></p>
  
  <p>This line to add every database row in as an item in qlistwidget.  </p>
  
  <p>I have tried to use the following but does not work .</p>
  
  <pre><code>QSqlQuery qry;
  qry.prepare("SELECT * FROM users");
  qry.exec();
  while(qry.next()){
     ui-&gt;listWidget-&gt;addItem(qry.value("username").toString());
     ui-&gt;listWidget-&gt;currentItem()-&gt;setData(Qt::UserRole, qry.value("id").toString());
  }
  </code></pre>
  
  <p>I want to get last inserted item, to apply the <code>setData()</code> method, on each item was added.</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>I have an error, when delete an item Deletes the item that before.</p>
  
  <p><img src="https://i.stack.imgur.com/qYVuB.jpg" alt=""></p>
  
  <p>What is the problem in the following code:</p>
  
  <pre><code>ui-&gt;listWidget-&gt;takeItem(ui-&gt;listWidget-&gt;row(ui-&gt;listWidget-&gt;currentItem()));
  QSqlQuery qry;
  qry.prepare("DELETE FROM users WHERE id=:id");
  qry.bindValue(":id",ui-&gt;listWidget-&gt;currentItem()-&gt;data(Qt::UserRole).toString());
  if(qry.exec()){
       qDebug()&lt;&lt; "Ok";
  }else{
       qDebug()&lt;&lt; "Errro";
  }
  </code></pre>