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

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();
    setWidget(dockContentsWidget);
    QVBoxLayout* dockLayout = new QVBoxLayout();
    dockContentsWidget->setLayout(dockLayout);
    
    // Create the scroll area and content widget
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setContentsMargins(10, 10, 10, 10);
    dockLayout->addWidget(scrollArea);
    QWidget* bgWidget = new QWidget();
    bgWidget->setStyleSheet("QWidget { background-color: #3A3A3A; }");
    scrollArea->setWidget(bgWidget);
    // Create the scroll area contents layout
    vertLayout = new QVBoxLayout();
    vertLayout->setAlignment(Qt::AlignTop);
    vertLayout->setMargin(0);
    vertLayout->setSpacing(0);
    bgWidget->setLayout(vertLayout);
    

    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->setWidgetResizable(true);
    parentWidget->setContentsMargins(10, 10, 10, 10);
    setWidget(parentWidget);
    
    QVBoxLayout* parentLayout = new QVBoxLayout();
    parentWidget->setLayout(parentLayout);
    
    QWidget* bgWidget = new QWidget();
    bgWidget->setStyleSheet("QWidget { background-color: #3A3A3A; }");
    parentLayout->addWidget(bgWidget);
    
    vertLayout = new QVBoxLayout();
    vertLayout->setAlignment(Qt::AlignTop);
    vertLayout->setMargin(0);
    vertLayout->setSpacing(0);
    bgWidget->setLayout(vertLayout);
    

    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

    Hi,

    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

    @Andaharoo
    Hi
    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;
        dock.setWidget(area);
        QWidget *widget = new QWidget;
        area->setWidget(widget);
        widget->setStyleSheet("background-color: #3A3A3A;");
        widget->setMinimumSize(400, 400);
        dock.show();
        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.
    0_1531274737315_ex.png
    Resizing the dock however, results in this:
    0_1531274773894_exs.png


  • Lifetime Qt Champion

    Hi
    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

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


Log in to reply