Need an Help on Qt Mysql on Linux
-
Hi guys,
I am developing an GUI application that consists of some 'N' number of tabs.I have put some widgets like analog meters on window for displaying data.I use MySQL database in Linux for retrieving data from tables and displaying it on window.For each tab i am querying different database that is resided on some remote places of india connected in network one followed by other.Everything works fine. In case my database is not reachable for some network issues,it is getting struck there only for sometime and my GUI too gets struck for a moment.I do not know how to overcome this.My code for opening databases is like this:
if(db1.isOpen())
{
// do something with database operations
}
else
{
qDebug()<<"database is not reachable/opened";
}
if(db2.isOpen())
{
// do something with database operations
}
else
{
qDebug()<<"database is not reachable/opened";
}
.
.
.
.
.
This is how i have done it..
can anyone suggest me with some solution.Help would be highly appreciated.Awaiting for answers ..............
-
-
Hi,
I never used database and threads before so what I will say might not work.
According to "this document":https://qt-project.org/doc/qt-5.1/qtcore/threads-modules.html#threads-and-the-sql-module the connection and queries must be made on the same thread. Moreover, things might go wrong depending on the database driver internals.
What I would do is create a subclass of QThread which would serve as a wrapper to database actions and communicate asynchronously via signal/slot connections. You will need to use queued connections (Qt::QueuedConnection) in order to communicate.
Something like this:
@
class Wrapper : public QThread {
public:
Wrapper(){ /constructor code/}public slots:
void open(){if(m_db->open()) emit dbOpen();}signals:
void dbOpen();
protected:
void run(){
m_db = &(QSqlDatabase::addDatabase("QMYSQL"));
// some init code
exec();
// some cleanup code
QSqlDatabase::removeDatabase();
}private:
QSqlDatabase *m_db;
};
@This is just a quickly written example, it may contain syntax errors and a better interface could be written.
Don't forget to start/stop your thread. -
Hi ,
As i told earlier,there is a problem on GUI that gets struck when database is not opened due to network problem or access problem.So,
I need some suggestions or part of code for my requirement on multithreading. Basically my need is like thread 1 will fetch an data from database and handle that to another thread where it does updating the GUI fields. so thread 2 is exposed to user events from gui.suggestions would be highly appreciated and eagerly waiting for your replies..Thanks in advance