qt 子視窗呼叫父視窗的函式
阿新 • • 發佈:2019-01-10
Father.h
#ifndef FATHER_H #define FATHER_H #include <QtGui/QMainWindow> #include "ui_Father.h" #include "Son.h" class Father : public QMainWindow { Q_OBJECT public: Father(QWidget *parent = 0, Qt::WFlags flags = 0); ~Father(); protected: Son *m_son; protected slots: void SonWindowEventClicked(); //子視窗呼叫的父視窗槽函式 void on_pushButton_Father_clicked();//開啟子視窗 private: Ui::FatherClass m_FatherUi; }; #endif // FATHER_H
Father.cpp
#include "stdafx.h" #include "Father.h" #include <QMessageBox> Father::Father(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags) { m_FatherUi.setupUi(this); } Father::~Father() { } void Father::SonWindowEventClicked() { QMessageBox::information(0, tr(""), tr(""));//檢測函式呼叫是否成功 } void Father::on_pushButton_Father_clicked() { m_son = new Son; m_son->show(); connect(m_son, SIGNAL(SonWindowEvent()),this,SLOT(SonWindowEventClicked()));//連線子視窗的訊號和父視窗的槽 }
Son.h
#ifndef SON_H #define SON_H #include <QWidget> #include "ui_Son.h" class Son : public QWidget { Q_OBJECT public: Son(QWidget *parent = 0); ~Son(); signals: void SonWindowEvent(); //子視窗的訊號 protected slots: void on_pushButton_Son_clicked();//子視窗呼叫父視窗的槽函式 private: Ui::Son m_SonUi; }; #endif // SON_H
Son.cpp
#include "StdAfx.h"
#include "Son.h"
Son::Son(QWidget *parent)
: QWidget(parent)
{
m_SonUi.setupUi(this);
}
Son::~Son()
{
}
void Son::on_pushButton_Son_clicked()
{
emit SonWindowEvent();
}