开发者

Sql connection and GUI problem

开发者 https://www.devze.com 2023-01-28 08:38 出处:网络
In my code I connect MySql database and query data. I draw graphics scene using this data. When database connection return false,

In my code I connect MySql database and query data.

I draw graphics scene using this data.

When database connection return false,

开发者_运维问答my program try to connect database with ( db.Open()).

In the meantime, gui is locked.

Do you have any idea about this problem?

How can I solve it?


To solve this, you'll have to create a multi-threaded application. A process can have multiple threads which are something like sub-processes that run parallel to the main-sub-process (Slam me for this explanation :P). Read here for further information about threads.

In your case, you'll have to spawn a thread which, in the background, makes the MySql connection (QT Doc says "A connection can only be used from within the thread that created it."), fetches the data from the server and draws it to the widget you want. In the time the background thread is doing this, the main run cycle of you application will not get disrupted, and your app will not get unresponsive. However, you'll have to implement a mechanism that ensures that only 1 Mysql-Data-Fetcher-Thread exists at a time, because else it will get chaotic (e.g. multiple threads want access to the same drawing region, which is impossible in QT).

On the QT documentation site exists a very good section about threads and how to implement them in QT. You should also consult the external links there if you're new to thread programming.

0

精彩评论

暂无评论...
验证码 换一张
取 消