Results 1 to 5 of 5

Thread: Thread signal/slot problem

  1. #1
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Thread signal/slot problem

    Trying to update a progress bar in a dialog with a signal from an object launched from a thread and it's not working - no updates. Went over the doc and looked at many posts. The code looks right to me, but obviously, something is wrong.

    The dialog:
    Qt Code:
    1. HamlibNotice::HamlibNotice(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::HamlibNotice)
    4. {
    5. ui->setupUi(this);
    6. ui->progressBar->setValue(0);
    7.  
    8. connect(&load, SIGNAL(updateVal(int)), this, SLOT(updatePB(int)));
    9.  
    10. LoadHamlibThread* hamlibThread = new LoadHamlibThread;
    11. connect(hamlibThread, SIGNAL(finished()), this, SLOT(closeNotice()));
    12. hamlibThread->loadHamlib();
    13. }
    14.  
    15. HamlibNotice::~HamlibNotice()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void HamlibNotice::updatePB(int val)
    21. {
    22. qDebug() << "in pb slot";
    23. ui->progressBar->setValue(val);
    24. }
    25.  
    26. void HamlibNotice::closeNotice()
    27. {
    28. HamlibNotice::close();
    29. }
    To copy to clipboard, switch view to plain text mode 

    The thread:
    Qt Code:
    1. LoadHamlibThread::LoadHamlibThread(QObject *parent) :
    2. QThread(parent)
    3. {
    4. }
    5.  
    6. void LoadHamlibThread::loadHamlib()
    7. {
    8. start();
    9. }
    10.  
    11. void LoadHamlibThread::run()
    12. {
    13. LoadHamlib* loadhamlib = new LoadHamlib;
    14. loadhamlib->load();
    15. exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    the loadhamlib class emits the signal updateVal(val);

    The loadhamlib code functions correctly - it loads a database successfully, but the progress bar update slot is never called. What do I have wrong here?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Thread signal/slot problem

    What is a load object in connect line (line 8) ? I do not see the relationship between load and hamlibThread.

  3. #3
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread signal/slot problem

    Qt Code:
    1. class HamlibNotice : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit HamlibNotice(QWidget *parent = 0);
    7. ~HamlibNotice();
    8.  
    9. private:
    10. Ui::HamlibNotice *ui;
    11. LoadHamlib load;
    12.  
    13. public slots:
    14. void updatePB(int);
    15. void closeNotice();
    16. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread signal/slot problem

    you never connect LoadHamlib* loadhamlib to anything so of course nothign will receive its signals.

    this 'load'
    connect(&load, SIGNAL(updateVal(int)), this, SLOT(updatePB(int)));
    has nothing to do with this loadhamlib:
    Qt Code:
    1. void LoadHamlibThread::run()
    2. {
    3. LoadHamlib* loadhamlib = new LoadHamlib;
    4. loadhamlib->load();
    5. exec();
    6. }
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    K4ELO (1st January 2013)

  6. #5
    Join Date
    Nov 2012
    Location
    Kentucky, USA
    Posts
    46
    Thanks
    21
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thread signal/slot problem

    Thanks Amleto - I have moved the "load" code from the LoadHamlib class to the run function in the thread class and changed the connection and it works fine now.

Similar Threads

  1. Qt Jambi: Thread and signal slot problem
    By newb_developer in forum Qt-based Software
    Replies: 1
    Last Post: 18th December 2012, 23:06
  2. Quit thread using signal/slot
    By mounte in forum Qt Programming
    Replies: 5
    Last Post: 31st January 2011, 20:14
  3. QThread Signal Not Received By Main Thread Slot
    By EF2008 in forum Qt Programming
    Replies: 7
    Last Post: 4th June 2010, 08:06
  4. Replies: 9
    Last Post: 28th November 2009, 20:31
  5. Replies: 16
    Last Post: 28th October 2008, 22:00

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
  •  
Qt is a trademark of The Qt Company.