I want to implement a dialog that is automatically executed at the beginning of a program if the data of a connection to a DB is not correct. Once the connection data has been configured, and if they are correct, an Accept button will be activated and the application will open, or Cancel will be pressed. If it is canceled when entered in this way (automatically), the application must be terminated.
This dialog can be called later at any time to modify connection data. In this case, if Cancel is pressed, the dialog should only be closed and the application will continue.
Once the idea is exposed, I show an example that will try to imitate the behavior that I want to achieve. In this case it is a widget that whenever it is started it will call the presumed configuration dialog. If I press the OK button, it goes back to "main". But if I press Cancel when it opens automatically (at the beginning), it should exit the application, but if I do it when the dialog has been opened by pressing the button in the main window, it should just close the dialog and return to the main window .
I have tried to play (it is seen in the example) with the value of parent()
. For this I have tried that when the dialog opens automatically, it should be without a parent in the constructor, and when it is done through the button, it should have the main widget as a parent, but it has not worked for me.
I leave the code:
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
wdget.h (the main window)
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPushButton;
class QHBoxLayout;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QHBoxLayout* layout;
QPushButton* boton_Configurar;
public slots:
void Configurar();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "dialogoconfigurar.h"
#include <QHBoxLayout>
#include <QPushButton>
Widget::Widget(QWidget *parent):QWidget(parent)
{
layout = new QHBoxLayout(this);
boton_Configurar = new QPushButton("Configurar");
layout->addWidget(boton_Configurar);
bool res = false;
if (!res)
{
DialogoConfigurar* d = new DialogoConfigurar;
d->exec();
}
QObject::connect(boton_Configurar, &QPushButton::clicked, [=](){Configurar();});
}
Widget::~Widget()
{
}
void Widget::Configurar()
{
DialogoConfigurar* d = new DialogoConfigurar(this);
d->exec();
}
configuredialog.h
#ifndef DIALOGOCONFIGURAR_H
#define DIALOGOCONFIGURAR_H
#include <QDialog>
class QPushButton;
class QHBoxLayout;
class DialogoConfigurar : public QDialog
{
Q_OBJECT
public:
explicit DialogoConfigurar(QWidget *parent = nullptr);
~DialogoConfigurar();
public slots:
void Salir();
private:
QHBoxLayout* layout;
QPushButton* boton_Aceptar;
QPushButton* boton_Salir;
};
#endif // DIALOGOCONFIGURAR_H
configuredialog.cpp
#include "dialogoconfigurar.h"
#include <QHBoxLayout>
#include <QPushButton>
#include <QApplication>
#include <QDebug>
DialogoConfigurar::DialogoConfigurar(QWidget *parent) : QDialog(parent)
{
layout = new QHBoxLayout(this);
boton_Aceptar = new QPushButton("Aceptar");
boton_Salir = new QPushButton("Salir");
layout->addWidget(boton_Aceptar);
layout->addWidget(boton_Salir);
QObject::connect(boton_Aceptar, &QPushButton::clicked, this, &DialogoConfigurar::done);
QObject::connect(boton_Salir, &QPushButton::clicked, this, &DialogoConfigurar::Salir);
}
DialogoConfigurar::~DialogoConfigurar()
{
}
void DialogoConfigurar::Salir()
{
qDebug()<<parent();
if (!parent())
{
QApplication::exit();//no funciona
}
else
{
this->done(0);
}
}
(In the slot void Salir()
is where I have tried to implement the logic of the button)
You can't call
QApplication::exit()
from the functionSalir
If you check the
main
:a.exec()
is the function that initializes the event loop. Key piece of any graphic application. Without that function, your application remains dumb , because that function is the one that processes all the events and signals that come from Windows.If you need to do those checks at startup, and those checks don't have to make use of any objects that require the use of Qt signals and slots, you can just leave it in the
main
:I have made another proposal that seems to work:
configuredialog.h
configuredialog.cpp
mainwindow.h
mainwindow.cpp
main.cpp