#include <QtGui/QApplication>#include "widget.h"#include "widget2.h"int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; Widget2 w2; w.show(); w2.show(); QObject::connect(w.pb,SIGNAL(clicked()),&w2,SLOT(close())); QObject::connect(w2.pb1,SIGNAL(clicked()),&w,SLOT(close())); return a.exec();}
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); QHBoxLayout* hbl = new QHBoxLayout(this); pb = new QPushButton("PushButton1"); hbl->addWidget(pb); setLayout(hbl); setFixedSize(150,100);}Widget::~Widget(){ delete ui;}
#include "widget2.h"Widget2::Widget2(QWidget *parent) : QWidget(parent){ QHBoxLayout* hbl = new QHBoxLayout(this); pb1 = new QPushButton("PushButton2"); hbl->addWidget(pb1); setLayout(hbl); setFixedSize(150,100);}Widget2::~Widget2(){}
#include "widget.h"#include "widget2.h"#include <QApplication>int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget *w = new Widget; Widget2 *w2 = new Widget2; w->show(); return a.exec();}
#include "widget2.h"#include "ui_widget.h"Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this);}Widget::~Widget(){ delete ui;}void Widget::on_pushButton_clicked(){ w2->show();}
void MainWindow::on_pushButton_clicked(){ Widget2 *w2 = new Widget2; w2->show();}
C++ (Qt)void Widget::on_pushButton_clicked(){ emit showW2();}
Widget *w = new Widget;Widget2 *w2 = new Widget2;QObject::connect(&w, SIGNAL(showW2()),&w2, SLOT(show()));
C++ (Qt)QObject::connect(w, SIGNAL(showW2()), w2, SLOT(show()));