Important: Please read the Qt Code of Conduct -

[Solved] Scrollable Widget as container for other widgets

  • Here's what I'm trying to achieve:

    I'd like to have a widget I can place anywhere on a view/form, that'll be able to contain other child controls and view them using a scroll bar, if necessary. The size of such parent widget can be 100x100 pixels, but it can host a "logical" area of 500x500 where controls are placed and the user should be able to reach them via vertical and horizontal scroll bars (that appear when the sub-area is larger than the viewport).

    I tried using QAbstractScrollArea for this, but although I can see my controls inside of it, I can't get it to show scroll bars. This is what I'm doing inside the constructor for the QAbstractScrollArea derived class:

       @ // add controls

    QPushButton *a = new QPushButton(QString("a"), this);
    QPushButton *b = new QPushButton(QString("b"), this);
    QPushButton *c = new QPushButton(QString("c"), this);
    QPushButton *d = new QPushButton(QString("d"), this);
    QPushButton *e = new QPushButton(QString("e"), this);
    QPushButton *f = new QPushButton(QString("f"), this);
    QPushButton *g = new QPushButton(QString("g"), this);
    QPushButton *h = new QPushButton(QString("h"), this);

    this->addScrollBarWidget(new QScrollBar(this), Qt::AlignRight);

    Any ideas will be most welcome.

  • I would put all the buttons into a container widget with a layout (a QWidget is sufficient) and put that as the viewport widget of a [[Doc:QScrollArea]]. The "sipdialog example":/doc/qt-4.8/dialogs-sipdialog.html could be of help here.

  • Reported as no longer relevant -- I interpret it as solved.

Log in to reply