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 a custom drawn widget
Forum Updated to NodeBB v4.3 + New Features

Scroll a custom drawn widget

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 693 Views 1 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.
  • D Offline
    D Offline
    Daniel Santos
    wrote on last edited by
    #1

    Hello,
    I have a QWidget on which I do custom drawing. I want to put it inside a scroll area but am not able to do so. The code is :
    displayArea = std::unique_ptr<MapDisplayArea>(new MapDisplayArea(this, mapReader.get()));

    QScrollArea* scroll = new QScrollArea();
    scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    scroll->setWidgetResizable(true);
    scroll->setWidget(displayArea.get());
    
    QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
    scroll->setLayout(boxLayout);
    this->setCentralWidget(scroll);
    

    DisplayArea is the widget where I draw. The window appears but doesn't scroll.
    Thanks,
    Regards

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      is displayArea->size() greater than the viewport size?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • D Offline
        D Offline
        Daniel Santos
        wrote on last edited by
        #3

        Hello,

        How do I get the viewport size ?

        Regards

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          How do I get the viewport size ?

          scroll->viewport()->size()

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          D 1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by Bonnie
            #5

            Does displayArea has valid size hint or minimum size that larger than the viewport size?
            (The viewport size is basically the scroll area size but without the scroll bars and frame.)

            widgetResizable : bool
            This property holds whether the scroll area should resize the view widget
            If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget()->resize(), and the scroll area will automatically adjust itself to the new size.
            If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.

            Or you can set the property to false and set the size of displayArea manually (must be large enough).

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

              Hi,

              @Daniel-Santos said in Scroll a custom drawn widget:

              QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
              scroll->setLayout(boxLayout);

              Why are you setting a layout on your QScrollArea ? If you need one, it should be on the widget contained in your scroll area.

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

              D 1 Reply Last reply
              1
              • VRoninV VRonin

                How do I get the viewport size ?

                scroll->viewport()->size()

                D Offline
                D Offline
                Daniel Santos
                wrote on last edited by
                #7

                @VRonin The view port is the size of the window, so I guess that's why it doesn't scroll. Now question is : how do I set the view port to the new size ?

                Thanks

                1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  @Daniel-Santos said in Scroll a custom drawn widget:

                  QBoxLayout* boxLayout = new QBoxLayout(QBoxLayout::TopToBottom);
                  scroll->setLayout(boxLayout);

                  Why are you setting a layout on your QScrollArea ? If you need one, it should be on the widget contained in your scroll area.

                  D Offline
                  D Offline
                  Daniel Santos
                  wrote on last edited by
                  #8

                  @SGaist You are right that didn't make any sense, I removed it. Anyway I got it working by implementing the method minimumSizeHint in the custom drawn widget.
                  Thanks for your help

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

                    Great !

                    Then please mark the thread as solved so other forum users may know a solution has been found :-)

                    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
                    0

                    • Login

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