Results 1 to 2 of 2

Thread: Add QLayout to QWidget which already has a layout

  1. #1
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Add QLayout to QWidget which already has a layout

    I have a QWidget in my Window "AvailableDataWidget" while runtime I add layouts with groupboxes, labels and checkboxes. I want to delete them later and add new groupboxes and checkboxes.

    This is the way I create it:

    Qt Code:
    1. void MonitorWindow::on_actionGetAvailableData_triggered(){
    2. QVBoxLayout* verticalLayout[NumberOfAvailableTypes];
    3. QVBoxLayout* verticalLayoutForWidget = new QVBoxLayout(ui->AvailableDataWidget);
    4.  
    5. QGroupBox *TypeGroupBox[NumberOfAvailableTypes];
    6.  
    7. for (int i = 0; i < NumberOfAvailableTypes; i++){ //1 groupbox for each type
    8. TypeGroupBox[i] = new QGroupBox(ui->AvailableDataWidget);
    9. TypeGroupBox[i]->setTitle(DataList.at(i).name);
    10. verticalLayout[i] = new QVBoxLayout(TypeGroupBox[i]);
    11.  
    12. if (DataList.at(i).type != TYPEBINARY){
    13. QCheckBox *DataCheckbox[DataList.at(i).quantity];
    14. QLabel* tempLabel[DataList.at(i).quantity];
    15. QHBoxLayout* horizontalLayout[DataList.at(i).quantity];
    16.  
    17. for (int j = 1; j <= DataList.at(i).quantity; j++){
    18. DataCheckbox[j] = new QCheckBox(TypeGroupBox[i]);
    19. DataCheckbox[j]->setChecked(false);
    20. connect(DataCheckbox[j],SIGNAL(clicked()),SLOT(setTransducerFlags()));
    21. horizontalLayout[j] = new QHBoxLayout();
    22. horizontalLayout[j]->addWidget(DataCheckbox[j]);
    23.  
    24. tempLabel[j] = new QLabel(TypeGroupBox[i]);
    25. tempLabel[j]->setText(QString("%1%2%3").arg(DataList.at(i).name).arg(" ").arg(j));
    26. horizontalLayout[j]->addWidget(tempLabel[j]);
    27.  
    28. verticalLayout[i]->addLayout(horizontalLayout[j]);
    29. }
    30. }
    31. else{
    32. ...
    33. QCheckBox *DataCheckbox = new QCheckBox(TypeGroupBox[i]);
    34. DataCheckbox->setChecked(false);
    35. connect(DataCheckbox,SIGNAL(clicked()),SLOT(setTransducerFlags()));
    36. QLabel* tempLabel = new QLabel(TypeGroupBox[i]);
    37. tempLabel->setText(DataList.at(i).name);
    38. QHBoxLayout* horizontalLayout = new QHBoxLayout;
    39. horizontalLayout->addWidget(DataCheckbox);
    40. horizontalLayout->addWidget(tempLabel);
    41. myTransducermodel->addColumn(DataList.at(i).name);
    42. verticalLayout[i]->addLayout(horizontalLayout);
    43. }
    44. verticalLayoutForWidget->addWidget(TypeGroupBox[i]);
    45. }
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    So I need to delete the layout and widgets, right?
    AvailableDataWidget should be the parent of all layouts and widgets, so i tried this:
    Qt Code:
    1. QList<QWidget *> widgets = ui->AvailableDataWidget->findChildren<QWidget *>();
    2. foreach(QWidget * widget, widgets){
    3. delete widget;
    4. }
    To copy to clipboard, switch view to plain text mode 

    but it makes my program crash.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add QLayout to QWidget which already has a layout

    Quote Originally Posted by mikrocat View Post
    AvailableDataWidget should be the parent of all layouts and widgets, so i tried this:
    Qt Code:
    1. QList<QWidget *> widgets = ui->AvailableDataWidget->findChildren<QWidget *>();
    2. foreach(QWidget * widget, widgets){
    3. delete widget;
    4. }
    To copy to clipboard, switch view to plain text mode 

    but it makes my program crash.
    QObject::findChildren() is recursive by default, so you get the immediate children and their children and so on.
    When you delete a QObject object, it will delete all its children, so some of the pointers in your list will already have become invalid.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 18th August 2012, 08:57
  2. Replies: 6
    Last Post: 13th August 2011, 18:31
  3. Resize QLayout w/ Mouse...or, a QWidget similar to QDockWidget
    By kiss-o-matic in forum Qt Programming
    Replies: 2
    Last Post: 24th December 2009, 10:05
  4. Using QLayout for layout in a graphics scene
    By ticica in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2009, 04:07
  5. Replies: 4
    Last Post: 5th September 2009, 15:24

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.