[SOLVED] mainwindow and dialog load up as separate programs



  • 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.


  • Moderators

    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.



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

    @Dialog::Dialog(QWidget *Child)@


  • Moderators

    [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?



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


  • Moderators

    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.)



  • 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. :)


  • Moderators

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



  • please,

    remove line 20 in mainwindow.h

    or

    change line 9 in mainwindow.cpp to

    @
    tt = new Dialog(this);
    @


Log in to reply
 

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