【问题标题】:Emit a signal from another class从另一个类发出信号
【发布时间】:2019-04-12 16:43:36
【问题描述】:

我有这段代码,由 2 个 *.cpp 文件和 2 个 *.h 文件构成,我只是不明白如何将信号从一个类发送到另一个类:

我有 ma​​inwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "serialcommunication.h"
#include "QDebug"

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

}

MainWindow::~MainWindow()
{
    delete ui;
    //Here is where I want to emit the signal
    qDebug() << "DONE!";
}

这是 mainwindow.cpp 的 标头

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
private slots:
    void on_connectButton_clicked();

private:
    Ui::MainWindow *ui;

};

所以,我想从主窗口类发送一个信号到串行通信类,在这里调用一个函数:

第二个 *.cpp 文件:Serialcommunication.cpp

#include "serialcommunication.h"
#include "mainwindow.h"

SerialCommunication::SerialCommunication(QObject *parent) : QObject(parent)
{   
    isStopReadConsoleActivated = false;


    QtConcurrent::run(this,&SerialCommunication::readConsole,isStopReadConsoleActivated);

    }
    void FUNCTION THANT I WANT TO BE CALLED FROM MAINWINDOW CLASS()
    {
//DO SOMETHING
    }

以及串行通信header

class SerialCommunication : public QObject
{
     Q_OBJECT
private:

    //some other fucntions

public:
    explicit SerialCommunication(QObject *parent = nullptr);
     ~SerialCommunication();
};

我需要把插槽、信号和连接方法放在哪里?非常感谢!

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    首先,您需要了解QT的特征SlotsSignal的基本理论。它们允许 QOBJECT 固有的任何对象在它们之间发送消息,例如事件。

    1. 发出事件的类必须实现signal
    //Definition into the Class A (who emits)
    signals:
        void valueChanged(int newValue);
    
    1. 将接收事件 (signal) 的类必须实现一个公共 slot,它必须具有与 signal 相同的参数。
    
    //Definition into the Class B (who receives)
    public slots:
        void setValue(int newValue);
    
    1. 将接收事件 (signal) 的类必须连接 SignalSlot。使用connect 方法链接信号,来自A 类的实例,以及来自B 类实例的槽。
    
    //There is an instance of class A called aEmit.
    void B::linkSignals()
    {
       connect(&aEmit, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));
    }
    
    
    1. 要触发信号,请使用关键字emit 将信号作为函数及其参数:。
    //from Class A
    void A::triggerSignal()
    {
      int myValue{23};
      emit valueChanged(myValue);
    }
    
    
    1. 在B类中,应该调用被声明为槽的方法。
    //from Class A
    void B::setValue(int newValue);
    {
       cout << newValue << endl;
    }
    
    

    您可以在此处查看有关信号和插槽的更多信息。

    https://doc.qt.io/qt-5/signalsandslots.html

    【讨论】:

      【解决方案2】:

      如果要从 MainWindow 向 SerialCom 发送信号,则应在 MainWindow 中定义信号,并在 SerialCom 中定义 slot。在 MainWindow 中,应该为此信号调用一个“发射”(可能来自 on_connectButton_clicked)。 最好从 MainWindow 将信号连接到插槽。 SerailCom 对象应该在那里知道但是这样做。它将类似于(伪代码):

      connect(this, signal(sig_name), comm_object, slot(slot_name))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-17
        • 2013-02-17
        • 2019-07-31
        • 1970-01-01
        • 1970-01-01
        • 2019-02-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多