【问题标题】:Qt not emitting signalQt没有发出信号
【发布时间】:2018-06-28 07:33:58
【问题描述】:

我是新手,我正在尝试从头文件中发出信号并在我的主类中捕获它。为此写了这个头文件:-

KeyBoard.h:-

#ifndef KEYBOARD_H
#define KEYBOARD_H

#include <QObject>
#include <QDebug>

class KeyBoard : public QObject{
    Q_OBJECT
public:
    KeyBoard();
    int keyboard_update(QByteArray recieved_key);
signals:
    void keyboard_respoense(QString message);
private:
    int level;
    int sub_level;

};

#endif 

KeyBoard.cpp:-

#include "KeyBoard.h"

KeyBoard::KeyBoard()
{
    level = 0;
    sub_level = 0;
}

int KeyBoard::keyboard_update(QByteArray recieved_key)
{
    qDebug() << "recieved key == " << recieved_key;
        qDebug() << "button  == " << recieved_key.at(8);    
    emit keyboard_respoense("PRESS ENTER TO SELECT TRAIN"); 

    return 1;
}

在我的主要课程中,我正在捕捉这样的信号:-

keyboard = new KeyBoard;
connect(keyboard,SIGNAL(keyboard_respoense(QString message)),this,SLOT(send_to_MBU(QString message)));

但是当我使用这一行调用keyboard_update函数时:-

keyboard->keyboard_update(raw_data_MBU_qb);

它进入keyboard_update() 函数但从不发出信号,或者它可能发出信号但我无法捕捉到它。所以,请告诉我我到底错过了什么。

【问题讨论】:

  • 请分享send_to_MBU方法的代码。
  • 您的连接错误。它应该是connect(keyboard, SIGNAL(keyboard_respoense(QString)), this, SLOT(send_to_MBU(QString)));,即不需要包含参数名称。
  • @vahancho:谢谢兄弟。它现在正在工作

标签: c++ windows qt signals


【解决方案1】:

connect 函数中,不需要提及参数名称。只需声明参数类型。像这样连接:-

connect(keyboard,SIGNAL(keyboard_respoense(QString)),this,SLOT(send_to_MBU(QString))); 

【讨论】:

    【解决方案2】:

    为了让编译器帮助你,请更新到 C++11 或更高版本并编写 连接(键盘,&Keyboard::Keyboard_respoense,这个,&SnakeCaseClass:send_to_MBU);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-16
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多