#include <QtGui>class TChildWindow:public QMainWindow{public: TChildWindow(void); ~TChildWindow(void);private: void virtual mousePressEvent(QMouseEvent *event); };//-------------------------------------------------------#include "TChildWindow.h"TChildWindow::TChildWindow(void){ setStyleSheet("background-color: gray"); }TChildWindow::~TChildWindow(void){}void TChildWindow::mousePressEvent(QMouseEvent *event){ this->close(); };//----------------------------------------#include <QtGui>#include "TChildWindow.h"class TMainWindow:public QMainWindow{Q_OBJECTpublic: TMainWindow(void); ~TMainWindow(void);private: TChildWindow *childwindow; QWidget *mainWidget; QGridLayout *layout; QPushButton *button;public slots: void slotPushButton(void);};//---------------------------#include "TMainWindow.h"TMainWindow::TMainWindow(void){ mainWidget=new QWidget(); layout=new QGridLayout(); mainWidget->setLayout(layout); this->setCentralWidget(mainWidget); button=new QPushButton("OK"); layout->addWidget(button); childwindow=new TChildWindow(); connect(button,SIGNAL(clicked()),this,SLOT(slotPushButton()));}TMainWindow::~TMainWindow(void){}void TMainWindow::slotPushButton(void){ childwindow->setParent(this); childwindow->setFixedWidth(this->width()/2); childwindow->setFixedHeight(this->height()/2); childwindow->move(20,20); childwindow->setWindowModality(Qt::WindowModal); childwindow->show();};//---------------------------------------int main (int argc, char* argv[] ){ QApplication app(argc, argv); TMainWindow *window=new TMainWindow(); window->show(); return app.exec(); delete window;}
...return app.exec();delete window;...
C++ (Qt)TChildWindow::TChildWindow(QWidget *parent): QMainWindow(parent){ setStyleSheet("background-color: gray"); }...childwindow=new TChildWindow(this);...