Results 1 to 8 of 8

Thread: Help with getting record ID from a QList when clicking a record in listwidget

  1. #1
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Help with getting record ID from a QList when clicking a record in listwidget

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    Do you mean ?:
    Qt Code:
    1. int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
    2. QString strID = presondata.at(iRow)->ID;
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    Maybe there be similar "firstnames" in your database, search doesn't work here!
    You need to embed "ID"s to "item"s,

    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. QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
    13. item->setData(Qt::UserRole, QVariant(item->ID));
    14. ui->listWidget->addItem(item);
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Nov 2010
    Posts
    22
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    Quote Originally Posted by srazi View Post
    Maybe there be similar "firstnames" in your database, search doesn't work here!
    You need to embed "ID"s to "item"s,

    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. QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
    13. item->setData(Qt::UserRole, QVariant(item->ID));
    14. ui->listWidget->addItem(item);
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    Thank you very much for replying. I have tried adding the above code and i get an error class QListWidgeItem has no member named 'ID'

    Quote Originally Posted by srazi View Post
    Maybe there be similar "firstnames" in your database, search doesn't work here!
    You need to embed "ID"s to "item"s,

    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. QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
    13. item->setData(Qt::UserRole, QVariant(item->ID));
    14. ui->listWidget->addItem(item);
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    Thank you very much for replying. I have tried adding the above code and i get an error class QListWidgeItem has no member named 'ID'


    Added after 24 minutes:


    Quote Originally Posted by high_flyer View Post
    Do you mean ?:
    Qt Code:
    1. int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
    2. QString strID = presondata.at(iRow)->ID;
    To copy to clipboard, switch view to plain text mode 
    Hi high_flyer

    Thanks very much for replying. I have tried to implement what you recommended but the program errors out and exits the simulator (nokia). The program crashes out when i click on a result record in the listwidget.

    Qt Code:
    1. void MainWindow::on_listWidget_itemSelectionChanged()
    2. {
    3.  
    4. int iRow = ui->listWidget->row(ui->listWidget->selectedItems()[0]);
    5.  
    6. //HAD TO CHANGE strID to an INT AS QTCREATOR WAS COMPLAINING ABOUT IT BEING A QSTRING
    7. int strID = persondata.at(iRow)->ID;
    8.  
    9. ui->listWidget->clear();
    10. persondata.clear();
    11. persondata = dbManager->getPerson(strID);
    12.  
    13. PersonData* persondata3;
    14.  
    15. foreach(persondata3,persondata)
    16. {
    17.  
    18. ui->listWidget->addItem(gadata3->FIRSTNAME);
    19.  
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 


    Added after 13 minutes:


    Quote Originally Posted by srazi View Post
    Maybe there be similar "firstnames" in your database, search doesn't work here!
    You need to embed "ID"s to "item"s,

    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. QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
    13. item->setData(Qt::UserRole, QVariant(item->ID));
    14. ui->listWidget->addItem(item);
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 
    Thank you very much for replying. I have tried adding the above code and i get an error class QListWidgeItem has no member named 'ID'


    Added after 24 minutes:


    Quote Originally Posted by high_flyer View Post
    Do you mean ?:
    Qt Code:
    1. int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
    2. QString strID = presondata.at(iRow)->ID;
    To copy to clipboard, switch view to plain text mode 
    Hi high_flyer

    Thanks very much for replying. I have tried to implement what you recommended but the program errors out and exits the simulator (nokia). The program crashes out when i click on a result record in the listwidget.

    Qt Code:
    1. void MainWindow::on_listWidget_itemSelectionChanged()
    2. {
    3.  
    4. int iRow = ui->listWidget->row(ui->listWidget->selectedItems()[0]);
    5.  
    6. //HAD TO CHANGE strID to an INT AS QTCREATOR WAS COMPLAINING ABOUT IT BEING A QSTRING
    7. int strID = persondata.at(iRow)->ID;
    8.  
    9. ui->listWidget->clear();
    10. persondata.clear();
    11. persondata = dbManager->getPerson(strID);
    12.  
    13. PersonData* persondata3;
    14.  
    15. foreach(persondata3,persondata)
    16. {
    17.  
    18. ui->listWidget->addItem(gadata3->FIRSTNAME);
    19.  
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jins; 26th November 2010 at 16:23.

  5. #5
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    Quote Originally Posted by jins View Post
    Thank you very much for replying. I have tried adding the above code and i get an error class QListWidgeItem has no member named 'ID'
    Sorry, there is a typo in my code, use this one:
    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. QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
    13. item->setData(Qt::UserRole, QVariant(persondata2->ID));
    14. ui->listWidget->addItem(item);
    15. }
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    . The program crashes out when i click on a result record in the listwidget.
    Run it in a debugger and see exactly which line is crashing.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    Quote Originally Posted by high_flyer View Post
    Do you mean ?:
    Qt Code:
    1. int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
    2. QString strID = presondata.at(iRow)->ID;
    To copy to clipboard, switch view to plain text mode 
    "selectedItems()" can be an empty list, you should change the above code to somthing like this one,
    Qt Code:
    1. if ( !ui->listWidget->selectedItems().isEmpty() && ui->listWidget->selectedItems().at(0) )
    2. {
    3. int iRow = ui->listWidget->row( ui->listWidget->selectedItems().at(0) );
    4. int strID = presondata.at(iRow)->ID;
    5. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Help with getting record ID from a QList when clicking a record in listwidget

    I'd say you should switch to a model based approach with custom roles or columns keeping particular pieces of data. You can do the same with QListWidget but then you'll need to have your data in two places which is more work than just having a single model and using it in two places.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 26th March 2010, 04:32
  2. Record video
    By tanatos in forum Qt Programming
    Replies: 4
    Last Post: 17th March 2010, 05:33
  3. Selected Record...
    By Nefastious in forum Newbie
    Replies: 1
    Last Post: 27th October 2009, 09:54
  4. Inserting Record
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 04:28
  5. QTableView, add new record?
    By grellsworth in forum Qt Programming
    Replies: 6
    Last Post: 5th July 2007, 15:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.