Results 1 to 7 of 7

Thread: how can thread receive data sent by main thread

  1. #1
    Join Date
    Apr 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question how can thread receive data sent by main thread

    got this

    Qt Code:
    1. #include <QThread>
    2.  
    3. class QString;
    4.  
    5. class SerialThread : public QThread
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. SerialThread(QObject *parent = 0);
    11.  
    12. protected:
    13. void run();
    14.  
    15. signals:
    16. void testowy(QString);
    17. };
    To copy to clipboard, switch view to plain text mode 

    while putting signal-slot mechanism to deliver some data from thread to my main its enough to do something like this in main thread as i got

    SerialThread thread;

    Qt Code:
    1. (...)
    2.  
    3. connect(&thread, SIGNAL(testowy(QString)),msg,SLOT(append(QString)));
    To copy to clipboard, switch view to plain text mode 

    but i would like also to send a QString from main thread to thread

    I could call something like thread.publicFunction(QString) from main but i wonder if its a propper way

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can thread receive data sent by main thread

    Try adding exec(); at the end of the run function. Its going to start an event loop for the thread and you will be able to connect some signals to solots in the thread, using Qt::QueuedConnection.

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how can thread receive data sent by main thread

    Just be aware that having slots in a QThread subclass is problematic. Please read the Multithreading material by Bradley T. Hughes, starting from page 33 to understand why.
    J-P Nurmi

  4. #4
    Join Date
    Apr 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can thread receive data sent by main thread

    Hello
    tx for replies!

    Quote Originally Posted by jpn View Post
    Just be aware that having slots in a QThread subclass is problematic. Please read the Multithreading material by Bradley T. Hughes, starting from page 33 to understand why.
    just read it and hope understood as well

    anyway it should not be so problematic in my application

    just short question

    as i'm sending data fom thread to main i use a &thread pointer in connect()...
    is there a way to use app.thread() pointer to make a signal-slot connection using connect() in created thread or there is another way to establish this-way connection?

    as my main tread is a GUI where i just wish to pick one parameter and send it to Qthread subclass maybe there would be better way to make
    Qt Code:
    1. label->moveToThread(thread)
    To copy to clipboard, switch view to plain text mode 

    Greetz, Myx

  5. #5
    Join Date
    Apr 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can thread receive data sent by main thread

    Quote Originally Posted by Grimlock View Post
    Try adding exec(); at the end of the run function. Its going to start an event loop for the thread and you will be able to connect some signals to solots in the thread, using Qt::QueuedConnection.
    by calling thread event loop by exec(); i got problems with signal-slot connection with, as i mentioned above, with stting propper pointer to my main thread

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: how can thread receive data sent by main thread

    Quote Originally Posted by Myx View Post
    Qt Code:
    1. label->moveToThread(thread)
    To copy to clipboard, switch view to plain text mode 
    What are you trying to do? You must not touch GUI in a worker thread:
    Quote Originally Posted by docs
    Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.
    J-P Nurmi

  7. #7
    Join Date
    Apr 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can thread receive data sent by main thread

    Quote Originally Posted by jpn View Post
    What are you trying to do? You must not touch GUI in a worker thread:
    just gave a guess but didnt try it this way...
    i made something like this

    Qt Code:
    1. class SerialThread : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. SerialThread(QObject *mainGUI, QObject *parent = 0)
    7. {
    8. GUI=mainGUI;
    9. }
    10.  
    11. protected:
    12. void run()
    13. {
    14. connect(GUI,SIGNAL(),this,SLOT());
    15. exec();
    16. }
    17.  
    18. private:
    19. QObject *GUI;
    20.  
    21. (...)
    22. };
    To copy to clipboard, switch view to plain text mode 
    and while creating a thread

    Qt Code:
    1. SerialThread *thread=new SerialThread(this);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Struggling with QThread...
    By TemporalBeing in forum Qt Programming
    Replies: 2
    Last Post: 23rd March 2009, 20:46
  2. Replies: 5
    Last Post: 17th January 2008, 21:49
  3. Accessing data from a worker thread
    By steg90 in forum Qt Programming
    Replies: 20
    Last Post: 25th May 2007, 10:20
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. Replies: 10
    Last Post: 20th March 2007, 22:19

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.