Results 1 to 10 of 10

Thread: Non-Gui thread Callback API and Qt - how

  1. #1
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Non-Gui thread Callback API and Qt - how

    Hello,

    I am developing a GUI application that use external non-Qt based library. This external library have a callback interface class that I have to inherit and override the virtual methods in order to use the data that comes. The problem is that the callbacks are called from other thread that is created by the external library and in this way I can't update my GUI with the new information that comes. My question is - is there a way to use signal and slots inside the callback interface class in order to update safely my GUI?

    Thank in advance.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-Gui thread Callback API and Qt - how

    Yes, just make sure you use a Qt::QueuedConnection in QObject::connect() when connecting signals to slots. Queued connections are not executed right away but rather queued into (the other threads) event queue and work fine with threads.

  3. #3
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Gui thread Callback API and Qt - how

    Thanks for the reply. Do I have to inherit QObject and put the Q_OBJECT macro in to the interface class?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-Gui thread Callback API and Qt - how

    if you want to emit signals: yes
    (ok, to be more precise: if you want to declare signals, yes.
    you could emit signals that a base class has declared w/o adding a Q_OBJECT.)

  5. The following user says thank you to caduel for this useful post:

    The Storm (29th October 2008)

  6. #5
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Gui thread Callback API and Qt - how

    Hi,

    Thank you for your help. I made my own signals in to the callback interface class and when I connect them everything is working fine. But when I create a new Widget(any kind) in the slot (located in to the main windows class) I am getting incorrect drowing. The widget is not drawn at all until I do not call setAutoFillBackground(true) and show() functions. Ok in this way I will get it drawed but if I made some changes on it ( for example treeview ) like to add an item it will now show up, until I do not call hide(); and show(); functions. Very weird and annoing. I am using Qt 4.4.3 on Windows XP with Visual C++ 2008.

    Please help.

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-Gui thread Callback API and Qt - how

    Make sure the slot is executed in the gui thread's context (print QThread::currentThread() and compare it to the gui thread).

    The widgets you are creating: are these new windows, or just additions to existing windows? In any case, newly created widgets (or a parent of them) have to be shown explicitly.

    If that does not answer your questions, please be more specific and maybe provide some source.

  8. #7
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Gui thread Callback API and Qt - how

    I used QThread::currentThread() and it seems that I am getting the slot calls in to the main GUI thread as it have to be but the problem still exist.

    In my case I create a QTreeWidget and I put items in it. I create it like this:

    Qt Code:
    1. void MainWindow::onLoginSuccess() // slot
    2. {
    3. // 'this' is a pointer to a QMainWindow
    4. QTreeWidget *TreeWidget = new QTreeWidget(this->ui.centralwidget);
    5. TreeWidget->show();
    6. }
    To copy to clipboard, switch view to plain text mode 

    In this way it will not show up, I will have to call setAutoFillBackground(true) before show in order to get the QTreeWidget visible. Ok I can live with that but when I add a items they are not showing up until I don't do hide(), show() calls to the QTreeWidget. I have connected the slot like this:

    Qt Code:
    1. // g_document.getListener() returns a pointer to the Callback API
    2. connect( g_document.getListener(), SIGNAL(loginSuccess()), this, SLOT(onLoginSuccess()), Qt::QueuedConnection );
    To copy to clipboard, switch view to plain text mode 

    But if I create the QTreeWidget for example in to the constuctor of the MainWindow it will work just fine and I am able to add items via slots that are called from the other external library thread using QObject::connect().
    Last edited by The Storm; 30th October 2008 at 15:54.

  9. #8
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Gui thread Callback API and Qt - how

    Anyone, please ?

  10. #9
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Non-Gui thread Callback API and Qt - how

    Is that on purpose that you do not insert the newly created widget into its parent's layout()?

  11. #10
    Join Date
    Aug 2007
    Posts
    166
    Thanks
    16
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Non-Gui thread Callback API and Qt - how

    I do not use layouts at the moment, I think that the widget should display itself when I put the parent in its constructor, but when is called from an slot function it doesn't work correctly...

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.