×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Regular Contributor
    Join Date
    Nov 2010
    Posts
    52

    Help with getting records from QList from a QlistWidget selection click

    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.

  2. #2
    Regular Contributor
    Join Date
    Nov 2010
    Posts
    52

    Re: Help with getting records from QList from a QlistWidget selection click

    The button click event works and displays records (list of users firstnames) in the listwidget. I would like to now be able to click or double tap on a record which will then display the entire users record details i.e. ID, FIRSTNAME, LASTNAME and AGE.

    Because there could be many users with same firstname i need to get the records ID (Unique database id) so that i can make a select against it. Could some one please guide me on how to achieve this, i would really appriciate any help.


    Here is code:

    dal.h

    Code:
    #ifndef DAL_H
    #define DAL_H
    
    #include <QObject>
    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlError>
    #include <QtSql/QSqlQuery>
    #include <QString>
    #include <QVariant>
    
    
    class PersonData
        {
        public:
            PersonData();
            ~PersonData();
            int     ID;
            QString FIRSTNAME;
            QString LASTNAME;
            int     AGE;
        };
    
    
    class Dal : public QObject
        {
        public:
            Dal(QObject *parent = 0);
            ~Dal();
    
        public:
            bool openDB();
            QList<PersonData*> getPerson();
    
    
        private:
        QSqlDatabase db;
        };
    
    
    #endif // DAL_H

    dal.cpp

    Code:
    #include "dal.h"
    
    Dal::Dal(QObject *parent):
            QObject (parent)
    {
    }
    
    Dal::~Dal()
    {
        if (db.isOpen())
        db.close();
    }
    
    PersonData::PersonData()
    {
        ID = 0;
    }
    
    PersonData::~PersonData()
    {
    }
    
    
    bool Dal::openDB()
        {
    
        // Find QSLite driver
        db = QSqlDatabase::addDatabase("QSQLITE");
    
        #ifdef Q_OS_LINUX
        // NOTE: We have to store database file into user home folder in Linux
        QString path(QDir::home().path());
        path.append(QDir::separator()).append("mydb.sqlite");
        path = QDir::toNativeSeparators(path);
        db.setDatabaseName(path);
        #else
        // NOTE: File exists in the application private folder, in Symbian Qt implementation
        db.setDatabaseName("mydb.sqlite");
        #endif
    
        // Open databasee
        return db.open();
    
        }
    
    
    QList<PersonData*> Dal::getPerson()
    {
        QList<PersonData*> result;
    
        QSqlQuery query("select ID, FIRSTNAME, LASTNAME, AGE from PERSON");
        while (query.next()) {
            PersonData* item = new PersonData();
            item->ID = query.value(0).toInt();
            item->FIRSTNAME = query.value(1).toString();
            item->LASTNAME = query.value(2).toString();
            item->AGE = query.value(3).toInt();
            result.append(item);
        }
    
        return result;
    }

    mainwindow.cpp


    Code:
    void MainWindow::on_pushButton1_clicked()
    {
    
        ui->listWidget->clear();
        persondata.clear();
        persondata = dbManager->getPerson();
    
        PersonData* persondata2;
    
        foreach(persondata2,persondata)
        {
            ui->listWidget->addItem(persondata2->FIRSTNAME);
        }
    
    }

Similar Threads

  1. QListWidget : avoid click before selecting
    By henk_marsman in forum Qt
    Replies: 16
    Last Post: 2011-03-29, 16:03
  2. QList Query
    By jainmonali21 in forum Qt
    Replies: 1
    Last Post: 2010-06-24, 16:22
  3. Replies: 1
    Last Post: 2010-05-20, 11:37
  4. joystick righ click and left click and right menu option
    By omoshtagh in forum Mobile Java General
    Replies: 3
    Last Post: 2009-03-18, 08:56
  5. Replies: 5
    Last Post: 2008-01-07, 07:03

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •