Qt: crash when changing centralWidget

I created my own class, here 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

      

Constructor:

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

      

In the callback, I create some widgets like QLabel

or QLineEdit

. Here is my function 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);
}

      

But! When I try to call the application function 2 times for different EWindows, my program crashes without error. I think the error in this line: window->setCentralWidget(this->widget);

. Help please, thanks.

+3


source to share


1 answer


More problems. I forgot that Qt removes the previous one QWidget

when applying the new one. I did it like this:



The callback will not be called in the constructor, only in the function apply()

after a new instance is created QWidget

. It works fine now. Thank you drescherjm

!

+1


source







All Articles