Important: Please read the Qt Code of Conduct -

Use QThread or Timer to constantly update GUI?

  • I am working on a program in which when ran, the GUI is launched and the GUI needs to be constantly updated based on the values stored in a database (I'm not using any of the QT database tools). For the program, I have multiple functions that create a the connection to the database and get specific values. Those functions store the values in a struct that I made. From there, I have a function that goes through a vector made up of structs that I made, looks at the data in the struct and updates different parts GUI if need be. Currently, all of these functions are running every second, using the same timer. As I've added more functions to the program, I've noticed that the GUI is updating a little more slowly. To fix this delay and avoid more problems in the future (I still have 2 functions to add that need to be ran constantly), I've been reading up on QThreads. After reading through multiple pages, I still haven't found a solid answer on how I should structure this code.

    Should I even be looking into using threads for this program? If not, what is a better way to implement timers for this scenario? If I should be using threads, do I need to subclass them (read a lot of sites arguing for and against)?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should start by benchmarking these function, you'll see if the times add up "too much".

    Also, how heavy are your GUI updates ?

  • It's mainly QLabel->setText("value"). But, a few functions are always running to check if any of the data needs to be changed.

  • Lifetime Qt Champion

    Gui operations must be done fro the main thread. So no need to think about it further.

  • Lifetime Qt Champion

    In that case you should consider the model view paradigm. Fetch your data, update the model(s) if needed and signal what has changed so the view(s) can update their content.

    You can use QDataWidgetMapper to automate that part.

Log in to reply