PDA

View Full Version : QString problem...



Nefastious
24th September 2009, 19:59
Hi there guys,

I have a bit of a problem here. I have the following code:

Implementation:

//This function will open the database file (which should lie in the path indicated by the user)
void mainwindow::loaddatabase() {
//This pretty much says that the database will be of SQLite type, and opens it up from a given path
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);

//This "if" shows up a messagebox in case the program cannot load a given database
if (!db.open()) {
QMessageBox::warning(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n"
"This program requires a database to operate. Please contact "
"the developer of this software for more information on "
"how to use it.\n\n"), QMessageBox::Ok);
}

//Note: "medadmin" is the name of the table in the SQLite3 database
model->setTable ("medadmin");
model->select();
...
}



//this makes the openFile dialog get path of the database file and then invocate loaddatabase with such path as the parameter
void mainwindow::openfile() {

path = QFileDialog::getOpenFileName(this,
tr("Open Database File"), "/Users", tr("SQLite Database File (*.db)"));
if (!path.isEmpty()){
loaddatabase();
}
}

This was declared in the header:


class mainwindow : public QMainWindow
{
//declaring the path variable
QString path;
...
}

... And it doesn't work! whenever test the program and select a open a .db file he does nothing! I believe that the problem might be associated with the QString path.

Any code with be greatly appreciated :)

Thanks in advance

Nefastious
25th September 2009, 20:22
BUMP

Please help, please...

If any additional information is required please request it :)

lasher
25th September 2009, 20:30
Hi,

Please try to use debugger, set up breakpoint at


if (!path.isEmpty()){
loaddatabase();


and check path value.

gboelter
26th September 2009, 04:05
Play with something like this ...



void mainwindow::openfile()
{
// for testing only ....
qDebug() << "we are in openfile() now";

path = QFileDialog::getOpenFileName(this, tr("Open Database File"), "/Users", tr("SQLite Database File (*.db)"));

qDebug() << path;

if ( !path.isEmpty() )
{
qDebug() << "path not empty";
loaddatabase();
}
else
{
qDebug() << "path is empty";
{
}


Regards Guenther

wysota
26th September 2009, 08:27
What exactly "doesn't work"?

Nefastious
26th September 2009, 12:15
@wysota

it simply doesn't open up any databases, when requested to do so

@at the other guys
will try qdebug and then post another reply with the results

Nefastious
26th September 2009, 19:00
well I did play with qDebug for a while it tells me that well, here are the results:

we are in openfile() now
database opened
path not empty

one thing I added qDebug() << "database opened"
as part of that if that tests if the database is opened (in loaddatabase() ),
so aparently the problem doesn't lie in the path String.

Any ideas where it might lie?

If you need any code I will show it upon request

Thanks in advance

wysota
26th September 2009, 23:48
Did you try asking the database what the problem was?
QSqlDatabase::lastError()

Nefastious
27th September 2009, 15:30
By using


qDebug() << db.lastError();

I get the following:

QSqlError(-1, "", "")

What does this mean?

wysota
27th September 2009, 15:49
It means there is no error in the database object. How do you know the database failed to open?

Nefastious
27th September 2009, 17:29
Well,

there is a model connected to the database and then a TableView connected to the model, and the View remains blank after I supposedly load the database... :(


Any ideas?

wysota
27th September 2009, 18:07
there is a model connected to the database and then a TableView connected to the model, and the View remains blank after I supposedly load the database... :(

It doesn't mean the database failed to open!

What do QSqlDatabase::open() and QSqlDatabase::isOpen() return?

Nefastious
27th September 2009, 19:22
true on both

so we can assume that the problem doesn't lie on the database object itself, but where could it lie? Any ideas?

wysota
27th September 2009, 19:48
The query is probably incorrect or the table is empty. Check the last error of the model.

ChrisW67
27th September 2009, 23:19
A scope issue? "QSqlDatabase db" goes out of scope at the end of the mainwindow::loaddatabase().

wysota
27th September 2009, 23:31
A scope issue? "QSqlDatabase db" goes out of scope at the end of the mainwindow::loaddatabase().

That's irrelevant. SQL databases are kept within a global singleton hash until they are removed by QSqlDatabase::removeDatabase().

Nefastious
29th September 2009, 16:26
Sorry to ask but I am unsure how to use qDebug to check for errors in the model (what member should I use?)

Not sure if this will help but here is the code for the view and the model:



//Note: "medadmin" is the name of the table in the SQLite3 database
model->setTable ("medadmin");
model->select();
qDebug << model->error();

//The following lines edit a couple of parameters in the table view "View" and bind it to the model
ui->View->setWindowTitle ("Patients List");
ui->View->setModel (model);
ui->View->setSelectionBehavior (QAbstractItemView::SelectRows);
ui->View->resizeColumnsToContents();
ui->View->setSelectionMode (QTableView::SingleSelection);
ui->View->setVisible (true);
ui->View->setColumnHidden(0, true);

Nefastious
29th September 2009, 16:29
Note: qDebug << model->error(); is the possible way to use qDebug, it obviously doesn't work, I simply copy+paste'd it by accident... =P