Results 1 to 4 of 4

Thread: Threading or signal/slot

  1. #1
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question Threading or signal/slot

    Dears,

    I have a question about the QThread with signal/slot. I am wondering if using a signal to call a slot is also a kind of threading?
    If not, is that true if I created a qthread(mythread) but calling it by a signal, they would stay in the same thread?

    Actually, I am having a problem like, I have a mainwindow for the UI, when I calling some heavy function(e.g. rotation a very large picture in a pIII 500MHz), the main UI got a notice (Not Responding) if I click on it or just wait, I main UI got whitten=.=. I wanna to solve it so I tried to use signal/slot and put that heavy function into a thread, and I called that function(mythread.run()) when a receive a signal from the qpushbutton. Yet the problem still there!=.=
    So I am wondering is that something I can do really like separating the main UI's operations(e.g. receiving signals for other thread functions) when I running a function on a queue(e.g. current running the rotation of a picture and will be followed by a flipping of the picture just rotated).

  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: Threading or signal/slot

    Quote Originally Posted by lip View Post
    Dears,

    I have a question about the QThread with signal/slot. I am wondering if using a signal to call a slot is also a kind of threading?
    No, it's not the same.

    If not, is that true if I created a qthread(mythread) but calling it by a signal, they would stay in the same thread?
    What is "they"?
    You can use signals and slots to communicate between threads. But you need to take care that slots are executed in the thread you want.

    Actually, I am having a problem like, I have a mainwindow for the UI, when I calling some heavy function(e.g. rotation a very large picture in a pIII 500MHz), the main UI got a notice (Not Responding) if I click on it or just wait, I main UI got whitten=.=. I wanna to solve it so I tried to use signal/slot and put that heavy function into a thread, and I called that function(mythread.run()) when a receive a signal from the qpushbutton. Yet the problem still there!=.=
    So I am wondering is that something I can do really like separating the main UI's operations(e.g. receiving signals for other thread functions) when I running a function on a queue(e.g. current running the rotation of a picture and will be followed by a flipping of the picture just rotated).
    Just put the rotating code in a thread, or better yet, use Qt Concurrent (there's an image manipulation example in the docs)

  3. #3
    Join Date
    Dec 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Threading or signal/slot

    Quote Originally Posted by tbscope View Post
    No, it's not the same.


    What is "they"?
    You can use signals and slots to communicate between threads. But you need to take care that slots are executed in the thread you want.



    Just put the rotating code in a thread, or better yet, use Qt Concurrent (there's an image manipulation example in the docs)
    I see the point, but I am using qt 4.3 without qconcurrent class.

    But for example i have following code, what i expected is, after the signal has been emitted, it will not wait util the slot function has been finished then goto the next statement. Because the slot function rotation2() is a function in a qthread class I inherited. Is there any condition I need to do to tell qt where to start the fork(threading) from master thread?

    .....
    connect:someclass,SIGNAL(backrotation(double),mythreadclas s.SLOT(rotation2(double)));
    .....
    .....

    int someclass::rotation(double &degree)
    {
    ...
    ...
    ...
    emit backrotation(degree); /*is there any method to start this function and no need to wait for it to finish for the return statement below*/
    return 0;
    }


    Added after 32 minutes:


    One more thing, if mythreadclass::run() does not include qtcpsocket, is it all fine?
    Last edited by lip; 26th December 2010 at 18:31.

  4. #4
    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: Threading or signal/slot

    Something in the lines of this will work:

    Qt Code:
    1. class RotationClass : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. RotationClass();
    7. void setImage(imagefile);
    8.  
    9. public slots:
    10. void doRotation();
    11.  
    12. signals:
    13. void rotationFinished(imagefile);
    14. }
    15.  
    16. // Implement the RotationClass
    17.  
    18. // To use it:
    19.  
    20. RotationClass *rotator = new RotationClass;
    21.  
    22. rotator->setImage(someImage);
    23. connect(anotherClass, SIGNAL(whatever()), rotator, SLOT(doRotation()));
    24.  
    25. QThread *workerThread = new QThread;
    26. rotator->moveToThread(workerThread);
    27. workerThread->start();
    To copy to clipboard, switch view to plain text mode 

    Make sure that your version of Qt starts an eventloop when doing workerThread->start().

Similar Threads

  1. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 10:00
  2. signal and slot
    By Devoraz in forum Newbie
    Replies: 3
    Last Post: 12th August 2009, 10:06
  3. Signal & Slot
    By LordQt in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2007, 22:22
  4. Replies: 1
    Last Post: 8th November 2007, 17:11
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.