Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4072834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:56:05+00:00 2026-05-20T16:56:05+00:00

I’m building a Qt based application to monitor and capture data streaming from a

  • 0

I’m building a Qt based application to monitor and capture data streaming from a serial port. The data is plotted real-time, sent out via TCP, and stored into an SQLite database. Unfortunately, I’ve found that the SQLite insertions causes the GUI to become unresponsive, as I am performing serial data handling, plotting, TCP transmission, and database insertion all in the context of the main loop. I researched seperating the database insertions onto another thread and came up with the following code.

 #include <QObject>
#include <QDebug>
#include <QStringList>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <QObject>
#include <QList>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QSqlDatabase>
#include <QSqlRecord>
#include <QString>
#include "mMessage.h"
// The class that does all the work with the database. This class will
// be instantiated in the thread object's run() method.
class Worker : public QObject
{
  Q_OBJECT
   public:
    Worker( QObject* parent = 0);
    ~Worker();
    bool insertADC(mMessage* insertMessage);
  public slots:
    void slotExecute( mMessage* insertMessage );
  signals:
    void queryResult(bool);
   private:
     QSqlDatabase m_database;
     bool m_databaseOpen;
     void prepareQueries();
     QSqlQuery *m_accelerometerQuery;
     QSqlQuery *m_adcQuery;
     QSqlQuery *m_metricsQuery;
     QSqlQuery *m_rssiQuery;
};

class mDatabaseThread : public QThread
{
  Q_OBJECT
  public:
    mDatabaseThread(QObject *parent = 0);
    ~mDatabaseThread();
    void executeInsertion( mMessage* insertMessage );
  signals:
    void progress( const QString& msg );
void ready(bool);
  protected:
    void run();
  signals:
    void executefwd( mMessage* insertMessage );
  private:
     Worker* m_worker;
};

CPP FILE

    #include "mDatabaseThread.h"
    //

    Worker::Worker( QObject* parent )
        : QObject( parent )
    {
        // thread-specific connection, see db.h
        m_database = QSqlDatabase::addDatabase( "QSQLITE",
                                                "WorkerDatabase" ); // named connection
        m_database.setDatabaseName("trainingX.db3");
        if ( !m_database.open() )
        {
            qWarning() << "Unable to connect to database, giving up:" << m_database.lastError().text();
            m_databaseOpen = false;
            return;
        }
     m_databaseOpen = true;

    }

    Worker::~Worker()
    {
        //close the database
       // m_database.close();
       // m_database.removeDatabase("trainingX.db3");
    }
    void Worker::prepareQueries()
    {
        if (m_databaseOpen)
        {
        m_accelerometerQuery->prepare("INSERT INTO accelerometer (key, timestamp, nickname, unitid,"
                                   "sectorid, acc_x, acc_y, acc_z) "
                                   "VALUES (NULL, :timestamp, :nickname, :unitid, :sectorid,"
                                   ":acc_x, :acc_y, :acc_z)");

        m_adcQuery->prepare("INSERT INTO adc (key, timestamp, nickname, unitid, sectorid,"
                         "adc0, adc1, adc2, adc3, adc4, adc5, adc6, adc7) "
                         "VALUES (NULL, :timestamp, :nickname, :unitid, :sectorid,"
                         ":adc0, :adc1, :adc2, :adc3, :adc4, :adc5, :adc6, :adc7)");

        m_metricsQuery->prepare("INSERT INTO metrics (key, timestamp, nickname, unitid, sectorid, "
                             "acc_temp, unit_temp, unit_pressure, bpm_instant, bpm_average, base_pressure, base_temp) "
                             "VALUES (NULL, :timestamp, :nickname, :unitid, :sectorid,"
                             ":acc_temp, :unit_temp, :unit_pressure, :bpm_instant, :bpm_average, :base_pressure, :base_temp)");

        m_rssiQuery->prepare("INSERT INTO rssi (key, timestamp, nickname, unitid, sectorid, "
                          "rssi_1, rssi_2, rssi_3, rssi_4, rssi_avg) "
                          "VALUES (NULL, :timestamp, :nickname, :unitid, :sectorid,"
                          ":rssi_1, :rssi_2, :rssi_3, :rssi_4, :rssi_avg)");
        }
    }

    void Worker::slotExecute( mMessage* insertMessage )
    {
        m_accelerometerQuery = new QSqlQuery("WorkerDatabase");
        m_adcQuery = new QSqlQuery("WorkerDatabase");
        m_metricsQuery = new QSqlQuery("WorkerDatabase");
        m_rssiQuery = new QSqlQuery("WorkerDatabase");

        prepareQueries();

        insertADC(insertMessage);
        //insertRSSI(insertMessage);
        //insertAccelerometer(insertMessage);
        //insertMetrics(insertMessage);
        emit queryResult( true );
    }
bool Worker::insertADC(mMessage *insertMessage)
{
    if (m_databaseOpen)
    {
           // m_adcQuery->bindValue(":key",0);
            m_adcQuery->bindValue(":timestamp",insertMessage->m_timestamp);
            m_adcQuery->bindValue(":nickname",insertMessage->m_nickname);
            m_adcQuery->bindValue(":unitid",insertMessage->m_unitId.toInt());
            m_adcQuery->bindValue(":sectorid",insertMessage->m_sectorId.toInt());
            m_adcQuery->bindValue(":adc0",insertMessage->m_adc0.toInt());
            m_adcQuery->bindValue(":adc1",insertMessage->m_adc1.toInt());
            m_adcQuery->bindValue(":adc2",insertMessage->m_adc2.toInt());
            m_adcQuery->bindValue(":adc3",insertMessage->m_adc3.toInt());
            m_adcQuery->bindValue(":adc4",insertMessage->m_adc4.toInt());
            m_adcQuery->bindValue(":adc5",insertMessage->m_adc5.toInt());
            m_adcQuery->bindValue(":adc6",insertMessage->m_adc6.toInt());
            m_adcQuery->bindValue(":adc7",insertMessage->m_adc7.toInt());
            if (m_adcQuery->exec())
            {
                return true;
            }
            else
            {
                qDebug() << "SQL ADC failed.";
                qDebug() << m_adcQuery->lastError();
                return false;
            }
    }
    else
    {
    //database isn't open
    return false;
    }
}
////database thread

mDatabaseThread::mDatabaseThread(QObject *parent)
    : QThread(parent)
{
}
mDatabaseThread::~mDatabaseThread()
{
    delete m_worker;
}

void mDatabaseThread::executeInsertion( mMessage* insertMessage )
{
    emit executefwd( insertMessage ); // forwards to the worker
}

void mDatabaseThread::run()
{
    emit ready(false);
     // Create worker object within the context of the new thread
    m_worker = new Worker();
    connect( this, SIGNAL( executefwd( mMessage* ) ),
             m_worker, SLOT( slotExecute( mMessage* ) ) );
    connect( m_worker, SIGNAL(queryResult(bool)), this, SIGNAL(ready(bool)));
    emit ready(true);

    exec();  // our event loop
}

The mDatabaseThread class has a worker object which it sends the database work to. The signals and slots are all functioning and get fired correctly. However, the actual QSqlQuery fails complaining that the database is not open – but when I attempt to debug it, I see m_database is actually set to the right file/parameters. It is based on a solution here http://www.linuxjournal.com/article/9602. The mMessage class is a qObject which also gets passed to the m_worker object correctly.

From the main class, I call m_databaseThread->start() in the constructor and then I call the executeInsertion(mMessage*) function. I’ve tried to change the way m_database is initialized as well as the QSqlQueries but it seems no matter what I do, it complains that the QSqlQuery cannot find the database connection.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:56:06+00:00Added an answer on May 20, 2026 at 4:56 pm

    You were close.

    Because you gave a name to the database in addDatabase() (“WorkerDatabase”), the resulting connection is not the application default and will not be returned by QSqlDatabase().

    Because of this, you need to pass the database object to your QSqlQuery constructors:

    m_accelerometerQuery = new QSqlQuery(m_database);
    

    The constructor you’re using here:

    m_accelerometerQuery = new QSqlQuery("WorkerDatabase");
    

    is:

    QSqlQuery (const QString & query = QString(), QSqlDatabase db = QSqlDatabase())
    

    When you pass in “WorkerDatabase” to that it’s being stored as the SQL query, and the default (non-existent) db returned by QSqlDatabase() is being stored for the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a small JavaScript validation script that validates inputs based on Regex. I
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.