0

I'm running a C++ program, build with Qt, that never can stop.
The program always fetches data from database and if there is a result sends an SMS.

I'm able to connect to database, but after some hours (+/- 10), it doesn't work anymore.

I don't know if the problem is because I lose connection with database or because my computer goes standby...

I'm not able in Qt to see database status: db.open() always returns true when tested inside while loop.

QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("");
db.setPort();
db.setDatabaseName("");
db.setUserName("");
db.setPassword(""); 
if (db.open())
{
    while (true)
    {
        // MySQL Request
        // If data -> send SMS
    }
}
2
  • I understood where the issue was: not with database or status of computer, but with serial port connection. See new question Commented Oct 20, 2014 at 6:13
  • stackoverflow.com/questions/26459788/… Commented Oct 20, 2014 at 6:48

2 Answers 2

1

There's always the possibility to loose a DB connection for whatever reason. You just can't rely on it. You have to check your connection inside the loop and implement some kind of re-connection scheme if the connection gets lost. As far as I know Qt doesn't do that for you.

Sign up to request clarification or add additional context in comments.

3 Comments

I know, but how to check this connection? QSqlDatabase::open always return true. Even when connection is failed. The only way to see when it fails is to insert data in database, that has to be send
Just do whatever SQL you have intended to do and check the result of your exec() or whatever method you use. If it fails you may check with QSqlDatabase::lastError() for QSqlError::ConnectionError. If true you may have to reinitialize the connection. I doubt that open(...) on a new connection object always returns true even if the database is not available.
You where right, open should return false if not connected. My problem is with serial port connection. Open new question stackoverflow.com/questions/26459788/…
0

Qt provides an event driven framework; events occur and the program reacts to those events.

When you have a never ending loop, events are queued, waiting until they can be processed. In your case, this is never going to happen, so the queue of events will just keep increasing, taking up resources such as memory.

There are two possible ways of solving this. The first is to call QApplication::processEvents every now and again in your loop.

However, the better method would be to remove the while(true) and instead use a QTimer which will periodically call a function to process any available data.

Assuming you have a class, derived from QObject, here's skeleton code using QObject's own timer

class MyObject : public QObject
{
    Q_OBJECT

public:
    MyObject(QObject *parent = 0);

protected:
    // this will be called periodically from the timer
    void timerEvent(QTimerEvent *event);

private:
    m_timerId = 0; // C++ 11 initialisation
};

MyObject::MyObject(QObject *parent)
    : QObject(parent)
{
    m_timerId = startTimer(50);     // 50-millisecond timer
}

void MyObject::timerEvent(QTimerEvent *event)
{
    if(event->timerId() == m_timerId)
    {
        // MySQL Request
        // If data -> send SMS
    }
}

6 Comments

Thank you for your response. Could you give me an exemple? I can only see slot example in Qt documentation; which I don't need. Something like javascript: setInterval(myFunction, 1000);
Example added in C++, as the question is tagged with C++, not javascript.
Thank you! I'm a begginer in Qt and couldn't apply that to my code... Could you just give me the easy example with QApplication::processEvents please?
The easier, but not recommended method is to put just that one line of code "QApplication::processEvents()" inside your while(true) loop. Ensure the file includes QApplication
Yes it is. QApplication is used with widgets and QCoreApplication is without.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.