Results 1 to 3 of 3

Thread: Child object not showing

  1. #1
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Child object not showing

    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

    Qt Code:
    1. #include <QApplication>
    2. #include "mm.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. mm m;
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    mm.h
    Qt Code:
    1. #ifndef MM_H
    2. #define MM_H
    3. #include <QDialog>
    4. #include <QtGui>
    5.  
    6. class mm : public QDialog
    7. {
    8. Q_OBJECT
    9. public:
    10. mm(QWidget *parent = 0);
    11. public slots:
    12. void showMM();
    13. };
    14. #endif // MM_H
    To copy to clipboard, switch view to plain text mode 


    mm.cpp
    Qt Code:
    1. #include <QDialog>
    2. #include <QLayout>
    3. #include <QPushButton>
    4.  
    5. #include "mm.h"
    6. #include "sm.h"
    7.  
    8. mm::mm(QWidget *parent)
    9. : QDialog(parent)
    10. {
    11. QHBoxLayout *layout=new QHBoxLayout;
    12. QPushButton *p = new QPushButton("MM");
    13. connect(p,SIGNAL(clicked()),this,SLOT(showMM()));
    14. layout->addWidget(p);
    15. setLayout(layout);
    16. setWindowTitle("MainMenu");
    17. show();
    18.  
    19. sm s;
    20. }
    21.  
    22.  
    23. void mm::showMM()
    24. {
    25. QMessageBox::about(0,"","MM");
    26. }
    To copy to clipboard, switch view to plain text mode 

    sm.h
    Qt Code:
    1. #ifndef SM_H
    2. #define SM_H
    3.  
    4. #include <QDialog>
    5. #include <QtGui>
    6.  
    7. class sm : public QDialog
    8. {
    9. Q_OBJECT
    10. public:
    11. sm(QWidget *parent = 0);
    12. public slots:
    13. void showSM();
    14. };
    15. #endif // SM_H
    To copy to clipboard, switch view to plain text mode 

    sm.cpp

    Qt Code:
    1. #include "sm.h"
    2.  
    3. sm::sm(QWidget *parent)
    4. : QDialog(parent)
    5. {
    6. QHBoxLayout *layout1=new QHBoxLayout;
    7. QPushButton *p1 = new QPushButton("SM");
    8. layout1->addWidget(p1);
    9. setLayout(layout1);
    10. setWindowTitle("sub");
    11. this->show();
    12. }
    13.  
    14. void sm::showSM()
    15. {
    16. QMessageBox::about(0,"","SM");
    17. }
    To copy to clipboard, switch view to plain text mode 

    thanks
    Bala

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Child object not showing

    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:
    Qt Code:
    1. mm::mm(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. QHBoxLayout *layout=new QHBoxLayout;
    5. QPushButton *p = new QPushButton("MM");
    6. connect(p,SIGNAL(clicked()),this,SLOT(showMM()));
    7. layout->addWidget(p);
    8. setLayout(layout);
    9. setWindowTitle("MainMenu");
    10. show();
    11. sm *s = new sm(); // This line is changed
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to yogeshgokul for this useful post:

    BalaQT (24th September 2009)

  4. #3
    Join Date
    Aug 2009
    Location
    coimbatore,India
    Posts
    314
    Thanks
    37
    Thanked 47 Times in 43 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Child object not showing

    Great Yogesh,

    Thanks for ur excellent support.

    Bala

Similar Threads

  1. Replies: 4
    Last Post: 4th January 2009, 01:14
  2. showing menu mouseclick event with opengl object
    By validator in forum Qt Programming
    Replies: 1
    Last Post: 8th March 2008, 23:25
  3. Showing QMainWindow without showing a child QWidget
    By discostu in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2007, 09:03
  4. Replies: 4
    Last Post: 10th May 2006, 22:37
  5. Replies: 9
    Last Post: 9th May 2006, 19:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.