Qt - 实现自定义信号和槽的正确方法
Qt - Right Way to Implement Custom Signals and Slots
我有一个 MainWindow 和一个 Class;我想使用自定义信号和插槽在它们之间发送数据。我似乎无法弄清楚,我需要帮助。
这是我的代码:
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <iostream>
#include "receive.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void sendit(QString name);
private slots:
void on_send_button_clicked();
void display(QString e)
{
std::cout<<"Here is where I am called this "<<e.toStdString()<<std::endl;
}
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "receive.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Receive *r = new Receive();
connect(this, SIGNAL(sendit(QString)), r, SLOT(display(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_send_button_clicked()
{
emit sendit(ui->lineEdit->text());
}
receive.h
#ifndef RECEIVE_H
#define RECEIVE_H
#include <iostream>
#include <QDialog>
class Receive : public QDialog
{
public:
Receive();
private slots:
void display(QString e);
};
#endif // RECEIVE_H
receive.cpp
#include "receive.h"
#include "mainwindow.h"
Receive::Receive()
{
}
void Receive::display(QString e)
{
std::cout<<"Here is where I am called this "<<e.toStdString()<<std::endl;
}
当我运行这个程序时,我得到这个消息:
06:26:29:启动 C:\Users\Troy\Documents\build-tests-Desktop_Qt_5_14_1_MinGW_32_bit-Debug\tests.exe ...
QObject::connect: 在 ..\tests\mainwindow.cpp:11 中没有这样的插槽 QDialog::display(QString)
QObject::connect:(发件人姓名:'MainWindow')
请问我该如何完成?
感谢您的帮助。
您在 Receive
中的位置需要 public 不是私人的。与其他 class 成员非常相似,私人插槽只能由 class 自己使用。
如果您使用现代连接语法,您将获得更好的编译时错误:
connect(this, &MainWindow::sendit, r, &Receive::display);
您还需要确保将 Q_OBJECT
添加到每个 Qt class,它在 Receive
中缺失。
我有一个 MainWindow 和一个 Class;我想使用自定义信号和插槽在它们之间发送数据。我似乎无法弄清楚,我需要帮助。
这是我的代码: MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <iostream>
#include "receive.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
signals:
void sendit(QString name);
private slots:
void on_send_button_clicked();
void display(QString e)
{
std::cout<<"Here is where I am called this "<<e.toStdString()<<std::endl;
}
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "receive.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
Receive *r = new Receive();
connect(this, SIGNAL(sendit(QString)), r, SLOT(display(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_send_button_clicked()
{
emit sendit(ui->lineEdit->text());
}
receive.h
#ifndef RECEIVE_H
#define RECEIVE_H
#include <iostream>
#include <QDialog>
class Receive : public QDialog
{
public:
Receive();
private slots:
void display(QString e);
};
#endif // RECEIVE_H
receive.cpp
#include "receive.h"
#include "mainwindow.h"
Receive::Receive()
{
}
void Receive::display(QString e)
{
std::cout<<"Here is where I am called this "<<e.toStdString()<<std::endl;
}
当我运行这个程序时,我得到这个消息:
06:26:29:启动 C:\Users\Troy\Documents\build-tests-Desktop_Qt_5_14_1_MinGW_32_bit-Debug\tests.exe ... QObject::connect: 在 ..\tests\mainwindow.cpp:11 中没有这样的插槽 QDialog::display(QString) QObject::connect:(发件人姓名:'MainWindow')
请问我该如何完成?
感谢您的帮助。
您在 Receive
中的位置需要 public 不是私人的。与其他 class 成员非常相似,私人插槽只能由 class 自己使用。
如果您使用现代连接语法,您将获得更好的编译时错误:
connect(this, &MainWindow::sendit, r, &Receive::display);
您还需要确保将 Q_OBJECT
添加到每个 Qt class,它在 Receive
中缺失。