Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Run time error in calling one window from other

    General and Desktop
    4
    5
    2221
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aurora last edited by

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

      @

      1 Reply Last reply Reply Quote 0
      • K
        kussg last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • A
          andre last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • B
            broadpeak last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • A
              aurora last edited by

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

              1 Reply Last reply Reply Quote 0
              • First post
                Last post