Thanks for tinysoft now my code is working well.

Qt Code:
  1. void Dialog::RetrieveFromDatabase()
  2. {
  3. QString imagePath = QFileDialog::getSaveFileName(this,"Save image to",QDir::currentPath(),"JPEG Image (*.jpg)");
  4. QModelIndex index;
  5. QByteArray bytes;
  6.  
  7. QSqlQuery query;
  8. query.prepare("SELECT image FROM `test`.`images` WHERE `id_no`=:id;");
  9. query.bindValue(":id",ui->lineID->text());
  10. if (!query.exec())
  11. {
  12. QMessageBox::critical(this,"Query error",query.lastError().text());
  13. }
  14. else
  15. {
  16. //Set model
  17. model.setQuery(query);
  18. index = model.index(0,0);
  19. bytes = index.data().toByteArray();
  20.  
  21. //Save image
  22. QImage imageWrite;
  23. imageWrite.loadFromData(bytes);
  24. imageWrite.save(imagePath,"JPG");
  25.  
  26. }
  27. }
To copy to clipboard, switch view to plain text mode