Results 1 to 3 of 3

Thread: need help on inter Qthread communication

  1. #1
    Join Date
    Nov 2009
    Posts
    60
    Thanks
    3
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default need help on inter Qthread communication

    Hi All,

    I am trying out a inter thread communication for my project.

    I have copied the below example with my modification.

    http://stackoverflow.com/questions/6...d-signal-in-qt

    class MyObject : public QObject
    {
    Q_OBJECT
    public slots:
    void MySlot( void )
    {
    std::cout << "slot called" << std::endl;
    }
    };
    - Thread 1
    class CThread1 : public QThread
    {
    Q_OBJECT
    public:
    void run( void )
    {
    std::cout << "thread 1 started" << std::endl;
    int i = 0;
    while(1)
    {
    msleep( 200 );
    i++;
    if(i==1000)
    emit MySignal();
    }
    }
    signals:
    void MySignal( void );
    };

    - MyServer is class I added
    class MyServer
    {

    public slots :

    void sendMessage(int val);


    }

    Thead-2
    class CThread2 : public QThread
    {
    Q_OBJECT
    public:
    void run( void )
    {
    MyServer objServer; --This is my class instance
    std::cout << "thread 2 started" << std::endl;
    exec();
    }
    };


    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    CThread1 oThread1;
    CThread2 oThread2;
    MyObject myObject;
    QObject::connect( & oThread1, SIGNAL( MySignal() ), &myObject, SLOT( MySlot() ) );
    oThread2.start();
    myObject.moveToThread(&oThread2)
    oThread1.start();
    return a.exec();
    }

    my doubt is how we can "sendMessage" method of MyServer from "myObject's" method "MySlot:

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: need help on inter Qthread communication

    Just do the same with MyServer as you do with MyObject. Do not create them in the thread itself, but in, for example, the initialisation of you main window. And then move it to the correct thread. This means you can just use connect(myServer, SIGNAL(), myObject, SLOT)

    Try to leave the run() function of a QThread subclass as is, or at least make sure you use exec().
    What needs to be run in the thread should be added to the object you move to the thread.

  3. The following user says thank you to tbscope for this useful post:

    Ratheendrans (12th October 2010)

  4. #3
    Join Date
    Nov 2009
    Posts
    60
    Thanks
    3
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: need help on inter Qthread communication

    Thanks for the reply.

    I am able to get the expected behaviour.

Similar Threads

  1. Inter App Communication
    By baray98 in forum General Programming
    Replies: 13
    Last Post: 13th February 2010, 18:16
  2. QThread interthread communication
    By SABROG in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:57
  3. QThread communication with GUI
    By ldiamond in forum Qt Programming
    Replies: 1
    Last Post: 14th March 2008, 00:00
  4. inter widget communication
    By venk2ksubbu in forum Qt Tools
    Replies: 2
    Last Post: 14th November 2007, 04:57
  5. Inter Process Communication
    By yellowmat in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2006, 11:44

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.