Qt:更改 centralWidget 时崩溃
Qt: crash when changing centralWidget
我创建了自己的类,这里是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
构造 函数:
EWindow::EWindow(void (*callback)(EWindow*, MainWindow*), MainWindow *window, QString name, QString title) {
this->width = 0;
this->height = 0;
this->x = -1;
this->y = -1;
this->name = name;
this->title = title;
this->window = window;
this->widget = new QWidget();
(*callback)(this, window);
}
在回调中,我创建了一些小部件,例如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 调用应用函数 2 次时,我的程序崩溃而没有任何错误。我认为这一行有错误:window->setCentralWidget(this->widget);
.请帮忙,谢谢。
没有问题了。我忘记了Qt在应用new时会删除以前的QWidget
。我是这样做的:
回调不会在构造函数中调用,只会在函数apply()
中调用,在创建QWidget
的新实例后。现在它工作得很好。谢谢,drescherjm
!
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 程序崩溃并显示"std::out_of_range"错误
- CoInitialize()在单独的线程上崩溃而不返回
- 使用调试/崩溃报告将应用程序部署到客户端
- 为什么所有C++编译器都会崩溃或挂起此代码
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么我的多线程作业队列崩溃
- ExtractIconEx:可以工作,但偶尔会崩溃
- 为什么引用传递会导致此崩溃(C++)
- 试图创建流或fopen时程序崩溃
- 类对象数组的问题会导致崩溃
- 排序时无法执行交换操作.我做的时候它会崩溃.为什么
- 为什么要增加导致崩溃的指针
- 在虚幻引擎中删除NXOpen对象时崩溃
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Visual Studio在尝试读取resource.txt文件时崩溃
- Qt:更改 centralWidget 时崩溃