PDA

View Full Version : Thread signal/slot problem



K4ELO
31st December 2012, 19:56
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:


HamlibNotice::HamlibNotice(QWidget *parent) :
QDialog(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:


LoadHamlibThread::LoadHamlibThread(QObject *parent) :
QThread(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?

Lesiok
31st December 2012, 20:29
What is a load object in connect line (line 8) ? I do not see the relationship between load and hamlibThread.

K4ELO
31st December 2012, 21:04
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();
};

amleto
1st January 2013, 03:01
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:

void LoadHamlibThread::run()
{
LoadHamlib* loadhamlib = new LoadHamlib;
loadhamlib->load();
exec();
}

K4ELO
1st January 2013, 14:34
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.