PDA

View Full Version : Child object not showing



BalaQT
24th September 2009, 13:57
hi,
I wish to Display two Dialogs(one object initializes another object).
so i have created MM, SM classes.
MM is the main object.
i want to show SM, in MM's initialization.
so i declared the SM object in MM'S constructor.
But when running the program , Only the MM Dialog is Visible.
SM Dialog is not visible.

as per the functional flow, main() will create MM, and MM will show.
then MM will create SM. and SM has to show.
But im getting the err here.

pls let me know, where my code went wrong.

How to show the second dialog in first dialog's constructor?

Below are the complete code.

main.cpp



#include <QApplication>
#include "mm.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
mm m;
return a.exec();
}



mm.h


#ifndef MM_H
#define MM_H
#include <QDialog>
#include <QtGui>

class mm : public QDialog
{
Q_OBJECT
public:
mm(QWidget *parent = 0);
public slots:
void showMM();
};
#endif // MM_H




mm.cpp


#include <QDialog>
#include <QLayout>
#include <QPushButton>

#include "mm.h"
#include "sm.h"

mm::mm(QWidget *parent)
: QDialog(parent)
{
QHBoxLayout *layout=new QHBoxLayout;
QPushButton *p = new QPushButton("MM");
connect(p,SIGNAL(clicked()),this,SLOT(showMM()));
layout->addWidget(p);
setLayout(layout);
setWindowTitle("MainMenu");
show();

sm s;
}


void mm::showMM()
{
QMessageBox::about(0,"","MM");
}



sm.h


#ifndef SM_H
#define SM_H

#include <QDialog>
#include <QtGui>

class sm : public QDialog
{
Q_OBJECT
public:
sm(QWidget *parent = 0);
public slots:
void showSM();
};
#endif // SM_H



sm.cpp



#include "sm.h"

sm::sm(QWidget *parent)
: QDialog(parent)
{
QHBoxLayout *layout1=new QHBoxLayout;
QPushButton *p1 = new QPushButton("SM");
layout1->addWidget(p1);
setLayout(layout1);
setWindowTitle("sub");
this->show();
}

void sm::showSM()
{
QMessageBox::about(0,"","SM");
}


thanks
Bala

yogeshgokul
24th September 2009, 14:07
Create the instance of SM in heap. Currently you are creating in stack so its deleting immediately.
Or you can call exec() instead of show();

So now your code should look like this:

mm::mm(QWidget *parent)
: QDialog(parent)
{
QHBoxLayout *layout=new QHBoxLayout;
QPushButton *p = new QPushButton("MM");
connect(p,SIGNAL(clicked()),this,SLOT(showMM()));
layout->addWidget(p);
setLayout(layout);
setWindowTitle("MainMenu");
show();
sm *s = new sm(); // This line is changed
}

BalaQT
24th September 2009, 15:46
Great Yogesh,

Thanks for ur excellent support.

Bala