Results 1 to 6 of 6

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

  1. #1
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question How to use external C library which has got call back funtions and update GUI

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use external C library which has got call back funtions and update GUI

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use external C library which has got call back funtions and update GUI

    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.

  4. #4
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use external C library which has got call back funtions and update GUI

    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
    Last edited by tama; 5th January 2015 at 03:46.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,233
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to use external C library which has got call back funtions and update GUI

    Maybe this article will help.

  6. #6
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use external C library which has got call back funtions and update GUI

    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

Similar Threads

  1. Replies: 1
    Last Post: 17th April 2014, 06:11
  2. Database stored funtions calling
    By gbaguma in forum Qt Programming
    Replies: 8
    Last Post: 11th July 2013, 10:12
  3. Replies: 0
    Last Post: 20th September 2011, 07:53
  4. QLineEdit update before external method
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 19th August 2011, 19:52
  5. Replies: 11
    Last Post: 14th March 2011, 14:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.