Results 1 to 3 of 3

Thread: Problem using signals and thread

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem using signals and thread

    Hello, I have the following problem:
    I have to execute a long algorithm when a user pushes a button. So I created a new class that is a subclass from QThread and use the button push to start said thread. But I also want to have some sort of measure of the progress and for that I wanted to create a signal and a slot. So I made a little test program. However when I used connect, to connect my slot and signal I get a console message (I'm compiling in debug mode):

    Object::connect: No such signal QThread:: p(int)
    Object::connect: (receiver name: 'ConnectTryClass')

    Where p is the name of my signal in my QThread subclass.

    Here is the code for the Sender class (.cpp) (QThread decendant):
    Qt Code:
    1. #include "Sender.h"
    2.  
    3. Sender::Sender()
    4. {
    5. }
    6.  
    7. void Sender::run(){
    8. int N = 10000000;
    9. int a;
    10. for (int i = 0; i < N; i++){
    11. a++;
    12. double val = (double)(i+1)*100/double(N);
    13. emit p((int)(val));
    14. }
    15. }
    16.  
    17. void Sender::p(int val){
    18. }
    19.  
    20. Sender::~Sender()
    21. {
    22. }
    To copy to clipboard, switch view to plain text mode 

    and its header:
    Qt Code:
    1. class Sender: public QThread
    2. {
    3. public:
    4. Sender();
    5. void run();
    6. ~Sender();
    7.  
    8. signals:
    9. void p(int val);
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    And where I call the connec (ConnectTry.cpp)t:
    Qt Code:
    1. #include "connecttry.h"
    2.  
    3. ConnectTry::ConnectTry(QWidget *parent): QMainWindow(parent)
    4. {
    5. ui.setupUi(this);
    6. sender = new Sender();
    7. bool res = connect(sender,SIGNAL(p(int)),this,SLOT(on_p(int)));
    8. if (res)
    9. ui.teLog->append("Yes");
    10. else{
    11. ui.teLog->append("No");
    12. }
    13. }
    14.  
    15. void ConnectTry::on_p(int val){
    16. ui.pbDone->setValue(val);
    17. }
    18.  
    19. void ConnectTry::on_pbTest_clicked(){
    20. sender->start(QThread::InheritPriority);
    21. ui.teLog->append("Done");
    22. }
    To copy to clipboard, switch view to plain text mode 

    Can any one tell me what I'm doing wrong?
    Thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem using signals and thread

    You forgot the Q_OBJECT macro.

  3. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem using signals and thread

    Thank you very much that effectively solved my problem

Similar Threads

  1. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. GUI thread and Working thread comunication
    By FasTTo in forum Qt Programming
    Replies: 2
    Last Post: 13th September 2007, 15:31
  3. Memory Problem with SIGNALS and SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2007, 20:39
  4. Replies: 11
    Last Post: 7th July 2006, 13:09
  5. Replies: 2
    Last Post: 6th January 2006, 21:15

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.