Results 1 to 8 of 8

Thread: Main loop thread loop communication

  1. #1
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Main loop thread loop communication

    is there a way of communication between main loop's objects' signals and thread objects' slots and vise versa.

    For example:
    main() has:
    obj1 has signal1
    obj2 has slot slot2

    thread
    tread.start()
    -----------------------------------------------------------

    thread has:
    obj3 has signal3
    obj4 has slot4
    -----------------------------------------------------------

    is it possible to call slot2 when signal3 emitted?
    is it possible to call slot4 when signal1 emitted?

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

    Default Re: Main loop thread loop communication

    Yes, it is possible.
    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 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Main loop thread loop communication

    in main loop i can not access obj3's or obj4's signals or slots. I could only refer to thread's signal or slots
    how can i connect?
    main()
    {
    QObject::connect(thread->obj3, SIGNAL(signal3),
    &obj2, SLOT(slot2), Qt::QueuedConnection); //failed with message "thread has no signal3 event"

    }

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Main loop thread loop communication

    define a function in 'thread' that returns a pointer to obj3. Or forward the signal:
    Qt Code:
    1. //in thread
    2. connect(obj3, SIGNAL(signal3()), this, SIGNAL(forwardedFrom3()));
    3.  
    4. //in main
    5. connect(thread, SIGNAL(forwardedFrom3()), ...);
    To copy to clipboard, switch view to plain text mode 

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

    mcsahin (25th January 2011)

  6. #5
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Re: Main loop thread loop communication

    I have found another solution, is it correct?

    Qt Code:
    1. //in thread constructor
    2. obj3 = new obj3;
    3. obj3->moveToThread(this);
    4.  
    5. //in main
    6. connect(thread->obj3, SIGNAL(signal3), obj2, SLOT(slot2), Qt::QueuedConnection);
    To copy to clipboard, switch view to plain text mode 

    it works!

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Main loop thread loop communication

    Isn't this simpler?
    Qt Code:
    1. QThread *thread = new QThread;
    2. thread->start();
    3. QObject *obj3 = new QObject;
    4. obj3->moveToThread(thread);
    5. connect(obj3, SIGNAL(...), obj2, SLOT(...));
    To copy to clipboard, switch view to plain text mode 
    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.


  8. The following user says thank you to wysota for this useful post:

    mcsahin (25th January 2011)

  9. #7
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Main loop thread loop communication

    Quote Originally Posted by wysota View Post
    Isn't this simpler?
    Qt Code:
    1. QThread *thread = new QThread;
    2. thread->start();
    3. QObject *obj3 = new QObject;
    4. obj3->moveToThread(thread);
    5. connect(obj3, SIGNAL(...), obj2, SLOT(...));
    To copy to clipboard, switch view to plain text mode 
    yes, actually they are same but i am going to give the thread code to someone, because of that creating in thread is better for me.
    is there any disadvantages of using moveToThread?

    thanks guys!

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,356
    Thanks
    3
    Thanked 5,010 Times in 4,789 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Blog Entries
    4
    Wiki edits
    10

    Default Re: Main loop thread loop communication

    Quote Originally Posted by mcsahin View Post
    yes, actually they are same
    No, they are not. In my code obj3 is not part of the thread object. In your code QThread needs to be subclasses, in mine it doesn't. And if someone subclasses QThread without a really good reason and without being sure he knows what he's doing, he will most likely run into problems.

    is there any disadvantages of using moveToThread?
    Don't use it blindly in a constructor of QThread.
    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.


Similar Threads

  1. main loop
    By dusza in forum Qt Programming
    Replies: 10
    Last Post: 6th June 2009, 17:29
  2. HAL & DBusConnection & Qt main loop integration
    By juannm in forum Qt Programming
    Replies: 3
    Last Post: 15th January 2009, 08:06
  3. Main Thread Event loop
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2007, 12:10
  4. How to bind Qt3 and Qt4 main event loop.
    By Opilki_Inside in forum Qt Programming
    Replies: 7
    Last Post: 10th May 2006, 08:04
  5. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.