Hi all, could really do with some help and advice.

I have written a small app that will search a database by a users name

for example: select ID, FNAME, LNAME, AGE from PERSON where FNAME = $1

the app runs the above query and returns it into a QList which is then displayed in a QlistWidget

The Qlistwidget displays only a list of "FNAME" (users firstnames, which is exactly what i want at the moment)


Now what i need help to achieve:

I would like to click a users name in the listwidget list and display the entire users record details i.e. ID, FNAME, LNAME and AGE information.

I understand how to search for data, but i dont know how i would be able to get the users ID so that i may perform another select query against it to get the users entire record details.

Any help no matter how small would be really appriciated.

Thanks in advance.


dal.h

Qt Code:
  1. #ifndef DAL_H
  2. #define DAL_H
  3.  
  4. #include <QObject>
  5. #include <QtSql/QSqlDatabase>
  6. #include <QtSql/QSqlError>
  7. #include <QtSql/QSqlQuery>
  8. #include <QString>
  9. #include <QVariant>
  10.  
  11.  
  12. class PersonData
  13. {
  14. public:
  15. PersonData();
  16. ~PersonData();
  17. int ID;
  18. QString FIRSTNAME;
  19. QString LASTNAME;
  20. int AGE;
  21. };
  22.  
  23.  
  24. class Dal : public QObject
  25. {
  26. public:
  27. Dal(QObject *parent = 0);
  28. ~Dal();
  29.  
  30. public:
  31. bool openDB();
  32. QList<PersonData*> getPerson();
  33.  
  34.  
  35. private:
  36. };
  37.  
  38.  
  39. #endif // DAL_H
To copy to clipboard, switch view to plain text mode 


dal.cpp

Qt Code:
  1. #include "dal.h"
  2.  
  3. Dal::Dal(QObject *parent):
  4. QObject (parent)
  5. {
  6. }
  7.  
  8. Dal::~Dal()
  9. {
  10. if (db.isOpen())
  11. db.close();
  12. }
  13.  
  14. PersonData::PersonData()
  15. {
  16. ID = 0;
  17. }
  18.  
  19. PersonData::~PersonData()
  20. {
  21. }
  22.  
  23.  
  24. bool Dal::openDB()
  25. {
  26.  
  27. // Find QSLite driver
  28. db = QSqlDatabase::addDatabase("QSQLITE");
  29.  
  30. #ifdef Q_OS_LINUX
  31. // NOTE: We have to store database file into user home folder in Linux
  32. QString path(QDir::home().path());
  33. path.append(QDir::separator()).append("mydb.sqlite");
  34. path = QDir::toNativeSeparators(path);
  35. db.setDatabaseName(path);
  36. #else
  37. // NOTE: File exists in the application private folder, in Symbian Qt implementation
  38. db.setDatabaseName("mydb.sqlite");
  39. #endif
  40.  
  41. // Open databasee
  42. return db.open();
  43.  
  44. }
  45.  
  46.  
  47. QList<PersonData*> Dal::getPerson()
  48. {
  49. QList<PersonData*> result;
  50.  
  51. QSqlQuery query("select ID, FIRSTNAME, LASTNAME, AGE from PERSON");
  52. while (query.next()) {
  53. PersonData* item = new PersonData();
  54. item->ID = query.value(0).toInt();
  55. item->FIRSTNAME = query.value(1).toString();
  56. item->LASTNAME = query.value(2).toString();
  57. item->AGE = query.value(3).toInt();
  58. result.append(item);
  59. }
  60.  
  61. return result;
  62. }
To copy to clipboard, switch view to plain text mode 



mainwindow.cpp

Qt Code:
  1. void MainWindow::on_pushButton1_clicked()
  2. {
  3.  
  4. ui->listWidget->clear();
  5. persondata.clear();
  6. persondata = dbManager->getPerson();
  7.  
  8. PersonData* persondata2;
  9.  
  10. foreach(persondata2,persondata)
  11. {
  12. ui->listWidget->addItem(persondata2->FIRSTNAME);
  13. }
  14.  
  15. }
To copy to clipboard, switch view to plain text mode