【发布时间】:2020-05-18 12:57:04
【问题描述】:
我从 QDialog 派生了用于显示图表的类。构造函数如下所示:
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);
}
我从 Mainwindow.cpp 调用我的课程,但 App 退出后对话框没有关闭:
myplot* plot = new myplot(); //does not close after app exit
plot->do_something();
plot->show();
我以为我会通过这个来纠正问题,但它不起作用:
myplot* plot = new myplot(this); //does not work
当我使用它时,对话框会立即关闭
myplot plot; //immediatelly close
plot.do_something();
plot.show();
当我使用 exec 而不是 .show() 时,对话框关闭后出现错误 "Debug Assertion Failed, _CtrlIsValidHeapPointer(block)"
plot.exec();
//work but after exiting dialog error
请问,如何正确处理我的派生类在 App 退出后关闭?我还希望 myplot 类不是模态的(现在我将它设为模态,以便用户在应用退出之前手动关闭它)。
添加头文件:
#ifndef MYPLOT_H
#define MYPLOT_H
class myplot : public QDialog
{
Q_OBJECT
private:
public:
explicit myplot(QDialog *parent = nullptr);
signals:
};
#endif // MYPLOT_H
【问题讨论】:
-
请显示对话框的标题......
-
我添加了头文件(为了更好的可读性,我删除了一些与我认为与问题无关的变量和方法)。