Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Scroll Area in QDockWidget
Forum Updated to NodeBB v4.3 + New Features

Scroll Area in QDockWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 3.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andaharoo
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        A Offline
        A Offline
        Andaharoo
        wrote on last edited by
        #3

        @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.

        mrjjM 1 Reply Last reply
        0
        • A Andaharoo

          @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.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          A 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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();
            }
            

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • mrjjM mrjj

              @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.

              A Offline
              A Offline
              Andaharoo
              wrote on last edited by
              #6

              @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

              1 Reply Last reply
              0
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                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.

                1 Reply Last reply
                1
                • A Offline
                  A Offline
                  Andaharoo
                  wrote on last edited by
                  #8

                  @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?

                  mrjjM 1 Reply Last reply
                  0
                  • A Andaharoo

                    @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?

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved