如果父窗口关闭,如何使父窗口接受焦点/关闭

时间:2015-04-20 21:06:06

标签: c++ qt

我希望父窗口接受焦点,或者只有在打开的子窗口关闭时才能关闭。就像Qt Creator工具菜单中的选项操作在打开时的行为一样 代码;

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();
}

我该怎么做?

0 个答案:

没有答案