PDA

View Full Version : QTimer and QSqlDatabase Problem



smtgra011
3rd July 2007, 16:30
Hi

I am currently writing an app that reads information from a database, displays it to a screen, delays for x amount of seconds, then displays some different information.

I'm using QTimer to hadle the dealy but am running into some issues regarding the
QSqlDatabase.

What I do is this:



main()
{
create and open database
call screen1();
}

screen1()
{
read info from database;
display;
QTimer::singleShot(5000,this,screen2());
}

screen2()
{
read info from db;
display;

}


The first display method (screen1) works perfectly. However, the second method fails and says "Database not open". If I remove the line with "QTimer::single..." in it and replace it with "screen2();", then it works perfectly, just with no delay, which is obviously a bummer.

I don't understand the deep inner workings of either of these classes so if someone more knowledgable could perhaps point out why they "appear" to be interfering with each other.

They may well nt be, but, as I said, the fact that removing that line removes the problem tend to suggest as such.

Thanks
G

jacek
3rd July 2007, 17:45
Do you create any objects on the stack in screen1()? Is the call to QTimer::singleShot() in the last line or maybe screen1() does something afterwards?

smtgra011
3rd July 2007, 17:52
I don't explicitly create anything on the stack.
And yes, that is the last line in screen1().

jacek
3rd July 2007, 20:34
Do you use any static or global variables?

smtgra011
4th July 2007, 07:17
The only global variable is the reference to the database.

smtgra011
4th July 2007, 07:31
OK, I seem to have solved it, but I'm interested to know how.

Turns out I had another, half written method called mainLoop() that I was going to call later (note: I never called it in my code) which had, as part of it's instructions, a query call and a close call to the DB. If I comment out that method the code works perfectly.
It's a bit weird, but I'll take it and run.

Thanks for the help.
Cheers
G

jacek
4th July 2007, 16:37
Turns out I had another, half written method called mainLoop() that I was going to call later (note: I never called it in my code) which had, as part of it's instructions, a query call and a close call to the DB.
It's hard to say anything without seeing your code.