[SOLVED]Problem with the QMdiSubWindow



  • Good day everyone!
    I'm stuck with one problem! Please help me figure out what's wrong.
    That is my code:
    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include"sqlite_source/sqlitedb.h"

    class FormObject;
    class FormFlat;
    class dlgNewObject;
    class dlgnewflat;
    class QMdiSubWindow;

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void setActiveDb(DBBrowserDB *pdb);

    private slots:
    void on_actionObject_triggered();
    void formObject_open();
    void formObject_close();
    void on_actionNew_triggered();
    void on_actionModify_triggered();
    void setRecordId(const int recId);
    void on_actionDelete_triggered();
    void on_actionFlat_triggered();
    void subWindowChanged(QMdiSubWindow *window);
    public:
    Ui::MainWindow *ui;
    DBBrowserDB *mainDb;
    private:
    enum FORM_TYPE{NONE, OBJECT_FORM, FLAT_FORM, AGENT_FORM};
    FORM_TYPE currentForm;
    void init();
    FormObject *formObject; //derived from QWidget
    FormFlat *formFlat; //derived from QWidget
    dlgNewObject *dlgNewO;
    dlgnewflat *dlgNewFlat;
    int recordId;
    };

    #endif // MAINWINDOW_H
    @
    mainwindow.cpp
    @
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    init();
    }

    void MainWindow::init()
    {
    connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),
    this, SLOT(subWindowChanged(QMdiSubWindow*)));

    ui->mdiArea->setViewMode(QMdiArea::TabbedView);
    
    this->setCentralWidget(ui->mdiArea);
    

    }

    void MainWindow::on_actionObject_triggered()
    {
    formObject = new FormObject(mainDb, this);
    QMdiSubWindow *subWinObjects = ui->mdiArea->addSubWindow(formObject, Qt::Window);
    subWinObjects->setName("OBJECT_FORM");
    subWinObjects->setAttribute(Qt::WA_DeleteOnClose);

    connect(formObject, SIGNAL(formActivated()), this, SLOT(formObject_open()));
    connect(formObject, SIGNAL(destroyed()), this, SLOT(formObject_close()));
    connect(formObject, SIGNAL(currentItemChanged(int)), this, SLOT(setRecordId(int)));
    
    subWinObjects->show();
    

    }

    void MainWindow::on_actionFlat_triggered()
    {
    formFlat = new FormFlat(mainDb, this);
    QMdiSubWindow *subWinFlats = ui->mdiArea->addSubWindow(formFlat, Qt::Window);
    subWinFlats->setName("FLAT_FORM");
    subWinFlats->setAttribute(Qt::WA_DeleteOnClose);
    connect(formFlat, SIGNAL(formActivated()), this, SLOT(formObject_open()));
    connect(formFlat, SIGNAL(destroyed()), this, SLOT(formObject_close()));
    connect(formFlat, SIGNAL(currentItemChanged(int)), this, SLOT(setRecordId(int)));

    subWinFlats->show();
    

    }
    @

    This program work with SQLite database: insert, update, delete.
    Everything worked fine until I added this code in the MainWindow::init() function
    @
    connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),
    this, SLOT(subWindowChanged(QMdiSubWindow*)));
    @
    Now, after closing last QMdiSubWindow the program crashes.

    Some additional information:
    I'm using Qt 4.7.4
    OS: Debian GNU/Linux 6.0.3 _Squeeze

    Thank you!



  • YES! YES! YES!
    That is not a question any more!
    My deadline is up coming, so I found another way, how figure out which QMdiSubWindow the user has selected.
    The answer is simple.
    I added this code in the on_actionObject_triggered
    @
    connect(subWinObjects, SIGNAL(aboutToActivate()), this, SLOT(formObject_open()));
    @
    and this one in the on_actionFlat_triggered
    @
    connect(subWinFlats, SIGNAL(aboutToActivate()), this, SLOT(formFlat_open()));
    @
    and that code I removed from init()
    @
    connect(ui->mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),
    this, SLOT(subWindowChanged(QMdiSubWindow*)));
    @

    But in the other hand, I'm not going to mark my discussion with [SOLVED] mark,
    maybe someone tell me why my previous code leads to crash or I will find that answer by myself.

    Thank you!

    :-)



  • It's likely that it crashes in method subWindowChanged - provide the code for that, our crystal balls are on holidays. But I'm pretty sure you don't check in your slot whether the QMdiSubwindow pointer provided via that signal is null - and thus work on a null pointer occasionally:

    bq. void QMdiArea::subWindowActivated ( QMdiSubWindow * window ) [signal]
    QMdiArea emits this signal after window has been activated. When window is 0, QMdiArea has just deactivated its last active window, and there are no active windows on the workspace.

    BTW: whether you have a deadline or not is quite irrelevant for a forum driven by volunteers :-)



  • Note that subWindowChanged is also emitted when the main window loses focus (for example, when you show and close the modal dialog). And in a case of lost focus its argument will be set to 0. You need to handle this case in your signal handler. I've spent some time with this case about a year and half ago. Debugger (i.e. GDB or something) surely could help you with this kind of problem ;)

    Good luck! :)



  • [quote author="Volker" date="1321808984"]
    BTW: whether you have a deadline or not is quite irrelevant for a forum driven by volunteers :-)[/quote]
    Volker, believe me, I've never thought that someone have to help me in a short period of time. Nonsense.
    I'm a goalie in the amateur hockey team. There was the game in the evening. That was my "Deadline" :-)
    And I'm really sorry that seemed to be some kinda pretension.

    Thank you very much, guys. Now I'll be smarter :-)



  • Ah, I see - good to know you're back and fine now :-)

    You might check the advise given earlier, and report back if that was the problem.


Log in to reply
 

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