A
A
Alexander Sharomet2014-12-24 23:36:11
Programming
Alexander Sharomet, 2014-12-24 23:36:11

QT Creator how to close main window from dialog?

Hello. Such a question:
By clicking on the button, I open a new window in which 2 buttons of the type are sure you want to exit or not. If you click yes, then the main window should close and naturally this is a dialog.
How to do it? Thank you!
#include "exitmodal.h"
#include "ui_exitmodal.h"

exitModal::exitModal(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::exitModal){
    ui->setupUi(this);
}

exitModal::~exitModal(){
    delete ui;
}

void exitModal::on_pushButton_clicked(){
    //тут что то нужно написать, только вот не знаю что. close(); закрывает только это окно а мне нужно чтоб   //закрывалось и основное
}

Call window
void MainWindow::on_exit_triggered(){
    exitmodal =new exitModal(this);
    exitmodal->show();

}

61ecd32d92644c6ea5a35aea5a1a6649.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
tzlom, 2014-12-25
@sharomet

Well, in general you can call parent()->close() since set your main window as the parent.
In general, it’s better not to write a bicycle, but to use QMessageBox

T
tsarevfs, 2014-12-25
@tsarevfs

Everything is a little easier :

#include <QApplication>
#include <QMessageBox>
#include <QDebug>

// ...

void MyWidget::someSlot() {
  QMessageBox::StandardButton reply;
  reply = QMessageBox::question(this, "Test", "Quit?",
                                QMessageBox::Yes|QMessageBox::No);
  if (reply == QMessageBox::Yes) {
    qDebug() << "Yes was clicked";
    QApplication::quit();
  } else {
    qDebug() << "Yes was *not* clicked";
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question