Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED]scrollbars on a standard QWidget



  • Hi,
    For some days I have been to add scrollbars on a standard QWidget that contains other QWidgets.The number of contained widgets can be modified at runtime.QScrollArea seemed like a good starting point but I haven't been able to display scrollbars so far.

    The "doc":http://qt-project.org/doc/qt-4.8/qscrollarea.html says that to use QScrollArea on a standard QWidget you have to implement sizehint and apply QLayout::SetMinAndMaxSize constraint to the layout.

    I have tried to follow all that to no avail.

    This is how I implemented sizeHint:
    @

    class MStandardWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit MStandardWidget(QWidget *parent = 0);

    virtual QSize sizeHint () const
    

    {

    return QSize(500,500);
    }

    ..................

    };

    @

    and this is how my widgets are layed out in the constructor of my mainWidget:

    @

    labelContainer=new MStandardWidget(this);
    labelContainer->setBackgroundRole(QPalette::Dark);
    //I able to see this dark background ,meaning that my scrollArea is there,I just
    //can't figure out why the scrollbars are not showing up.

    labelContainerLayout=new QVBoxLayout();
    labelContainerLayout->setSizeConstraint(QLayout::SetNoConstraint);
    
    QLabel * mLabel1=new QLabel("TESTLABEL1",this);
    labelContainerLayout->addWidget(mLabel1);
    
    labelContainer->setLayout(labelContainerLayout);
    //labelContainer->setFixedSize(500,500);
    
    labelContainer->setMinimumSize(200,200);
    
    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(labelContainer);
    

    //GENERAL LAYOUT.
    widgetLayout=new QHBoxLayout();
    widgetLayout->addWidget(labelContainer);
    widgetLayout->addWidget(controlButton);

    setLayout(widgetLayout);
    

    @

    No matter how many things I add in the scrollArea, scrollbars are just not showing up.May be there is something I am missing here,or doing wrong? Please help.
    Thanks.


  • Moderators

    maybe because you don't add the QScrollArea to your main layout ;)
    @
    scrollArea = new QScrollArea;
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(labelContainer);

    //GENERAL LAYOUT.
    widgetLayout=new QHBoxLayout();
    widgetLayout->addWidget(labelContainer); //<-- add scrollArea here instead
    widgetLayout->addWidget(controlButton);
    @



  • Thanks,raven-works,
    You have saved my day more than once already!
    I thought that applying a scrollArea to the widget and adding the widget to the layout as usual does the trick but I obviously was wrong!I did what you said and it is working just fine.

    Thanks again.


Log in to reply