码迷,mamicode.com
首页 > 其他好文 > 详细

自定义信号与槽

时间:2019-06-30 19:15:57      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:col   pre   main   sign   click   har   connect   include   form   

#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();
}

 

自定义信号与槽

标签:col   pre   main   sign   click   har   connect   include   form   

原文地址:https://www.cnblogs.com/dianrain/p/11110698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!