Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. Need an Help on Qt Mysql on Linux

Need an Help on Qt Mysql on Linux

Scheduled Pinned Locked Moved India
6 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    beemaneni
    wrote on last edited by
    #1

    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 ..............

    Bala B
    Infinite Computer systems
    Chennai

    1 Reply Last reply
    0
    • U Offline
      U Offline
      unai_i
      wrote on last edited by
      #2

      Hi,
      I assume your database code is on your GUI thread. Try to move all your (potentially) time consuming operations like database queries over network into a separate thread in order to avoid interface freezes. See QThread class for this.
      Good luck.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        beemaneni
        wrote on last edited by
        #3

        Hi unai_i,
        Thanks for your reply.Actually i have created only default thread in my application and can i know how to execute your suggestion with a sample code.
        Thanking you in advance,

        Bala B
        Infinite Computer systems
        Chennai

        1 Reply Last reply
        0
        • U Offline
          U Offline
          unai_i
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            beemaneni
            wrote on last edited by
            #5

            Thank you Mr unai_i.
            i will try your method and get back.

            Bala B
            Infinite Computer systems
            Chennai

            1 Reply Last reply
            0
            • B Offline
              B Offline
              beemaneni
              wrote on last edited by
              #6

              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

              Bala B
              Infinite Computer systems
              Chennai

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved