Results 1 to 2 of 2

Thread: QScrollArea not working as expected with QWidget and QVBoxLayout

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default QScrollArea not working as expected with QWidget and QVBoxLayout

    So I have this QFrame which is the parent widget (represented by this in the code). In this widget, I want to place a QWidget at 10 px from top (and 10 px from bottom, as a result of which it will have a height of 140px, whereas parent is 160px). The QWidget will have a number of custom buttons inside it in a vertical layout, in a scroll area, so that when the height of the buttons combined exceeds the QWidget's height (140px), scroll sets in automatically. Because the scroll is not for the entire parent widget, but only for a child widget, the scroll should apply only to the child widget here. Here is my code:

    Qt Code:
    1. class MyButton: public QPushButton
    2. {
    3.  
    4. public:
    5. MyButton(std::string aText, QWidget *aParent);
    6.  
    7. };
    8.  
    9. MyButton::MyButton(std::string aText, QWidget *aParent): QPushButton(QString::fromStdString(aText), aParent)
    10. {
    11. this->setFixedHeight(30);
    12. this->setCursor(Qt::PointingHandCursor);
    13. this->setCheckable(false);
    14. this->setStyleSheet("background: rgb(74,89,98); color: black; border-radius: 0px; text-align: left; padding-left: 5px; border-bottom: 1px solid black;");
    15. }
    16.  
    17. this->setGeometry(x,y,width,160);
    18. this->setStyleSheet("border-radius: 5px; background:red;");
    19.  
    20. QWidget *dd = new QWidget(this);
    21. dd->setGeometry(0,10,width,140);
    22. dd->setStyleSheet("background: blue;");
    23.  
    24. QVBoxLayout *layout = new QVBoxLayout();
    25. dd->setLayout(layout);
    26.  
    27. for (int i = 0; i < fValues.size(); i++)
    28. {
    29. MyButton *button = new MyButton(fValues[i],dd);
    30. layout->addWidget(button);
    31. }
    32.  
    33. QScrollArea *scroll = new QScrollArea(this);
    34. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    35. scroll->setWidget(dd);
    To copy to clipboard, switch view to plain text mode 

    Contrary to my expectations, this is what I am getting (attached image). What am I doing wrong, and how do I fix this?

    Screen Shot 2015-03-01 at 10.49.55 PM.png
    Last edited by Cupidvogel; 1st March 2015 at 20:30.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QScrollArea not working as expected with QWidget and QVBoxLayout

    Do these modifications to your code.
    Qt Code:
    1. this->setGeometry(x,y,width,160);
    2. this->setStyleSheet("border-radius: 5px; background:red;");
    3.  
    4. QWidget *dd = new QWidget(this);
    5. //dd->setGeometry(0,10,width,140);//<<<<<<<<<<<<<<<<<<<<<<<<<Remove. Geometry will be managed by layout, don't set it.
    6. dd->setStyleSheet("background: blue;");
    7.  
    8. QVBoxLayout *layout = new QVBoxLayout();
    9. dd->setLayout(layout);
    10.  
    11. for (int i = 0; i < fValues.size(); i++)
    12. {
    13. MyButton *button = new MyButton(fValues[i].toStdString(),dd);
    14. layout->addWidget(button);
    15. }
    16.  
    17. QScrollArea *scroll = new QScrollArea(this);
    18. scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    19. scroll->setWidget(dd);
    20.  
    21. QGridLayout * gridLayout = new QGridLayout(this);//<<<<<<<<<<<<<<<<<<<<<<<<<Add
    22. gridLayout->addWidget(scroll);//<<<<<<<<<<<<<<<<<<<<<<<<<Add
    23. gridLayout->setMargin(10);//<<<<<<<<<<<<<<<<<<<<<<<<<Add
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. QAction::setEnabled() Not Working as Expected
    By pmwalk in forum Qt Programming
    Replies: 6
    Last Post: 24th March 2012, 06:20
  2. QDir not working as expected
    By BettaUseYoNikes in forum Qt Programming
    Replies: 2
    Last Post: 24th August 2011, 16:55
  3. QVBOxLayout problem with QSCrollArea and QWidget
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2011, 09:43
  4. Replies: 0
    Last Post: 17th August 2010, 14:37
  5. QTableView not working as expected.
    By junxuan in forum Qt Programming
    Replies: 7
    Last Post: 30th July 2009, 08:17

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.