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);
}