Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt:更改centralWidget时崩溃_C++_Qt_Class - Fatal编程技术网

C++ Qt:更改centralWidget时崩溃

C++ Qt:更改centralWidget时崩溃,c++,qt,class,C++,Qt,Class,我创建了自己的类,这里是ewindow.h: #ifndef EWINDOW_H #define EWINDOW_H #include <QWidget> #include <QString> #include <mainwindow.h> class MainWindow; class EWindow { public: EWindow(void (*callback)(EWindow*, MainWindow*), MainWi

我创建了自己的类,这里是
ewindow.h

#ifndef EWINDOW_H
#define EWINDOW_H
#include <QWidget>
#include <QString>
#include <mainwindow.h>

class MainWindow;

class EWindow
{
    public:
        EWindow(void (*callback)(EWindow*, MainWindow*), MainWindow *window, QString name, QString title);
        QWidget *widget;
        void resize(int width, int height);
        void move(int x, int y);
        void move();
        void apply();
        void append(QWidget *newWidget);
        int* getSize();
        ~EWindow();

    private:
        int width, height, x, y;
        QString name, title;
        MainWindow *window;
};

#endif // EWINDOW_H
在回调中,我创建了一些小部件,如
QLabel
QLineEdit
。 这是我的
apply
功能:

void EWindow::apply() {
    window->setCentralWidget(this->widget);
    window->setWindowTitle(this->title);
    window->setFixedWidth(this->width);
    window->setFixedHeight(this->height);
    if (this->x == -1 || this->y == -1) this->move();
    window->move(this->x, this->y);
}

但是!当我尝试为不同的EWindows调用apply函数2次时,我的程序崩溃了,没有出现任何错误。我认为这行有错误:
window->setCentralWidget(这个->小部件)。请帮忙,谢谢。

没问题了。我忘了Qt在应用新组件时会删除以前的
QWidget
。我是这样做的:


在创建
QWidget
的新实例后,将不会在构造函数中调用回调,只会在函数
apply()
中调用回调。现在它工作得很好。谢谢,
drescherjm

使用调试器精确定位崩溃的确切位置(为什么不同时解决它:))谢谢您的建议,但不幸的是,我不知道如何使用调试器:(我认为这是一个已知的问题。我不知道如何利用调试器时间来学习。这是作为程序员需要学习的最重要的技能之一。我认为这是一个已知的问题。这里是一个猜测。请注意双重删除。请记住,当父对象被释放时,Qt会自动删除子对象。
void EWindow::apply() {
    window->setCentralWidget(this->widget);
    window->setWindowTitle(this->title);
    window->setFixedWidth(this->width);
    window->setFixedHeight(this->height);
    if (this->x == -1 || this->y == -1) this->move();
    window->move(this->x, this->y);
}