10

QT多线程信号槽没有响应

程序逻辑很简单,就是让一个独立线程发送一个信号,UI线程的槽响应,但是运行报错居然是找不到信号
No such signal ,但是编译可以通过,moc文件也有这个信号。大侠们指点一下子,谢谢

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect (&thread,SIGNAL(sendStr(QString)),this,SLOT(MsgStr(QString)),Qt::DirectConnection);
//
}

Dialog::~Dialog()
{
delete ui;
}

// 启动线程按钮
void Dialog::on_startButton_clicked()
{
thread.start();
ui->startButton->setEnabled(false);
ui->stopButton->setEnabled(true);
}

// 终止线程按钮
void Dialog::on_stopButton_clicked()
{
qDebug ()<<"Button";
if (thread.isRunning()) {
thread.stop();
ui->startButton->setEnabled(true);
ui->stopButton->setEnabled(false);
}
}

void Dialog::MsgStr(QString &str)
{
qDebug ()<<str;
}

/////////////////////////////////////
#ifndef DIALOG_H
#define DIALOG_H

#include
#include "mythread.h"
#include

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
Q_OBJECT

public:
explicit Dialog(QWidget *parent = 0);
~Dialog();

private slots:
void on_startButton_clicked();
void on_stopButton_clicked();
void MsgStr(QString &str);
private:
Ui::Dialog *ui;
CanThread thread;

};

#endif // DIALOG_H

////////////////////////////////////////////////////////
#include "mythread.h"
#include

CanThread::CanThread(QObject *parent) :
QThread(parent)
{
stopped = false;
}

void CanThread::run()
{
qreal i = 0;
exec ();
while (!stopped) {
// qDebug() << QString("in CanThread: %1").arg(i);
msleep(1000);
QString str = QString("in CanThread: %1").arg(i);
emit sendStr(str);
// exportFun();
i++;
}
stopped = false;
}

void CanThread::exportFun()
{
msleep(1000);
}

void CanThread::stop()
{
stopped = true;
qDebug ()<<"start 1";
// qDebug ()<<"start 2";
}
////////////////////////////////////////
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include

class CanThread : public QThread
{
Q_OBJECT
public:
explicit CanThread(QObject *parent = 0);
void stop();
void exportFun();
protected:
void run();

private:
volatile bool stopped;

signals:
void sendStr(QString &);
public slots:

};

#endif // MYTHREAD_H

查看全部
sanmao270453171
MicroLabs
2015/09/11 05:28
  • no such signal
  • 点赞
  • 收藏
  • 回答
    私信

3个回复