运行截图如下:
关键代码如下:
connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked())); connect(this,SIGNAL(signal_1(QString)),this,SIGNAL(signal_2(QString))); connect(this,SIGNAL(signal_2(QString)),this,SIGNAL(signal_3(QString))); connect(this,SIGNAL(signal_3(QString)),this,SLOT(slot_3(QString)));
下面给出所有的代码
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); public slots: void btnClicked(); void slot_3(QString msg); signals: void signal_1(QString msg1); void signal_2(QString msg2); void signal_3(QString msg3); private: Ui::Widget *ui; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QDebug> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); this->setWindowTitle(tr("CSDN IT1995")); connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(btnClicked())); connect(this,SIGNAL(signal_1(QString)),this,SIGNAL(signal_2(QString))); connect(this,SIGNAL(signal_2(QString)),this,SIGNAL(signal_3(QString))); connect(this,SIGNAL(signal_3(QString)),this,SLOT(slot_3(QString))); } void Widget::slot_3(QString msg){ ui->lineEdit->setText(msg); } void Widget::btnClicked(){ emit signal_1(tr("CSDN IT1995")); } Widget::~Widget() { delete ui; }
main.cpp
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }