Problem with Mdiarea Subwindow



  • I have these files:
    main.cpp mainwindow.cpp .h .ui dialog.cpp .h. ui
    main.cpp
    @#include "mainwindow.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include "dialog.h"

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    Dialog *dialog;

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_2_clicked();
    

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @
    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    if (!dialog)
    {
    dialog = new Dialog(this);
    ui->mdiArea->addSubWindow(dialog,Qt::Dialog);
    }
    dialog->show();
    dialog->adjustSize();
    }

    void MainWindow::on_pushButton_2_clicked()
    {
    }
    @

    The problem is when i click the pushButton, the dialog appear in Mdiarea. Then i close the dialog and when i click again pushButton aplication crash.


  • Lifetime Qt Champion

    Hi,

    You're lucky it doesn't crash directly, you don't initialize dialog to 0 in your constructor.



  • So how can i solve the problem?


  • Lifetime Qt Champion

    Start by initializing dialog to 0 in the constructor



  • i am noob. can you please show me the code, because i dont know how to do it


  • Lifetime Qt Champion

    You don't know how to initialize a member variable in C++ ?

    Then you should rather grab a good C++ book and read it before continuing.



  • i know how to initialize the variable, but sometimes my english isn't good enough to understand what am i reading


  • Lifetime Qt Champion

    So it's alright now ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.