PDA

View Full Version : Help with getting record ID from a QList when clicking a record in listwidget



jins
26th November 2010, 12:18
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


#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


#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


void MainWindow::on_pushButton1_clicked()
{

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();

PersonData* persondata2;

foreach(persondata2,persondata)
{
ui->listWidget->addItem(persondata2->FIRSTNAME);
}

}

high_flyer
26th November 2010, 13:17
Do you mean ?:


int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
QString strID = presondata.at(iRow)->ID;

srazi
26th November 2010, 13:24
Maybe there be similar "firstnames" in your database, search doesn't work here!
You need to embed "ID"s to "item"s,



void MainWindow::on_pushButton1_clicked()
{

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();

PersonData* persondata2;

foreach(persondata2,persondata)
{
QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
item->setData(Qt::UserRole, QVariant(item->ID));
ui->listWidget->addItem(item);
}

}

jins
26th November 2010, 16:23
Maybe there be similar "firstnames" in your database, search doesn't work here!
You need to embed "ID"s to "item"s,



void MainWindow::on_pushButton1_clicked()
{

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();

PersonData* persondata2;

foreach(persondata2,persondata)
{
QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
item->setData(Qt::UserRole, QVariant(item->ID));
ui->listWidget->addItem(item);
}

}


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'


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



void MainWindow::on_pushButton1_clicked()
{

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();

PersonData* persondata2;

foreach(persondata2,persondata)
{
QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
item->setData(Qt::UserRole, QVariant(item->ID));
ui->listWidget->addItem(item);
}

}


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:


Do you mean ?:


int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
QString strID = presondata.at(iRow)->ID;


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.


void MainWindow::on_listWidget_itemSelectionChanged()
{

int iRow = ui->listWidget->row(ui->listWidget->selectedItems()[0]);

//HAD TO CHANGE strID to an INT AS QTCREATOR WAS COMPLAINING ABOUT IT BEING A QSTRING
int strID = persondata.at(iRow)->ID;

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson(strID);

PersonData* persondata3;

foreach(persondata3,persondata)
{

ui->listWidget->addItem(gadata3->FIRSTNAME);


}

Added after 13 minutes:


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



void MainWindow::on_pushButton1_clicked()
{

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();

PersonData* persondata2;

foreach(persondata2,persondata)
{
QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
item->setData(Qt::UserRole, QVariant(item->ID));
ui->listWidget->addItem(item);
}

}


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:


Do you mean ?:


int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
QString strID = presondata.at(iRow)->ID;


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.


void MainWindow::on_listWidget_itemSelectionChanged()
{

int iRow = ui->listWidget->row(ui->listWidget->selectedItems()[0]);

//HAD TO CHANGE strID to an INT AS QTCREATOR WAS COMPLAINING ABOUT IT BEING A QSTRING
int strID = persondata.at(iRow)->ID;

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson(strID);

PersonData* persondata3;

foreach(persondata3,persondata)
{

ui->listWidget->addItem(gadata3->FIRSTNAME);


}

srazi
26th November 2010, 19:56
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:


void MainWindow::on_pushButton1_clicked()
{

ui->listWidget->clear();
persondata.clear();
persondata = dbManager->getPerson();

PersonData* persondata2;

foreach(persondata2,persondata)
{
QListWidgeItem *item = new QListWidgeItem(persondata2->FIRSTNAME);
item->setData(Qt::UserRole, QVariant(persondata2->ID));
ui->listWidget->addItem(item);
}

}

high_flyer
27th November 2010, 09:43
. 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.

srazi
27th November 2010, 11:31
Do you mean ?:


int iRow = ui->listWidget->row(ui->listWidget->selctedItmes()[0]);
QString strID = presondata.at(iRow)->ID;


"selectedItems()" can be an empty list, you should change the above code to somthing like this one,


if ( !ui->listWidget->selectedItems().isEmpty() && ui->listWidget->selectedItems().at(0) )
{
int iRow = ui->listWidget->row( ui->listWidget->selectedItems().at(0) );
int strID = presondata.at(iRow)->ID;
}

wysota
27th November 2010, 15:55
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.