Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] App crashes when closing; some objects not closed? MWE
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] App crashes when closing; some objects not closed? MWE

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 754 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    newe12
    wrote on last edited by newe12
    #1

    Hi!

    My app compiles and works fine, but crashes when i close it.

    I have read about this issue in the forum. Some people had solved this problem after they have removed all objects that they allocated on the heap which already had a QObject parent, but they had set them additionally to: "delete somewidget;" before.

    I just have:

    MainWindow::~MainWindow()
    {
        delete mainWindow;
    }
    

    to delete the mainWindow.

    So I guess, I am missing some objects which I should close too? Or is it another issue?

    I have produced a minimal working example:
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    
    class QTabWidget;
    class QWidget;
    class QLabel;
    class QGroupBox;
    class QLineEdit;
    class QPushButton;
    class QRadioButton;
    class QHBoxLayout;
    class QFormLayout;
    class QDoubleValidator;
    class QPalette;
    
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void startfile();
        void tabcalc();
    
    private:
        void setCurrentIndex();
    
        QMainWindow *mainWindow;
        QTabWidget *tabWidget;
    };
    
    
    
    
    
    class WelcomeTab : public QWidget
    {
        Q_OBJECT
    public:
        WelcomeTab(QWidget *parent = 0);
    };
    
    
    
    class TabCalc : public QWidget
    {
        Q_OBJECT
    public:
        TabCalc(QWidget *parent = 0);
    
    private slots:
        void calculate();
    
    private:
        QGroupBox *method();
        QGroupBox *input();
        QGroupBox *output();
        QGroupBox *calc();
        QRadioButton *radio1;
        QRadioButton *radio2;
        QLineEdit *inputnumber;
        QLineEdit *output;
    };
    
    #endif // MAINWINDOW_H
    
    

    main.cpp

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

    mainwindow.cpp

    #include "mainwindow.h"
    #include <QtGui>
    #include <QObject>
    #include <cmath>
    #include <QPalette>
    #include <QLabel>
    #include <QHBoxLayout>
    #include <QGroupBox>
    #include <QRadioButton>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QFormLayout>
    #include <QDoubleValidator>
    #include <QPalette>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        tabWidget = new QTabWidget;
        tabWidget->addTab(new WelcomeTab(), tr("Welcome"));
        tabWidget->addTab(new TabCalc(), tr("Calculate"));
        setCentralWidget(tabWidget);
    
        setWindowTitle(tr("Calculate A or B"));
        setMinimumSize(100, 100);
    }
    
    
    WelcomeTab::WelcomeTab(QWidget *parent)
        : QWidget(parent)
    {
        QLabel *welcomeLabel = new QLabel
                    (tr("Welcome"));
    
        QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->addWidget(welcomeLabel);
        setLayout(mainLayout);
    }
    
    void MainWindow::startfile()
    {
        tabWidget->setCurrentIndex(0);
    }
    
    
    TabCalc::TabCalc(QWidget *parent)
        : QWidget(parent)
    {
        QGridLayout *grid = new QGridLayout;
        grid->addWidget(method(), 0, 0);
        grid->addWidget(input(), 0, 1);
        grid->addWidget(output(), 0, 2);
        grid->addWidget(calc(), 0, 3);
        setLayout(grid);
    }
    
    QGroupBox *TabCalc::method()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Method"));
        radio1 = new QRadioButton(tr("Choice A"));
        radio2 = new QRadioButton(tr("Choice B"));
    
        QVBoxLayout *vbox = new QVBoxLayout;
         vbox->addWidget(radio1);
         vbox->addWidget(radio2);
         vbox->addStretch(1);
         groupBox->setLayout(vbox);
         return groupBox;
    }
    
    QGroupBox *TabCalc::input()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Input"));
        inputnumber = new QLineEdit;
    
        QFormLayout *formLayout = new QFormLayout;
          formLayout->addRow(tr("Insert a number [-10;10]"), inputnumber);
          groupBox->setLayout(formLayout);
    
          QValidator *validator = new QDoubleValidator(-10, 10, 10, this);
          inputnumber->setValidator(validator);
          return groupBox;
    }
    
    QGroupBox *TabCalc::output()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Output"));
        QLabel *label = new QLabel(tr("A or B"));
        output = new QLineEdit;
    
        QFormLayout *formLayout = new QFormLayout;
          formLayout->addRow(label);
          formLayout->addRow(tr("Result:"), output);
          groupBox->setLayout(formLayout);
          return groupBox;
    }
    
    QGroupBox *TabCalc::calc()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Calculate"));
        QPushButton *pushButton = new QPushButton(tr("Calculate"));
    
        QVBoxLayout *vbox = new QVBoxLayout;
         vbox->addWidget(pushButton);
         vbox->addStretch(1);
         groupBox->setLayout(vbox);
         connect(pushButton, SIGNAL(clicked()),this, SLOT(calculate()));
         return groupBox;
    }
    
    void TabCalc::calculate()
    {
        double result;
        bool ok;
        double A=10;
        double B=20;
        double insertedNumber = inputnumber->text().toDouble(&ok);
    
    if(radio1->isChecked())
        {
           result = insertedNumber+A;
           QString resultString = "";
           output->setText(resultString.setNum(result));
        }
        else if(radio2->isChecked())
        {
           result = insertedNumber+B;
           QString resultString = "";
           output->setText(resultString.setNum(result));
        }
        else
        {
           result = 0;
           output->setText("Choose A or B");
        }
    
    }
    
    
    void MainWindow::tabcalc()
    {
       tabWidget->setCurrentIndex(1);
    }
    
    MainWindow::~MainWindow()
    {
        delete mainWindow;
    }    
    

    Please let me know, if you see the problem!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAICS, you don't use nor initialize that mainWindow variable so technically you are trying to delete something that doesn't exist

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • N Offline
        N Offline
        newe12
        wrote on last edited by
        #3

        @SGaist, he man, that's great!

        Now it works!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved