lundi 20 avril 2015

How to make parent window accept focus/close only if child window is closed

I want the parent window to accept focus or be able to be closed only if the child window that is opened is closed. The code;

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QApplication>
#include <QMainWindow>
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>

#include "childwindow.h"

class MainWindow : public QWidget {
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void openChildWindow();

private:

    QPushButton *button;

    ChildWindow *childWindow;

};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {

    button = new QPushButton("Go");

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(button);

    connect(button, SIGNAL(clicked()), this, SLOT(openChildWindow()));

    setLayout(layout);
}

void MainWindow::openChildWindow() {
    childWindow = new ChildWindow;
    childWindow->show();
}

MainWindow::~MainWindow() {

}

childwindow.h

#ifndef CHILDWINDOW_H
#define CHILDWINDOW_H

#include <QDialog>

class ChildWindow : public QDialog {
    Q_OBJECT
public:
    explicit ChildWindow();
    ~ChildWindow();
};

#endif // CHILDWINDOW_H

childwindow.cpp

#include "childwindow.h"

ChildWindow::ChildWindow() {

}

ChildWindow::~ChildWindow() {

}

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);

    MainWindow *window = new MainWindow;
    window->showMaximized();

    return a.exec();
}

How do i go about it?

Aucun commentaire:

Enregistrer un commentaire