【发布时间】:2019-04-12 16:43:36
【问题描述】:
我有这段代码,由 2 个 *.cpp 文件和 2 个 *.h 文件构成,我只是不明白如何将信号从一个类发送到另一个类:
我有 mainwindow.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();
};
我需要把插槽、信号和连接方法放在哪里?非常感谢!
【问题讨论】: