自定义信号与槽
Posted dianrain
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义信号与槽相关的知识,希望对你有一定的参考价值。
#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();
以上是关于自定义信号与槽的主要内容,如果未能解决你的问题,请参考以下文章