[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.
-
[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_OBJECTpublic:
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_OBJECTpublic:
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;
}@ -
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.)
-
please,
remove line 20 in mainwindow.h
or
change line 9 in mainwindow.cpp to
@
tt = new Dialog(this);
@