Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Run time error in calling one window from other

Run time error in calling one window from other

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.5k Views
  • 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 Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    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
    0
    • K Offline
      K Offline
      kussg
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        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
        0
        • B Offline
          B Offline
          broadpeak
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            aurora
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved