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. QScrollArea as central widget problem
Forum Updated to NodeBB v4.3 + New Features

QScrollArea as central widget problem

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.4k Views 3 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    As was suggested on another thread https://forum.qt.io/topic/159300/qdockwidget-oddness, I tried to add a QScrollArea to my application:

    Old code:

    	ZTRACE_RUNTIME("Creating stackedWidget");
    	stackedWidget = new QStackedWidget(this);
    	stackedWidget->setObjectName("stackedWidget");
    	setCentralWidget(stackedWidget);
    
    	ZTRACE_RUNTIME("Creating Stacking Panel");
    	stackingDlg = new DSS::StackingDlg(this, pictureList);
    	stackingDlg->setObjectName("stackingDlg");
    
    	ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget");
    	stackedWidget->addWidget(stackingDlg);
    	
    	ZTRACE_RUNTIME("Creating Processing Panel");
    	processingDlg = new DSS::ProcessingDlg(this);
    	processingDlg->setObjectName("processingDlg");
    
    	ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget");
    	stackedWidget->addWidget(processingDlg);
    
    	stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    

    new code:

    	ZTRACE_RUNTIME("Creating scrollarea");
    	scrollArea = new QScrollArea(this);
    	scrollArea->setObjectName("scrollArea");
    	scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    	setCentralWidget(scrollArea);
    
    	ZTRACE_RUNTIME("Creating stackedWidget");
    	stackedWidget = new QStackedWidget(this);
    	stackedWidget->setObjectName("stackedWidget");
    	scrollArea->setWidget(stackedWidget);
    	scrollArea->ensureWidgetVisible(stackedWidget);
    
    	ZTRACE_RUNTIME("Creating Stacking Panel");
    	stackingDlg = new DSS::StackingDlg(this, pictureList);
    	stackingDlg->setObjectName("stackingDlg");
    
    	ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget");
    	stackedWidget->addWidget(stackingDlg);
    	
    	ZTRACE_RUNTIME("Creating Processing Panel");
    	processingDlg = new DSS::ProcessingDlg(this);
    	processingDlg->setObjectName("processingDlg");
    
    	ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget");
    	stackedWidget->addWidget(processingDlg);
    
    	stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    

    The problem is that nothing is visible in the central widget area? I expected the stacked widget with its owned widgets to be there.

    What have I missed or misunderstood?

    Pl45m4P 1 Reply Last reply
    0
    • PerdrixP Offline
      PerdrixP Offline
      Perdrix
      wrote on last edited by Perdrix
      #5

      The following works as I want which was to use a scroll bar when the widgets contained within the stacked widget hit their minimum size, and the user wants to resize the frame window smaller then would otherwise work.

      ZTRACE_RUNTIME("Creating stackedWidget");
      stackedWidget = new QStackedWidget(this);
      stackedWidget->setObjectName("stackedWidget");
      scrollArea->setWidget(stackedWidget);
      scrollArea->ensureWidgetVisible(stackedWidget);
      scrollArea->setWidgetResizable(true);
      scrollArea->setGeometry(0, 0, 500, 500);
      scrollArea->setMinimumHeight(300);
      

      David

      1 Reply Last reply
      1
      • PerdrixP Perdrix

        As was suggested on another thread https://forum.qt.io/topic/159300/qdockwidget-oddness, I tried to add a QScrollArea to my application:

        Old code:

        	ZTRACE_RUNTIME("Creating stackedWidget");
        	stackedWidget = new QStackedWidget(this);
        	stackedWidget->setObjectName("stackedWidget");
        	setCentralWidget(stackedWidget);
        
        	ZTRACE_RUNTIME("Creating Stacking Panel");
        	stackingDlg = new DSS::StackingDlg(this, pictureList);
        	stackingDlg->setObjectName("stackingDlg");
        
        	ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget");
        	stackedWidget->addWidget(stackingDlg);
        	
        	ZTRACE_RUNTIME("Creating Processing Panel");
        	processingDlg = new DSS::ProcessingDlg(this);
        	processingDlg->setObjectName("processingDlg");
        
        	ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget");
        	stackedWidget->addWidget(processingDlg);
        
        	stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        
        

        new code:

        	ZTRACE_RUNTIME("Creating scrollarea");
        	scrollArea = new QScrollArea(this);
        	scrollArea->setObjectName("scrollArea");
        	scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        	setCentralWidget(scrollArea);
        
        	ZTRACE_RUNTIME("Creating stackedWidget");
        	stackedWidget = new QStackedWidget(this);
        	stackedWidget->setObjectName("stackedWidget");
        	scrollArea->setWidget(stackedWidget);
        	scrollArea->ensureWidgetVisible(stackedWidget);
        
        	ZTRACE_RUNTIME("Creating Stacking Panel");
        	stackingDlg = new DSS::StackingDlg(this, pictureList);
        	stackingDlg->setObjectName("stackingDlg");
        
        	ZTRACE_RUNTIME("Adding Stacking Panel to stackedWidget");
        	stackedWidget->addWidget(stackingDlg);
        	
        	ZTRACE_RUNTIME("Creating Processing Panel");
        	processingDlg = new DSS::ProcessingDlg(this);
        	processingDlg->setObjectName("processingDlg");
        
        	ZTRACE_RUNTIME("Adding Processing Panel to stackedWidget");
        	stackedWidget->addWidget(processingDlg);
        
        	stackedWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        

        The problem is that nothing is visible in the central widget area? I expected the stacked widget with its owned widgets to be there.

        What have I missed or misunderstood?

        Pl45m4P Online
        Pl45m4P Online
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @Perdrix said in QScrollArea as central widget problem:

        I tried to add a QScrollArea to my application:

        There's nothing wrong with adding a QScrollArea as central widget.
        Works for me for any standard widget.

        I suspect your widget(s) is/are there, but you just don't see them because they are too small.

        Try

        stackedWidget->setGeometry(0, 0, 1000, 1000);
        

        Do you see something now?

        And how about this code?

        #include <QApplication>
        #include <QMainWindow>
        #include <QPushButton>
        #include <QHBoxLayout>
        #include <QScrollArea>
        #include <QStackedWidget>
        #include <QDebug>
        
        int main(int argc, char *argv[])
        {
            QApplication app(argc, argv);
            QMainWindow w;
        
            QWidget widget(&w);
            QPushButton pb("CLICK");
            widget.setObjectName("widget");
            widget.setStyleSheet("QWidget#widget { background-color: blue; }");
            QHBoxLayout layout(&widget);
            widget.setLayout(&layout);
            layout.addWidget(&pb);
            QScrollArea *area = new QScrollArea(&w);
            QStackedWidget *stack = new QStackedWidget(area);
        //    stack->setGeometry(0, 0, 500, 500);
            stack->addWidget(&widget);
            stack->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        
            area->ensureWidgetVisible(stack);
            area->setWidget(stack);
        
            w.setCentralWidget(area);
        
            w.show();
            return app.exec();
        }
        

        If this works, there's something wrong with your DSS:: classes.


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by
          #3

          The setGeometry call worked to get it to display but the dimensions of the widgets within the stacked widget then didn't resize as the main window was re-sized. I need to think again how to handle the problem of one of the widgets in the stacked widget set being a bit larger than desired (so that it creates sizing anomalies on small screens).

          D.

          Pl45m4P 1 Reply Last reply
          0
          • PerdrixP Perdrix

            The setGeometry call worked to get it to display but the dimensions of the widgets within the stacked widget then didn't resize as the main window was re-sized. I need to think again how to handle the problem of one of the widgets in the stacked widget set being a bit larger than desired (so that it creates sizing anomalies on small screens).

            D.

            Pl45m4P Online
            Pl45m4P Online
            Pl45m4
            wrote on last edited by Pl45m4
            #4

            @Perdrix said in QScrollArea as central widget problem:

            but the dimensions of the widgets within the stacked widget then didn't resize as the main window was re-sized

            Then you are using QScrollArea wrong... or expect something different from it.
            What's the point of scrollbars or a scrollable area in first place, if the widget would resize with the window?!

            QScrollArea is for displaying large (static) content like huge images through a small(er) viewport.
            Whenever you resize the window around it, the viewport (visible area) changes... not the scroll area's content widget.


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by Perdrix
              #5

              The following works as I want which was to use a scroll bar when the widgets contained within the stacked widget hit their minimum size, and the user wants to resize the frame window smaller then would otherwise work.

              ZTRACE_RUNTIME("Creating stackedWidget");
              stackedWidget = new QStackedWidget(this);
              stackedWidget->setObjectName("stackedWidget");
              scrollArea->setWidget(stackedWidget);
              scrollArea->ensureWidgetVisible(stackedWidget);
              scrollArea->setWidgetResizable(true);
              scrollArea->setGeometry(0, 0, 500, 500);
              scrollArea->setMinimumHeight(300);
              

              David

              1 Reply Last reply
              1
              • PerdrixP Perdrix has marked this topic as solved on

              • Login

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