Results 1 to 5 of 5

Thread: How to handle data provided by thread created from external library?

  1. #1
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    1

    Default How to handle data provided by thread created from external library?

    Hey,

    I have a library to communicate with sensors and different hardware. This library creates internal thread and allows me to bind receiver function. its non qt lib.

    For now I know that new data is generated every 100ms, so I have created QTimer that invokes every 100 ms and "eat" this data. QMutex is protecting both threads. It works, but I don't think that this is correct/best solution.

    How can I push data (std::vector<int>) to SLOT of my main window?

    Thanks for suggestions.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to handle data provided by thread created from external library?

    You say you can "bind a receiver function".
    I assume that means that the library allows you to specify a function that is invoked whenever there is new data.

    The easiest way is probably to emit a signal transporting the data as its argument right from that function.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    1

    Default Re: How to handle data provided by thread created from external library?

    Thanks!

    but...

    1. I would like to prevent creating a copy of data (lot of them).
    I was thinking about some kind of swap with vector created in thread with internal vector of my mainwindow class.
    Pass reference to slot?? is it possible??
    Allocate data and pass to slot using some kind of shared_ptr?

    2. Is it safe to emit signals not from QThread?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to handle data provided by thread created from external library?

    Quote Originally Posted by bamboosso View Post
    1. I would like to prevent creating a copy of data (lot of them).
    You could put the data into some kind of mutex protected storage and then only signal new availability to the main thread.

    Quote Originally Posted by bamboosso View Post
    Pass reference to slot?? is it possible?
    Not across threads.


    Quote Originally Posted by bamboosso View Post
    Allocate data and pass to slot using some kind of shared_ptr?
    That might work.

    Quote Originally Posted by bamboosso View Post
    2. Is it safe to emit signals not from QThread?
    Hmm, good question.
    You could ensure that a Qt::QueuedConnection is used by passing that to the connect() statement, or invoking the slot via QMetaObject::invokeMethod() and passing it there.

    Cheers,
    _

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

    bamboosso (30th April 2015)

  6. #5
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    1

    Default Re: How to handle data provided by thread created from external library?

    Qt::QueuedConnection <-- thank you for that. Saved me a lot of time. I'm new in Qt...

    Cheers

Similar Threads

  1. QByteArray can't read all the data provided
    By Vladimir_ in forum Newbie
    Replies: 2
    Last Post: 15th September 2014, 19:41
  2. Replies: 6
    Last Post: 25th May 2013, 23:26
  3. Replies: 3
    Last Post: 20th December 2012, 13:48
  4. HOW TO - created and use shared library (.so)
    By big4mil in forum Qt Programming
    Replies: 4
    Last Post: 26th November 2006, 00:11
  5. New thread to handle paintEvent
    By bitChanger in forum Qt Programming
    Replies: 1
    Last Post: 9th March 2006, 22:41

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.