Results 1 to 3 of 3

Thread: Error when QVBoxLayout is overflowed

  1. #1
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Error when QVBoxLayout is overflowed

    Hi, I have problem with QVBoxLayout (and other Q...Layout objects).
    1. I have QTabWidget, I add tab with set widget QScrollArea.
    2. QScrollArea has set widget QWidget.
    3. QWidget has set layout QVBoxLayout.
    4. I make array of pointers to QLabel, next in "for" loop I make objects of QLabel, add pointers to array and add objects to QVBoxLayout.
    Qt Code:
    1. (...)
    2. QTabWidget *tabWidget;
    3. QScrollArea *scrollArea1;
    4. QWidget *widget1;
    5. QLayout *layoutTab1;
    6. QLabel **labels;
    7. (...)
    8. this->tabWidget = new QTabWidget();
    9. this->scrollArea1 = new QScrollArea(this->tabWidget);
    10. this->widget1 = new QWidget(this->scrollArea1);
    11. this->layoutTab1 = new QVBoxLayout(widget1);
    12.  
    13. this->widget1->setLayout(this->layoutTab1);
    14. this->scrollArea1->setWidgetResizable(true);
    15. this->scrollArea1->setWidget(this->widget1);
    16. this->tabWidget->insertTab(0, this->scrollArea1, tr(TAB1_));
    17. *(this->labels) = new QLabel[10];
    18.  
    19. for(int i=0; i<=9; i++)
    20. {
    21. labels[i] = new QLabel(tr("TEST"));
    22. labels[i]->setFrameStyle(1);
    23. labels[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
    24. this->layoutTab1->addWidget(this->labels[i]);
    25. }
    To copy to clipboard, switch view to plain text mode 

    My problem is:
    When number of labels is over 5 they are outside app window (scrolling in QScrollArea works perfectly) and (in debug mode) I get error after close app - "Application has stopped working". Released app doesn't run, this same error is before start. If number of QLabels is less (all can be in window) everything works good.
    I think this is problem with QVBoxLayout because I comment everything except making object of QVBoxLayout, making array of objects QLabel and adding them to QVBoxLayout and problem is this same.
    What's wrong? Please help.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Error when QVBoxLayout is overflowed

    this code:
    Qt Code:
    1. QLabel **labels;
    2. ...
    3. *(this->labels) = new QLabel[10];
    4.  
    5. for(int i=0; i<=9; i++)
    6. {
    7. labels[i] = new QLabel(tr("TEST"));
    8. labels[i]->setFrameStyle(1);
    9. labels[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
    10. this->layoutTab1->addWidget(this->labels[i]);
    11. }
    To copy to clipboard, switch view to plain text mode 

    is very problematic.

    It seems you are probably coming from a C background, but don't understand what an object is.

    Put all your labels in a container, a QList or QVector.
    Qt Code:
    1. //in the class header make a member
    2. QList<QLabel*> m_labels;
    3. ...
    4. //in the implementation then you do:
    5. for(int i=0; i<=9; i++)
    6. {
    7. QLabel *pLabel = new QLabel(this,tr("TEST")); //if you don't give the label a parent, you have to make sure to delete it your self!
    8. pLabel ->setFrameStyle(1);
    9. pLabel ->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Maximum);
    10. this->layoutTab1->addWidget(pLabel );
    11. m_lables.push_back(pLabel);
    12. }
    13. ...
    14. //To access label you do:
    15. m_labels.at(index)->setText("some text");
    To copy to clipboard, switch view to plain text mode 

    Also, you have made your pointer local in the method - so you wont have access to them once the method is out of scope.
    So in my example the list should be a member.
    Last edited by high_flyer; 13th February 2011 at 20:54.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    emil0076 (14th February 2011)

  4. #3
    Join Date
    Feb 2011
    Location
    Poland
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error when QVBoxLayout is overflowed

    It works! Thank you.

Similar Threads

  1. QVBoxLayout to center
    By seltra in forum Newbie
    Replies: 2
    Last Post: 7th October 2010, 18:23
  2. QHBoxLayout & QVBoxLayout
    By damodharan in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2010, 14:13
  3. Problem with QHBoxlayout & QVBoxlayout
    By phillip_Qt in forum Qt Tools
    Replies: 5
    Last Post: 13th January 2009, 10:26
  4. QScrollView with a QVboxlayout
    By rishid in forum Newbie
    Replies: 1
    Last Post: 18th January 2008, 16:14
  5. [Qt4] Can QTextEdit be overflowed?
    By naresh in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2006, 13:28

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.