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:
Code:
HamlibNotice
::HamlibNotice(QWidget *parent
) : ui(new Ui::HamlibNotice)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
connect(&load, SIGNAL(updateVal(int)), this, SLOT(updatePB(int)));
LoadHamlibThread* hamlibThread = new LoadHamlibThread;
connect(hamlibThread, SIGNAL(finished()), this, SLOT(closeNotice()));
hamlibThread->loadHamlib();
}
HamlibNotice::~HamlibNotice()
{
delete ui;
}
void HamlibNotice::updatePB(int val)
{
qDebug() << "in pb slot";
ui->progressBar->setValue(val);
}
void HamlibNotice::closeNotice()
{
HamlibNotice::close();
}
The thread:
Code:
LoadHamlibThread
::LoadHamlibThread(QObject *parent
) :{
}
void LoadHamlibThread::loadHamlib()
{
start();
}
void LoadHamlibThread::run()
{
LoadHamlib* loadhamlib = new LoadHamlib;
loadhamlib->load();
exec();
}
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?
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.
Re: Thread signal/slot problem
Code:
class HamlibNotice
: public QDialog{
Q_OBJECT
public:
explicit HamlibNotice
(QWidget *parent
= 0);
~HamlibNotice();
private:
Ui::HamlibNotice *ui;
LoadHamlib load;
public slots:
void updatePB(int);
void closeNotice();
};
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:
Code:
void LoadHamlibThread::run()
{
LoadHamlib* loadhamlib = new LoadHamlib;
loadhamlib->load();
exec();
}
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.