Qt World Summit: Register Today!

Scroll Area in QDockWidget

  • I'm trying to add a scroll area to a dock widget. I've tried a lot of different random things. I'll share what I think should be correct. From what I've seen widgets like QScrollArea and QDockWidget have setWidget functions where you set the "content widget." Then in each content widget you'll probably set a layout.

    So in my dock constructor I do this:

    // Create the dock content widget and layout
    QWidget* dockContentsWidget = new QWidget();
    QVBoxLayout* dockLayout = new QVBoxLayout();
    // Create the scroll area and content widget
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setContentsMargins(10, 10, 10, 10);
    QWidget* bgWidget = new QWidget();
    bgWidget->setStyleSheet("QWidget { background-color: #3A3A3A; }");
    // Create the scroll area contents layout
    vertLayout = new QVBoxLayout();

    But this simply shows nothing. The dock widget shows but nothing is in it. Previously I did not consider content widgets and had this:

    QScrollArea* parentWidget = new QScrollArea();
    parentWidget->setContentsMargins(10, 10, 10, 10);
    QVBoxLayout* parentLayout = new QVBoxLayout();
    QWidget* bgWidget = new QWidget();
    bgWidget->setStyleSheet("QWidget { background-color: #3A3A3A; }");
    vertLayout = new QVBoxLayout();

    This worked but the scroll area did not. It would just scrunch everything together on resize. Any help would be greatly appeciated.

  • Lifetime Qt Champion


    Out of curiosity, why not put the QScrollArea directly in the QDockWidget ? dockContentsWidget seems to be there only to contain scrollArea which looks a bit like a waste of resources.

  • @SGaist Is that not what I tried on the second chunk of code (my first attempt). setWidget(QScrollArea)? Everything showed in this one. But the scrollbar never pops up and everything just "scrunches" down on resize.

  • Lifetime Qt Champion

    unless you set a MinimumSize on the widgets you put in Scroll Area, they are just "flatten"
    when more is added. They have a very small minimum for pure QWidgets.

  • Lifetime Qt Champion

    I think the following does what you want:

    int main(int argc, char **argv)
        QApplication app(argc, argv);
        QDockWidget dock;
        QScrollArea *area = new QScrollArea;
        QWidget *widget = new QWidget;
        widget->setStyleSheet("background-color: #3A3A3A;");
        widget->setMinimumSize(400, 400);
        return app.exec();

  • @mrjj How can I get it to update the minimum size based on the contents in the widget. I thought I'd be able to get this to work automatically?
    In my case I have a

    Dock Widget
        Widget Container for background elements and for different color
            Item 1
            Item 2

    It's kind of a list widget.
    Resizing the dock however, results in this:

  • Lifetime Qt Champion

    What are each item ? ( i mean what type of class )
    Normally one would use QListWidget/view for such list and it knows its size automatically.
    There is http://doc.qt.io/qt-5/qfontmetrics.html
    to know text sizes but i need to understand why you have this issue in first place.

  • @mrjj Each row is a QWidget with a horizontal layout. This makes a lot of things I do with it easier. Initially I explored the qlistwidget option but decided it was too difficult to do what I wanted to do. That's besides the point though.

    Each row contains a QPushButton (with eye icon), an extension to qlabel I made called ButtonLabel (for drag and drop), and another optional QPushButton for polygon color.

    What's preventing the minimum size information from getting to the parent?

  • Lifetime Qt Champion

    well you need to set some minimum on the "row" it seems.

Log in to reply