Q:

failure to initialize multiple windows using qt

I've been struggling with the problem of initializing of multiple forms in Qt under Windows (the version is 5.0.1 but I don't reckon this to matter much). Long story short, my application contains of 2 forms the one of which is supposed to be called on clicking the button on the another one.
The code runs as follows:
1) main.cpp:  
#include "main.h"
#include <QApplication>
FmNewWord fmNewWord;
MainWindow w;
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    w.show();

    return a.exec();
}

2) main.h:
#ifndef MAIN_H
#define MAIN_H
#include "mainwindow.h"
#include "fmnewword.h"
extern FmNewWord fmNewWord;
extern MainWindow w;
#endif // MAIN_H

3) mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "ui_mainwindow.h"

class MainWindow : public QMainWindow, private Ui::MainWindow
    {
    Q_OBJECT
signals:
    void sgnl_new_word();
public:
    explicit MainWindow(QWidget *parent = 0);

protected:
    void changeEvent(QEvent *e);
private slots:
    void on_commandLinkButton_clicked();
    void on_miExit_triggered();
    void on_miNewWord_triggered();
    };

#endif // MAINWINDOW_H

4) mainwindow.cpp:
#include "main.h"
#include <Windows.h>
extern FmNewWord fmNewWord;
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this);
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::on_commandLinkButton_clicked()
{
    MessageBox(0, 0, 0, 0);
}

void MainWindow::on_miExit_triggered()
{
    this->close();
}

void MainWindow::on_miNewWord_triggered()
{
    fmNewWord.show();
}

5) fmnewword.cpp:
#include "fmnewword.h"

FmNewWord::FmNewWord(QWidget *parent) :
    QMainWindow(parent)
{
    setupUi(this); /// here the app crashes with parent == NULL
}
void FmNewWord::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        retranslateUi(this);
        break;
    default:
        break;
    }
}

6) fmnewword.h:
#ifndef FMNEWWORD_H
#define FMNEWWORD_H

#include "ui_fmnewword.h"

class FmNewWord : public QMainWindow, private Ui::FmNewWord
    {
    Q_OBJECT

public:
    explicit FmNewWord(QWidget *parent = 0);

protected:
    void changeEvent(QEvent *e);
    };
#endif // FMNEWWORD_H

The code keeps crashing during inititialization in fmnewword.cpp on the line shown. Could anybody point out the reason? Thanks.

A:

First of all, QApplication must be created before any widgets.
Your code should look llike this:
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

And that's how the second form must be created:
class FmNewWord;

class MainWindow : public QMainWindow, private Ui::MainWindow
{
    Q_OBJECT
    ...
private slots:
    void on_miNewWord_triggered();

private:
    FmNewWord *fmNewWord;
};

void MainWindow::on_miNewWord_triggered()
{
    fmNewWord = new FmNewWord;
    fmNewWord->show();
}

