vlad
vlad

Reputation: 343

QDialog derived class

I have derived class from QDialog that I use for showing the charts. The constructor looks like this:

myplot::myplot(QDialog *parent) : QDialog(parent)
{
    chartView = new QChartView();
    chart = new QChart();

    Qt::WindowFlags flags = 0;
    flags |= Qt::WindowMaximizeButtonHint;
    flags |= Qt::WindowCloseButtonHint;
    setWindowFlags(flags);

    this->setLayout(new QVBoxLayout);
    this->layout()->addWidget(chartView);

    this->setModal(1);
    chartView->setChart(chart);
}

I call my class from Mainwindow.cpp but dialog does not close after App exit:

myplot* plot = new myplot();        //does not close after app exit
plot->do_something();
plot->show();

I thought I will correct the problem by this but it does not work:

myplot* plot = new myplot(this);    //does not work

When I use this the dialog is closed immediatelly

myplot plot;                //immediatelly close
plot.do_something();
plot.show();

When I use exec instead of .show() I get the error "Debug Assertion Failed, _CtrlIsValidHeapPointer(block)" after dialog is closed

plot.exec();

            //work but after exiting dialog error

Please, how to handle correctly my derived class to be closed after App exit? I also want to have myplot class to be not modal (now I have it modal in order the user would close it manually before app exit).

Added header file:

#ifndef MYPLOT_H
#define MYPLOT_H

class myplot : public QDialog
{
    Q_OBJECT

private:
public:
    explicit myplot(QDialog *parent = nullptr);
signals:

};


#endif // MYPLOT_H

Upvotes: 1

Views: 582

Answers (2)

you need to look what the QDialog looks like...

do: pass a QWidget instead of a QDialog to the constructor, add a destructor to the dialog and delete there all the things created by the myplot instance if any, call exec() instead of show()...

class myplot : public QDialog
{
    Q_OBJECT
    public:
        explicit myplot(QWidget *parent = nullptr);
         ~myplot();
    .... 

Upvotes: 0

FMeinicke
FMeinicke

Reputation: 648

This is probably a duplicate of this question.

The answer is you have to write that functionality yourself. If you have your myplot object as a member variable of your MainWindow class then you can properly close it in the MainWindow::closeEvent. See the linked answer above for a hint on how you might implement this.

BTW: Then you don't have to make your dialog modal to enforce closing it before the MainWindow.

Upvotes: 1

Related Questions