MicroLabs 2015-09-11 05:28 采纳率: 0%
浏览 3851
已结题

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

  • 写回答

3条回答

  • Robot-C 2015-09-11 05:32
    关注

    1.一个主界面(主线程),一个次线程,次线程通过信号向主线程传递递增变量,主线程将它显示出来
    mythread.h

    #include
    #include
    class Mythread : public QThread
    {
    Q_OBJECT
    public:
    Mythread();
    protected:
    void run();
    private:......
    答案就在这里:QT 多线程信号与槽(一)
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?

悬赏问题

  • ¥15 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示