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. Have a QWidget expand inside and resize with QScrollArea
QtWS25 Last Chance

Have a QWidget expand inside and resize with QScrollArea

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 2.1k Views
  • 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.
  • qwasder85Q Offline
    qwasder85Q Offline
    qwasder85
    wrote on last edited by qwasder85
    #1

    How can I get a QWidget to expand within the visible area of a QScrollArea and also resize with said ScrollArea? Setting the QSizePolicy to Expanding results in an empty ScrollArea.
    Is there a proper way without catching resize signals from the ScrollArea and relaying them to the QWidget?

    My goal is to have a QWidget resize down to its minimum size and then display scrollbars if the parent gets even smaller.

        // This results in an empty QScrollArea
        QStackedWidget* p_stacked_widget = new QStackedWidget;
        p_stacked_widget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
        p_stacked_widget->setContentsMargins( 0, 0, 0, 0 );
        p_stacked_widget->setMinimumSize( 500, 400 );
    
        // This QScrollArea is set to "Expanding" in a parent widget
        QScrollArea* p_scroll_area = new QScrollArea;
        p_scroll_area->setContentsMargins( 0, 0, 0, 0 );
        p_scroll_area->setWidget( m_p_stacked_widget );
    

    Maybe I'm going about this all wrong?

    raven-worxR 1 Reply Last reply
    0
    • qwasder85Q qwasder85

      How can I get a QWidget to expand within the visible area of a QScrollArea and also resize with said ScrollArea? Setting the QSizePolicy to Expanding results in an empty ScrollArea.
      Is there a proper way without catching resize signals from the ScrollArea and relaying them to the QWidget?

      My goal is to have a QWidget resize down to its minimum size and then display scrollbars if the parent gets even smaller.

          // This results in an empty QScrollArea
          QStackedWidget* p_stacked_widget = new QStackedWidget;
          p_stacked_widget->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding );
          p_stacked_widget->setContentsMargins( 0, 0, 0, 0 );
          p_stacked_widget->setMinimumSize( 500, 400 );
      
          // This QScrollArea is set to "Expanding" in a parent widget
          QScrollArea* p_scroll_area = new QScrollArea;
          p_scroll_area->setContentsMargins( 0, 0, 0, 0 );
          p_scroll_area->setWidget( m_p_stacked_widget );
      

      Maybe I'm going about this all wrong?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @qwasder85
      for this case you need to subclass QScrollArea and in it's resizeEvent you need to resize the content widget:

      MyScrollArea::resizeEvent( QResizeEvent* event )
      {
          QScrollArea::resizeEvent( event );
      
          if( QWidget* w = this->widget() )
          {
                QSize viewportSize = this->viewport()->size();
                w->resize( viewportSize.boundedTo( w->minimumSize() );
          }
      }
      

      Something like this.
      But i leave it to you, to calculate the size correctly beforehand depending if a scrollbar is needed or not.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1

      • Login

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