QT 多线程 信号与槽,该怎么解决
QT 多线程 信号与槽
.H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QThread>
#include <QLabel>
#include <QPointer>
class SecondFirer:public QObject
{
Q_OBJECT
public:
SecondFirer(int seconds) : m_nSeconds(seconds) {}
signals:
void secondLeft(int sec, unsigned int id);
public slots:
void onTimeout();
private:
int m_nSeconds;
};
class CountThread : public QThread
{
Q_OBJECT
public:
CountThread(QWidget *receiver);
~CountThread();
protected:
void run();
private:
QPointer<QWidget> m_receiver;
};
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void onSecondLeft(int sec, unsigned int id);
private:
QLabel *m_Label;
};
#endif // WIDGET_H
.CPP
#include "widget.h"
#include <QDebug>
#include <QTimer>
#include <QMessageBox>
#include <QVBoxLayout>
void SecondFirer::onTimeout()
{
if (m_nSeconds >= 0)
{
unsigned int id = (unsigned int)QThread::currentThreadId();
emit secondLeft(m_nSeconds, id);
--m_nSeconds;
qDebug() << "fire secondLeft signal";
}else
{
QThread::currentThread()->exit(0);
}
}
CountThread::CountThread(QWidget *receiver) : m_receiver(receiver)
{
}
CountThread::~CountThread()
{
qDebug() << "~~CountThread";
}
void CountThread::run()
{
qDebug() << "CountThread id -" << (unsigned int)QThread::currentThreadId();
QTimer timer;
SecondFirer firer(10);
connect(&timer, SIGNAL(timeout()), &firer, SLOT(onTimeout()));
timer.start(1000);
if (!m_receiver.isNull())
{
qDebug() << "connect firer && receiver";
connect(&firer, SIGNAL(secondLeft(int,unsigned int)), m_receiver.data(), SLOT(onSendLeft(int, unsigned int)));
}
exec();
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *lay= new QVBoxLayout(this);
m_Label = new QLabel();
lay->addWidget(m_Label);
CountThread *t = new CountThread(this);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
t->start();
m_Label->setText("dfsaf");
}
Widget::~Widget()
{
}
void Widget::onSecondLeft(int sec, unsigned int id)
{
QString str = QString("remain %1, current thread id %2, fire thrread id %3")
.arg(sec).arg((unsigned int)QThread::currentThreadId()).arg(id);
m_Label->setText(str);
if (sec == 0)
{
QMessageBox::information(this, "CLAP NOW", "Tt\'s time to clap now!Clap! Clap! Clap!");
}
}
出现的错误是:
QObject::connect: No such slot Widget::onSendLeft(int, unsigned int) in ..\Count\widget.cpp:45
------解决思路----------------------
Widget::onSecondLeft,函数名字是这个,run()里面连接时写错了。
.H
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QThread>
#include <QLabel>
#include <QPointer>
class SecondFirer:public QObject
{
Q_OBJECT
public:
SecondFirer(int seconds) : m_nSeconds(seconds) {}
signals:
void secondLeft(int sec, unsigned int id);
public slots:
void onTimeout();
private:
int m_nSeconds;
};
class CountThread : public QThread
{
Q_OBJECT
public:
CountThread(QWidget *receiver);
~CountThread();
protected:
void run();
private:
QPointer<QWidget> m_receiver;
};
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
void onSecondLeft(int sec, unsigned int id);
private:
QLabel *m_Label;
};
#endif // WIDGET_H
.CPP
#include "widget.h"
#include <QDebug>
#include <QTimer>
#include <QMessageBox>
#include <QVBoxLayout>
void SecondFirer::onTimeout()
{
if (m_nSeconds >= 0)
{
unsigned int id = (unsigned int)QThread::currentThreadId();
emit secondLeft(m_nSeconds, id);
--m_nSeconds;
qDebug() << "fire secondLeft signal";
}else
{
QThread::currentThread()->exit(0);
}
}
CountThread::CountThread(QWidget *receiver) : m_receiver(receiver)
{
}
CountThread::~CountThread()
{
qDebug() << "~~CountThread";
}
void CountThread::run()
{
qDebug() << "CountThread id -" << (unsigned int)QThread::currentThreadId();
QTimer timer;
SecondFirer firer(10);
connect(&timer, SIGNAL(timeout()), &firer, SLOT(onTimeout()));
timer.start(1000);
if (!m_receiver.isNull())
{
qDebug() << "connect firer && receiver";
connect(&firer, SIGNAL(secondLeft(int,unsigned int)), m_receiver.data(), SLOT(onSendLeft(int, unsigned int)));
}
exec();
}
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *lay= new QVBoxLayout(this);
m_Label = new QLabel();
lay->addWidget(m_Label);
CountThread *t = new CountThread(this);
connect(t, SIGNAL(finished()), t, SLOT(deleteLater()));
t->start();
m_Label->setText("dfsaf");
}
Widget::~Widget()
{
}
void Widget::onSecondLeft(int sec, unsigned int id)
{
QString str = QString("remain %1, current thread id %2, fire thrread id %3")
.arg(sec).arg((unsigned int)QThread::currentThreadId()).arg(id);
m_Label->setText(str);
if (sec == 0)
{
QMessageBox::information(this, "CLAP NOW", "Tt\'s time to clap now!Clap! Clap! Clap!");
}
}
出现的错误是:
QObject::connect: No such slot Widget::onSendLeft(int, unsigned int) in ..\Count\widget.cpp:45
------解决思路----------------------
Widget::onSecondLeft,函数名字是这个,run()里面连接时写错了。