Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7195

How to prevent crash after close QMainWindow in Qt?

$
0
0

I have an error, just after closing my application, I get an error message saying that it has stopped unexpectedly. I reached this:

enter image description here

But, after I close the window, I get this error:

The program has unexpectedly finished. The process was ended forcefully.

My project structure is:

enter image description here

And my code is:

main.cpp

#include "mainwindow.h"#include <QApplication>#include <windows/login.h>int main(int argc, char *argv[]){    QApplication a(argc, argv);    Login w;    w.show();    return a.exec();}

util_window.h

#ifndef UTIL_H#define UTIL_H#include <QObject>#include <QMainWindow>class UtilWindow : QObject{public:    // vars    // methods    util();    void setCenterWindow(QMainWindow* w);};#endif // UTIL_H

util_window.cpp

#include "util_window.h"#include <QDesktopWidget>UtilWindow::util(){}void UtilWindow::setCenterWindow(QMainWindow *w) {    int width = w->frameGeometry().width();    int height = w->frameGeometry().height();    QDesktopWidget wid;    int screenWidth = wid.screen()->width();    int screenHeight = wid.screen()->height();    w->setGeometry((screenWidth/2)-(width/2),(screenHeight/2)-(height/2),width,height);    w->show();}

login.h

#ifndef LOGIN_H#define LOGIN_H#include <QMainWindow>#include <QObject>#include <QWidget>#include "util/util_window.h"#include <QGroupBox>#include <QLabel>#include <QLineEdit>#include <QFormLayout>class Login : public QMainWindow{    Q_OBJECTpublic:    // vars    UtilWindow* util;    // widgets    // labels    QLabel* lblHost;    QLabel* lblPort;    QLabel* lblUser;    QLabel* lblPass;    // textbox    QLineEdit* leHost;    QLineEdit* lePass;    QLineEdit* leUser;    QLineEdit* lePort;    // layouts    QFormLayout* layout;    QWidget* central;    QVBoxLayout* mainLayout;    QGroupBox* gbCredentials;    // methods    void initLabels();    void initTextBoxes();    void initUI();    explicit Login(QWidget *parent = nullptr);    ~Login();signals:public slots:};#endif // LOGIN_H

login.cpp

#include "login.h"Login::Login(QWidget *parent) : QMainWindow(parent){    this->util = new UtilWindow();    this->initUI();}Login::~Login() {    delete this->lblHost;    delete this->lblPass;    delete this->lblPort;    delete this->lblUser;    delete this->leHost;    delete this->lePass;    delete this->lePort;    delete this->leUser;    delete this->layout;    delete this->central;    delete this->mainLayout;    delete this->gbCredentials;    delete this->util;}void Login::initUI() {    this->setFixedSize(400,400);    this->setWindowTitle(tr("Inicio de sesión"));    this->util->setCenterWindow(this);    this->initLabels();    this->initTextBoxes();    this->layout = new QFormLayout();    this->layout->addRow(this->lblHost, this->leHost);    this->layout->addRow(this->lblPort, this->lePort);    this->layout->addRow(this->lblUser, this->leUser);    this->layout->addRow(this->lblPass, this->lePass);    this->gbCredentials = new QGroupBox();    this->gbCredentials->setTitle(tr("Datos de conexión"));    this->gbCredentials->setLayout(layout);    this->mainLayout = new QVBoxLayout();    this->mainLayout->addWidget(gbCredentials);    this->central = new QWidget();    this->central->setParent(this);    this->central->setLayout(this->mainLayout);    this->setCentralWidget(this->central);}void Login::initLabels() {    this->lblHost = new QLabel();    this->lblPass = new QLabel();    this->lblPort = new QLabel();    this->lblUser = new QLabel();    this->lblHost->setText(tr("Host: "));    this->lblPass->setText(tr("Contraseña: "));    this->lblPort->setText(tr("Puerto: "));    this->lblUser->setText(tr("Usuario: "));}void Login::initTextBoxes() {    this->leHost = new QLineEdit();    this->lePass = new QLineEdit();    this->lePort = new QLineEdit();    this->leUser = new QLineEdit();    this->leHost->setPlaceholderText(tr("Host de MySQL"));    this->lePass->setPlaceholderText(tr("Ingrese su contraseña"));    this->leUser->setPlaceholderText(tr("Ingrese su nombre de usuario"));    this->lePort->setPlaceholderText(tr("Ingrese el número de puerto"));    this->leHost->setToolTip(this->leHost->placeholderText());    this->leUser->setToolTip(this->leUser->placeholderText());    this->lePass->setToolTip(this->lePass->placeholderText());    this->lePort->setToolTip(this->lePort->placeholderText());}

Thanks in advance!


Viewing all articles
Browse latest Browse all 7195

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>