自定义信号与槽

#pragma once

#include <QWidget>
#include "ui_RecvSignal.h"

class RecvSignal : public QWidget
{
    Q_OBJECT

public:
    explicit RecvSignal(QWidget *parent = Q_NULLPTR);
    ~RecvSignal();

public slots:
    void RecvMsg(QString str);

private:
    Ui::RecvSignal ui;
};

#include "RecvSignal.h"
#include <QMessageBox>

RecvSignal::RecvSignal(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);
}

RecvSignal::~RecvSignal()
{
}

void RecvSignal::RecvMsg(QString str)
{
    QMessageBox::information(NULL, tr("收到"), str);
}
#pragma once

#include <QWidget>
#include "ui_SendSignal.h"

class SendSignal : public QWidget
{
    Q_OBJECT

public:
    explicit SendSignal(QWidget *parent = Q_NULLPTR);
    ~SendSignal();

signals:
    void SendMsg(QString str);

public slots:
    void ButtonClicked();

private:
    Ui::SendSignal ui;
};

#include "SendSignal.h"

SendSignal::SendSignal(QWidget *parent)
    : QWidget(parent)
{
    ui.setupUi(this);

    connect(this->ui.pushButton, SIGNAL(clicked()), this, SLOT(ButtonClicked()));

}

SendSignal::~SendSignal()
{
}

void SendSignal::ButtonClicked()
{
    emit SendMsg(tr("发送数据----hello"));
}
#include "_1_hello.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>

#include "HelloWidget.h"
#include "SendSignal.h"
#include "RecvSignal.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //_1_hello w;
    //w.show();

    //QLabel label(QLabel::tr("坚持"));
    //label.show();

    //HelloWidget hw;
    //hw.show();

    SendSignal ss;
    RecvSignal rs;

    QObject::connect(&ss, SIGNAL(SendMsg(QString)), &rs, SLOT(RecvMsg(QString)));

    ss.show();


    return a.exec();
}

 

上一篇:QT+event() + 事件过滤器


下一篇:QT+创建两个不相干的窗口实现一个显示一个不显示