Qt Forum

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

    [SOLVED] mainwindow and dialog load up as separate programs

    General and Desktop
    3
    9
    3198
    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.
    • K
      kalster last edited by

      I have a dialog that displays under the mainwindow as soon as the program loads. The program loads up as if there is two programs. If i close the mainwindow then the dialog is still displayed.

      I would like my program to act as if there is only one program loaded. closing the mainwindow would then close the dialog. how to achieve this effect? I have setmodal to false and still the program acts as if there is two programs.

      1 Reply Last reply Reply Quote 0
      • M
        mlong last edited by

        You could make the dialog a child of the mainwindow.

        Alternately, in the closeEvent of the MainWindow you could emit a signal which could be connected to the dialog's close() or hide() slot.

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

          changing the dialog to child or parent has no effect as in the code below.

          @Dialog::Dialog(QWidget *Child)@

          1 Reply Last reply Reply Quote 0
          • M
            mlong last edited by

            [quote author="kalster" date="1312954651"]changing the dialog to child or parent has no effect as in the code below.

            @Dialog::Dialog(QWidget *Child)@[/quote]

            I assume you're meaning Parent (instead of Child) in your constructor.

            Do you have a small code sample you could share showing the construction of the two windows?

            Software Engineer
            My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

              dialog.h
              @#ifndef DIALOG_H
              #define DIALOG_H

              #include <QDialog>

              namespace Ui {
              class Dialog;
              }

              class Dialog : public QDialog
              {
              Q_OBJECT

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

              private:
              Ui::Dialog *ui;
              };

              #endif // DIALOG_H@

              mainwindow.h
              @#ifndef MAINWINDOW_H
              #define MAINWINDOW_H

              #include <QMainWindow>
              class Dialog;
              namespace Ui {
              class MainWindow;
              }

              class MainWindow : public QMainWindow
              {
              Q_OBJECT

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

              private:
              Ui::MainWindow *ui;
              Dialog *tt;
              };

              #endif // MAINWINDOW_H@

              dialog.cpp
              @#include "dialog.h"
              #include "ui_dialog.h"

              Dialog::Dialog(QWidget *child) :
              QDialog(child),
              ui(new Ui::Dialog)
              {
              ui->setupUi(this);
              }

              Dialog::~Dialog()
              {
              delete ui;
              }@

              mainwindow.cpp
              @#include "mainwindow.h"
              #include "ui_mainwindow.h"
              #include "dialog.h"
              MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
              {
              ui->setupUi(this);
              Dialog *tt = new Dialog;
              tt->show();
              }

              MainWindow::~MainWindow()
              {
              delete ui;
              }@

              1 Reply Last reply Reply Quote 0
              • M
                mlong last edited by

                In line 9 of mainwindow.cpp, try
                @
                Dialog *tt = new Dialog(this);
                @

                That will set the Dialog's parent to MainWindow, which will help with the stacking of the dialog and the mainwindow, and will also cause the MainWindow destructor to destroy the Dialog.

                Edit: Also in lines 4 and 5 of dialog.cpp, the variable name child is misleading, because it's actually taking a pointer to the Parent object. (Changing the variable name from parent to child doesn't actually alter the parent/child relationship of the two widgets.)

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

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

                  thank you mlong, it worked.
                  actually I believe i have a similar topic a while ago at this forum. but since then I forgot how to set the dialogs parent to mainwindow. :)

                  1 Reply Last reply Reply Quote 0
                  • M
                    mlong last edited by

                    Glad it's working! Doesn't hurt to relearn things :-)

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply Reply Quote 0
                    • D
                      dbzhang800 last edited by

                      please,

                      remove line 20 in mainwindow.h

                      or

                      change line 9 in mainwindow.cpp to

                      @
                      tt = new Dialog(this);
                      @

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