Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6707299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:37:22+00:00 2026-05-26T07:37:22+00:00

QScrollArea , for some reason, is ignoring the contentMargins setting when I set QGraphicsView

  • 0

QScrollArea, for some reason, is ignoring the contentMargins setting when I set QGraphicsView as its widget. Looking at the snippet below, can someone please tell if I’m doing something wrong or it could be a bug in the SDK?

Snippet 1 (works perfect):

QWidget *appWindow = new QWidget;

QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);

QWidget *widgetToScroll = new QWidget(sa);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);

QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);

appWindow->show();

Snippet 2 (It’s like setContentMargins() call is ignored completely):

QWidget *appWindow = new QWidget;

QScrollArea *sa = new QScrollArea(appWindow);
sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
sa->setContentMargins(50, 50, 50, 50);

QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
widgetToScroll->resize(5000, 5000);
sa->setWidget(widgetToScroll);

QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
appWindowLayout->addWidget(sa);
appWindow->setLayout(appWindowLayout);

appWindow->show();

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:37:23+00:00Added an answer on May 26, 2026 at 7:37 am

    It looks like you are confusing the structure of how to nest a QGraphicsView and a QGraphicsScene. (Maybe it was just a typo?)

        QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
    

    should be changed to

        QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa);
    

    or

        QGraphicsView *widgetToScroll = new QGraphicsView();
        sa->setWidget(widgetToScroll);
    

    When you add a QWidget to a layout, you change the widget’s parent. When you set a widget (or QGraphicsView) to a QScrollArea, you change that widget’s parent. See Object Trees & Ownership for more information. So if you wanted to set up your QGraphicsView inside a QScrollArea your code would look like this:

        QWidget *appWindow = new QWidget;
    
        QScrollArea *sa = new QScrollArea(); // No need to specify a parent here if
                                             // you add it to a layout later
        sa->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        sa->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
        sa->setContentsMargins(50, 50, 50, 50);
    
        QGraphicsView *widgetToScroll = new QGraphicsView();
        widgetToScroll->setAlignment(Qt::AlignLeft | Qt::AlignTop);
        widgetToScroll->resize(5000, 5000);
        sa->setWidget(widgetToScroll); // This sets the parent for widgetToScroll
    
        QVBoxLayout *appWindowLayout = new QVBoxLayout();
        appWindowLayout->addWidget(sa); // This sets the parent for sa
        appWindow->setLayout(appWindowLayout); // This sets the parent for appWindowLayout
    
        appWindow->show();
    

    As a side note…

    When using QGraphicsViews with a QGraphicsScene, instead of setting the margins using a QScrollArea’s setContentsMargins, I use the QGraphicsView automatic scrolling and just set the scene rect to have a larger margin that the size of my content like so:

        QWidget *appWindow = new QWidget;
    
        QGraphicsView *widgetToScroll = new QGraphicsView();
        QGraphicsScene *scene = new QGraphicsScene();
        scene->addRect(0,0, 5000, 5000);
    
        widgetToScroll->setSceneRect(-50,-50, 5050, 5050);
        widgetToScroll->setScene(scene);
    
        QVBoxLayout *appWindowLayout = new QVBoxLayout(appWindow);
        appWindowLayout->addWidget(widgetToScroll);
    
        appWindow->setLayout(appWindowLayout);
        appWindow->show();
    

    The QGraphicsView includes quite a bit more than just automatic scrolling when needed. You can resize everything inside of it and quite a bit more. It is great for 2D layouts, interactions and animations. See Qt’s Graphics View Framework at http://doc.qt.io/qt-5/graphicsview.html for more information.

    Here is more information that may be useful when using margins and paddings: The Box Model used by QStyleSheets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a QScrollArea fathering my awesome scrolling widget. I like to do updates
I have a ScrollArea, containing a VBoxLayout containing several Labels: realmScroll = QScrollArea(self.container.widget()) realmScroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
I have a Widget with QScrollArea in it and I want it to be
Is there a way to set the width of a QScrollArea so that a
I am trying to use a QTextEdit widget inside of a form containing several
I'm trying to put a new customized widget when the user press a button,
How to Fit ScrollArea into Grid in QT ? I want to apply QScrollArea
I'm writing an image viewer as a custom Qt widget (see: https://github.com/dov/Qviv ) and
Is it possible to add some black space between views that are shown by
This question relates to http://www.qtcentre.org/threads/18151-QScrollArea-misbehaving-background-style http://www.qtforum.org/article/34443/cannot-change-background-color-of-a-qscrollarea-with-setstylesheet.html I'm asking in here because I believe the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.