Run time error in calling one window from other



  • hi i created two windows window_a and window_b
    when i click on the button of window_a it should display window_b, but something flashes and goes....window_a remains like that
    please help me to solve this issue...
    program as shown below

    window_a.h
    @#ifndef WINDOW_A_H
    #define WINDOW_A_H
    #include "window_b.h"
    #include <QMainWindow>

    namespace Ui {
    class window_a;
    }

    class window_a : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit window_a(QWidget *parent = 0);
    ~window_a();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::window_a *ui;

    };
    @

    window_b.h
    @
    #ifndef WINDOW_B_H
    #define WINDOW_B_H

    #include <QMainWindow>
     
    namespace Ui {
    class window_b;
    }
     
    class window_b : public QMainWindow
    {
    Q_OBJECT
     
    public:
    explicit window_b(QWidget *parent = 0);
    ~window_b();
     
    private:
    Ui::window_b *ui;
    };
     
    #endif // WINDOW_B_H
    

    @

    window_a.cpp
    @
    #include "window_a.h"
    #include "ui_window_a.h"
    #include "window_b.h"
    window_a::window_a(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::window_a)
    {

    ui->setupUi(this);
    }
     
    window_a::~window_a()
    {
    delete ui;
    }
     
    void window_a::on_pushButton_clicked()
    {
    window_b  mywindow_b;
    mywindow_b.show();
    }
    

    @

    window_b.cpp
    @ #include "window_b.h"
    #include "ui_window_b.h"

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

    @

    main.cpp
    @
    #include <QtGui/QApplication>
    #include "window_a.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    window_a w;
    w.show();
     
    return a.exec&#40;&#41;;
    }
    

    @



  • on the line 19, in window_a.cpp,

    window_b mywindow_b;
    mywindow_b.show();

    these are temporary object, which is gone when the on_pushButton_clicked function is completely executed..

    create a dynamic object would help to solve the problem.



  • By "dynamic object", I guess kus77 means "an object on the heap", that is, you create the object with new and then manipulate the pointer you get to it. Alternatively, you might want to make window_b inherit QDialog instead of QWidget, and then use QDialog::exec() instead of QWidget::show(). That results in window_b becomming a modal dialog. Because the exec() call blocks, you don't need to use the heap.



  • And if you declare and define "parent", use it:
    @
    window_b w_b = new mywindow_b(this); // "this" is a ponter to the parent
    @
    The actual (child) object will be deleted by the owner (parent), at the end of the scope.



  • Thanks all....i got it..:)


Log in to reply
 

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