Results 1 to 7 of 7

Thread: initialize child widgets within parent?

  1. #1
    ucomesdag Guest

    Default initialize child widgets within parent?

    Hi, i was wondering if it is possible to initalize a child widget within the parent?
    I tried to add it in the constructor of the parent, but then the child widgets is not shown...

    In this case i want to initialize fx1 in the constructor of floorBoard... this to keep clean main.

    Qt Code:
    1. #include "stompBox.h"
    2. #include "floorBoard.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. floorBoard floor;
    9.  
    10. stompBox fx1(&floor);
    11. fx1.setId(0);
    12. fx1.setImage(":/images/fx1.png");
    13. fx1.setPos(floor.getPos(0));
    14.  
    15. QComboBox *fx1_comboBox = new QComboBox(&fx1);
    16. fx1_comboBox->addItem( "Test 1" );
    17. fx1_comboBox->addItem( "Test 2" );
    18. fx1_comboBox->addItem( "Test 3" );
    19.  
    20. fx1_comboBox->setGeometry(8, 31, 79, 13);
    21. fx1_comboBox->setEditable(false);
    22. fx1_comboBox->setFont(font);
    23. fx1_comboBox->setPalette(pal);
    24. fx1_comboBox->setFrame(false);
    25.  
    26. customButton fx1_button(false, QPoint::QPoint(4, 110), &fx1);
    27. customLed fx1_led(false, QPoint::QPoint(41, 4), &fx1);
    28. QObject::connect(&fx1_button, SIGNAL(valueChanged(bool)),
    29. &fx1_led, SLOT(setValue(bool)));
    30.  
    31. floor.show();
    32.  
    33. return app.exec();
    34. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: initialize child widgets within parent?

    If stompBox is a widget, then it shouldn't be any problem. If it is a dialog, though, you'll have to explicitely call show() or exec() for it. But you can still do the initialisation in the parent -- that's what is usually done, anyway...

  3. #3
    ucomesdag Guest

    Default Re: initialize child widgets within parent?

    It is a widget. But when I do so the widget isn't shown. And if I add .show() it just shown on initialization and disapears inmediatly after

    Qt Code:
    1. #include "stompBox.h"
    2. #include "floorBoard.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. floorBoard floor;
    9. floor.show();
    10.  
    11. return app.exec();
    12. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "floorBoard.h"
    2.  
    3. floorBoard::floorboard((QWidget *parent, ... )
    4. : QWidget(parent)
    5. {
    6. ...
    7.  
    8. stompBox fx1(this);
    9. fx1.setId(0);
    10. fx1.setImage(":/images/fx1.png");
    11. fx1.setPos(this->getPos(0));
    12.  
    13. QComboBox *fx1_comboBox = new QComboBox(&fx1);
    14. fx1_comboBox->addItem( "Test 1" );
    15. fx1_comboBox->addItem( "Test 2" );
    16. fx1_comboBox->addItem( "Test 3" );
    17.  
    18. fx1_comboBox->setGeometry(8, 31, 79, 13);
    19. fx1_comboBox->setEditable(false);
    20. fx1_comboBox->setFont(font);
    21. fx1_comboBox->setPalette(pal);
    22. fx1_comboBox->setFrame(false);
    23.  
    24. customButton fx1_button(false, QPoint::QPoint(4, 110), &fx1);
    25. customLed fx1_led(false, QPoint::QPoint(41, 4), &fx1);
    26. QObject::connect(&fx1_button, SIGNAL(valueChanged(bool)),
    27. &fx1_led, SLOT(setValue(bool)));
    28. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by ucomesdag; 6th June 2006 at 01:52.

  4. #4
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: initialize child widgets within parent?

    I think it should be something like

    Qt Code:
    1. stompBox *fx1 = new stompBox(this);
    2. fx1->setId(0);
    3. fx1->setImage(":/images/fx1.png");
    4. fx1->setPos(this->getPos(0));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    ucomesdag Guest

    Default Re: initialize child widgets within parent?

    Both do the samething, I guess... But that ain't the problem. The widget just flashes 1 time on the screen on initialization and thats it....

  6. #6
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: initialize child widgets within parent?

    Yes both do the samething but in your way of declaration the object is destroyed once the program is out of the constructor. Either you have to declare fx1,fx1_button..etc in your class declaration or use them the way you are using fx1_comboBox(as a pointer).

  7. The following user says thank you to munna for this useful post:


  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: initialize child widgets within parent?


  9. The following user says thank you to wysota for this useful post:


Similar Threads

  1. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00
  2. Infinite loop - resize parent from child
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2006, 13:21
  3. Referencing Parent Widget from Child
    By taylor34 in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2006, 15:13
  4. Replies: 2
    Last Post: 22nd February 2006, 14:58

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.