信号插槽连接。该过程被迫结束

时间:2019-02-26 04:18:29

标签: qt

以下是我在信号插槽连接演示中的代码:

Mainwindow.cpp(它从下面显示的Sender.cpp接收信号数据)

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    status = false; //bool status
    check = 0; //int check

    sender = new Sender(); //Sender *sender
}

MainWindow::~MainWindow()
{
    delete ui;
    if(sender!=NULL)
    {
        delete sender;
    }
}

void MainWindow::on_pushButton_clicked() //a pushbutton is created in mainwindow.ui
{
    if(sender==NULL)
    {
        sender = new Sender();
    }

    if(status==false)
    {
        qDebug()<<"Button pressed.";
        connectionStarter(); //Signal-slot connection is triggered here!
    }
}

void MainWindow::connectionStarter()
{
    connect(sender,SIGNAL(sendData(int)),this,SLOT(workSlot(int)),Qt::QueuedConnection);
}

void MainWindow::workSlot(int data)
{
    this->data = data;
    check++;

    if(check!=60)
    {
        qDebug()<<"connected. check="<<check;
        qDebug()<<"received data="<<this->data;
    }
    else
    {
        check=0;
        status = true;
        disconnect(sender,SIGNAL(sendData(int)),this,SLOT(workSlot(int)));
        qDebug()<<"Disconnected.";
    }
}

Sender.cpp(用于信号生成,由QTimer对象控制)

#include "sender.h"

Sender::Sender(QObject *parent) : QObject(parent)
{
    data = 0; //data to be updated and transmitted
    timer = new QTimer(this); //QTimer *timer
    timer->setInterval(1000);
    timer->start();

    qDebug()<<"Timer started.";
    connect(timer,SIGNAL(timeout()),this,SLOT(timerHandler()),Qt::DirectConnection);
}

void Sender::timerHandler()
{
    data++; //int data
    emit sendData(data);
    qDebug()<<"Data emited.";
}

运行程序时,it has unexpectedly finished. The process was ended forcefully。任何人都可以帮助调试我的代码吗?谢谢!

0 个答案:

没有答案