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. [SOLVED] mainwindow and dialog load up as separate programs
QtWS25 Last Chance

[SOLVED] mainwindow and dialog load up as separate programs

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.7k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

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

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

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

        @Dialog::Dialog(QWidget *Child)@

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          [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
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #5

            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
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

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

                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
                0
                • M Offline
                  M Offline
                  mlong
                  wrote on last edited by
                  #8

                  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
                  0
                  • D Offline
                    D Offline
                    dbzhang800
                    wrote on last edited by
                    #9

                    please,

                    remove line 20 in mainwindow.h

                    or

                    change line 9 in mainwindow.cpp to

                    @
                    tt = new Dialog(this);
                    @

                    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