PDA

View Full Version : How to use external C library which has got call back funtions and update GUI



tama
23rd December 2014, 02:41
Hi There,

I am new to QT and this forum. I am working on a project in which I need to use external C library(it has got several threads). This library has got various functions and callback functions. I have managed to integrate this library by defining a separate class with a pointer to main Widget class. These callbacks get called when some data is changed. I need to reflect this data change on the GUI which uses QtLabels. At present I am using a timer and connecting that timer to an gui update functions and some global variables(I know using global variables is bad practise). I am not sure whether this is a correct method to do it, secondly using Qt timers increases the CPU usage drastically. I am using an embedded ARM platfrom to run the application.

Searching online on how to update GUI, people recommended using threads. Being new to QT i am struggling to understand how to run external C library as thread and then update the GUI.

Can any one please shade some light on how to do this, an example project would be really very helpfull to undertand how this can be done.

thanks in advance

wysota
23rd December 2014, 06:46
What you do is not very correct. A better approach is to emit signals from your callbacks. If you connect slots to those signals you will get a nice way to update your UI from within those slots.

d_stranz
23rd December 2014, 18:08
To elaborate on what wysota said, since you are using a C library, your callback functions are going to be C-style (i.e. they are not ordinary member functions of a C++ class). The callbacks can be static member functions of a C++ class, but this doesn't help much. In order to emit a signal, you need an actual QObject class instance, and a static class method doesn't actually have an instance of the class that implements it.

So, the workaround for this is to implement your callback either as a standalone method or as a static method in a C++ class. In the callback, you need to be able to call into your GUI to retrieve a pointer to the GUI object instance you want to update. Maybe it is a progress dialog. Once you have that pointer, then you can call any of its member functions, including slots. And from that point, Qt will take care of it.

Remember that to keep your GUI "alive", you must allow the event loop to run. In a multi-threaded app, where computations are done in a separate thread, updates to the GUI will happen in the GUI thread so all should be OK. In a single-threaded app, even if your callback calls a slot that updates something in the GUI, nothing will happen until the event loop runs again. In this case, you will probably want to call QCoreApplication::processEvents() in your callback before returning to your computation.

tama
5th January 2015, 03:20
thanks for replying wysota, d_stranz. Wish you Happy New Year.

d_stranz, I am new to Qt and C++, at present in my project I have a QtServerWidget as a class and it has a void pointer to other class C_LibServer. C_LibServer has static pointer to QtServerWidget MyQtWidget and static functions for callbacks. In these callback functions I call MyQtWidget and its member function to update the status. So basically i have two classes one for the main QtWidget and one for the C library functions.

From the callback functions I call the MyQtWidget->function. Should I access the slots instead of functions and update the GUI. How do i keep the GUI alive and have a event loop. Could you please explain this bit more. I would appreciate if you could please give a small demo example project. Sorry for asking too many things, as i am new to this and trying to learn it. I would appreciate if you could please provide some pointer to reading material on this.

Thanks once again
Tama

d_stranz
5th January 2015, 03:49
Maybe this article (http://doc.qt.digia.com/qq/qq27-responsive-guis.html) will help.

tama
7th January 2015, 07:35
Thanks for the link d_stranz. I managed to implement separate Qt widget class and C library class and then implement signals in c library class which trigget slots in qt widget class. This updates the GUI properly now and over all CPU usage as drastically gone done as I am not using timers any more to check global variables.

I got a different problem now, my GUI is not fast enough to respond to qpushbutton on the gui. I do not have any threads in my application for GUI. Each pushbutton is connected to a slot which does some data update. How to debug this. In my qt widget class cpp inside the constructor i tried to connect pushbutton signal clicked to this, SLOT(aboutQt()). But as this is a instance for qtwidget class it doesnt have SLOT(aboutQt()), I tried the same thing in main also where qtwidget class intance is created. But there I cannot access UI->pushbutton.

Some pointer would really help. Do i need to do something addition to keep my GUI alive so its responds to phushbuttons immediately.

Thanks
Tama