Results 1 to 3 of 3

Thread: Layout changes parent by itself. How comoes?

  1. #1
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded

    Default Layout changes parent by itself. How comoes?

    Hi,

    I am trying to put some buttons dynamically into a QGridLayout, that is in a QTabView. Therefore I put that QGridLayout into a tab and fill it with the buttons.
    The problem is, that when I add more than 4 Buttons to that layout it changes its parent somwhow and I get an error. The error occurs, when the button shall be added to the layout.
    Qt Code:
    1. Error:
    2. parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
    3. parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
    4. parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
    5. parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
    6. parent: QWidget(0x289be8, name = "tabUserSpecificMethods")
    7. parent: screenMeasMeth(0x260830, name = "screenMeasMeth")
    8. QLayout::parentWidget: A layout can only have another layout as a parent.
    9. Segmentation fault
    To copy to clipboard, switch view to plain text mode 


    my Code looks like that:
    Qt Code:
    1. VLaytUserSpecificMethods = new QGridLayout(ui->tabUserSpecificMethods);
    2. VLaytUserSpecificMethods->setHorizontalSpacing(6);
    3. VLaytUserSpecificMethods->setVerticalSpacing(6);
    4. VLaytUserSpecificMethods->setObjectName(QString::fromUtf8("VLaytUserSpecificMethods"));
    5. VLaytUserSpecificMethods->setContentsMargins(10, 15, 0, 0);
    6. VLaytUserSpecificMethods->setGeometry(QRect(0,0,431,490));
    7.  
    8. for(int i = 0; i <= tmpMethodNames.size()-1; i++){
    9. listBtnUserMeasMeth[i] = new QPushButton(this);
    10. listBtnUserMeasMeth[i]->setVisible(true);
    11. listBtnUserMeasMeth[i]->setText(tmpMethodNames.at(i));
    12. listBtnUserMeasMeth[i]->setMaximumSize(QSize(200,55));
    13. listBtnUserMeasMeth[i]->setMinimumSize(QSize(200,55));
    14. listBtnUserMeasMeth[i]->setStyleSheet(globalStyleSheet::btnEnabled);
    15. listBtnUserMeasMeth[i]->setFont(dynamicBtnFont);
    16. listBtnUserMeasMeth[i]->setFocusPolicy(Qt::NoFocus);
    17.  
    18. qDebug() << "parent: " << VLaytUserSpecificMethods->parent();
    19. if(i % 2 == 0){
    20. VLaytUserSpecificMethods->addWidget(listBtnUserMeasMeth[i],c_GridRowCounter,0);
    21. }
    22. else{
    23. VLaytUserSpecificMethods->addWidget(listBtnUserMeasMeth[i],c_GridRowCounter,1);
    24. c_GridRowCounter++;
    25. }
    26. }
    27. VLaytUserSpecificMethods->addItem(VSpacerBtnList,++c_GridRowCounter,0);
    To copy to clipboard, switch view to plain text mode 


    Does anybody know the reason, why this happens?

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

    Default Re: Layout changes parent by itself. How comoes?

    Are you sure the error happens in there?

    Did you try a debug output after the loop?
    What does the backtrace of the crash say?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    18
    Thanks
    3
    Qt products
    Qt3 Qt4 Qt/Embedded

    Default [SOLVED] Layout changes parent by itself. How comoes?

    Yes, i tried debugging after the loop. The error occurs when adding the widget to the layout (line 20 / 23).

    Ok, I found a solution to go around that problem.

    Qt Code:
    1. ui->gridLayout->setHorizontalSpacing(20);
    2. ui->gridLayout->setVerticalSpacing(6);
    3. ui->gridLayout->setContentsMargins(10, 15, 0, 0);
    4.  
    5. UserSpecificMethods *userSpecificMeasMethods = new UserSpecificMethods();
    6. tmpMethodNames = userSpecificMeasMethods->readUserMeasMethodNames();
    7. delete userSpecificMeasMethods;
    8.  
    9. for(int i = 0; i <= tmpMethodNames.size()-1; i++){
    10. listBtnUserMeasMeth[i] = new QPushButton(this);
    11. listBtnUserMeasMeth[i]->setVisible(true);
    12. listBtnUserMeasMeth[i]->setText(tmpMethodNames.at(i));
    13. listBtnUserMeasMeth[i]->setMaximumSize(QSize(200,55));
    14. listBtnUserMeasMeth[i]->setMinimumSize(QSize(200,55));
    15. listBtnUserMeasMeth[i]->setStyleSheet(globalStyleSheet::btnEnabled);
    16. listBtnUserMeasMeth[i]->setFont(dynamicBtnFont);
    17. listBtnUserMeasMeth[i]->setFocusPolicy(Qt::NoFocus);
    18.  
    19. qDebug() << /*"parent: " << /*VLaytUserSpecificMethodsui->gridLayout->parent() <<*/ "\n vektor: " << listBtnUserMeasMeth[i];
    20. if(i % 2 == 0){
    21. ui->gridLayout->addWidget(/*vectorUserMeasMethodBtns[i]*/listBtnUserMeasMeth[i],c_GridRowCounter,0);
    22. }
    23. else{
    24. ui->gridLayout->addWidget(/*vectorUserMeasMethodBtns[i]*/listBtnUserMeasMeth[i],c_GridRowCounter,1);
    25. c_GridRowCounter++;
    26. }
    27. }
    28. QSpacerItem *VSpacerBtnList = new QSpacerItem(10,10,QSizePolicy::Expanding,QSizePolicy::Expanding);
    29. ui->gridLayout->addItem(VSpacerBtnList,++c_GridRowCounter,0);
    To copy to clipboard, switch view to plain text mode 

    I only put the QGridLayout in the Tab with the QtDesigner and add the buttons like before. This works very nice. The only thing is, that when I want to reload the tab, I have to remove the Buttons from the layout before, so that they won't be put twice to the QGridLayout.

    It works very well.

Similar Threads

  1. Replies: 0
    Last Post: 12th December 2010, 05:09
  2. Replies: 5
    Last Post: 21st April 2010, 21:36
  3. Replies: 4
    Last Post: 5th September 2009, 15:24
  4. Replies: 0
    Last Post: 25th May 2009, 10:00
  5. Replies: 6
    Last Post: 18th December 2008, 21:16

Tags for this Thread

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.