[solved] QScrollArea on Tab with QGridLayout not working

  • I have read all the posts about QScrollArea I could find but so far nothing works. I have subclassed QTabWidget for my main view area:

    @SF_UnitView::SF_UnitView(QTabWidget* pParent)
    : QTabWidget(pParent)
    pSystemTab = new SystemView;
    addTab(pSystemTab, tr("System"));


    void SF_UnitView::setModel(SF_UnitModel* pModel)
    pSF_Model = pModel;
    pChannelTab = new ChannelView;
    insertTab(1, pChannelTab, tr("Channels"));

    Each tab is defined as a class. I want to add a scroll area in one of the tabs. Tabs are added after the data is read in. In this case I want to have a scroll area in the Channel tab. So far the best I get is a very small version of my window in the upper left corner of the Channel tab. The Channel tab class looks like this:

    @ChannelView::ChannelView(QWidget pParent)
    : QWidget(pParent)
    pScrollArea = new QScrollArea(this);
    pGridLayoutMain = new QGridLayout(pScrollArea);
    pHBoxChannelLabels = new QHBoxLayout;
    QVBoxLayout* pVBoxChannel = new QVBoxLayout;
    QLabel* labelChannelNumber = new QLabel("Channel Number");
    labelChannelNumber->setStyleSheet("border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-bottom: 0px;");
    QLabel* labelChannelName = new QLabel("Channel Name");
    labelChannelName->setStyleSheet("border-top-left-radius: 0px; border-top-right-radius: 0px;");

    .... More stuff

    pGridLayoutMain->addLayout(pVBoxChannel,    0, 0, 2, 1);       
    pGridLayoutMain->addLayout(pVBoxModes,      0, 1, 2, 1);                  
    pGridLayoutMain->addLayout(pVBoxAddSub,     0, 2, 2, 1);                  
    pGridLayoutMain->addWidget(labelServoOrder, 0, 3, 1, 8);              


    I am not sure where I am going wrong. I got the whole layout working without the scroll area first so I would know that was not an issue. Thanks.

  • Lifetime Qt Champion


    You should rather have something like

    QVBoxLayout set on ChannelView

    Add a QScrollArea to that layout

    Placeholder QWidget with your grid layout

    set placeholder widget in QScrollArea

    Currently your QScrollArea is not set in any layout manager hence its strange size.

  • Thank you. The layout showed up but no scroll bars. this is what I did:

    pScrollArea = new QScrollArea;
    QVBoxLayout* pVBoxMain = new QVBoxLayout(this);
    QWidget* pPlaceHolder = new QWidget(pScrollArea);
    pGridLayoutMain = new QGridLayout(pPlaceHolder);

    Also, the layout acts funny now. I can dynamically add some comboboxes and now when they are added to a row the whole row squishes down, clipping labels and other stuff as their boxes shrink. This all worked fine before. I am assuming its because I added the VBox as the base layout. It hard to explain but I don't understand how to attach pictures as it asks for a web address.

  • Here are captures of before using only QGridLayout and after using the QScrollArea. !http://i59.tinypic.com/bj5w9j.jpg(QGridLayout Only - No Servos Added)!!http://i58.tinypic.com/287nsxi.jpg(QGridLayout Only - Servos Added)!!http://i62.tinypic.com/2zjkh78.jpg(QScrollArea Used - No Servos Added)!!http://i60.tinypic.com/fdcbvs.jpg(QScrollArea Used - Servos Added)!

  • Lifetime Qt Champion

    And without that line ?


  • I tried it both with and without "setWidgetResizable." No scroll bars. No change in behavior. Horizontal size is fixed, adding things squishes the other columns. Without the ScrollArea the right side would grow. I was wondering over the weekend, can you do something so that all tabs on a TabWidget have scroll areas or do you have to do each tab individually?

  • I got the behavior I wanted by setting the central widget of my mainwindow to the QScrollArea and setting "setWidgetResizable" to true. I then added my tabwidget (subclassed as SF_UnitView) to the ScrollArea. This enabled the ScrollArea on all the tabs. So far this seems to work the way I was hoping.

    My only other issues now is that on the initial tab I have a picture. It maintains its aspect ratio if the window is smaller than the picture but will stretch the picture horizontally but not vertically when the window is sized bigger than its minimum size. Do I need to intercept the resize and adjust it so the aspect is right? Thanks for all your suggestions.

  • Lifetime Qt Champion

    Your widgets were nested differently than I thought, but anyway, cool you found a solution.

    Do you mean resize when it's smaller otherwise keep it size ?

  • Actually, resize when the window is larger than the picture so the picture keeps its aspect ratio. When the window is larger than the picture, the picture will stretch horizontally but not vertically causing it to lose its aspect ratio. I don't know much about catching the resize signal at this point but I am guessing I need to adjust the vertical to match the horizontal stretch? Not a huge issue now but would make it look nicer in the end. Thanks,

  • Lifetime Qt Champion

    How did you setup that picture ?

  • This is the code for the label. Its placed in an HBox with a spacer on the right side.

    @ pLabelSF_Logo = new QLabel;
    pLabelSF_Logo->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);

  • Lifetime Qt Champion

    Then you should rather create your own e.g QFrame and draw the image yourself so you can scale it as wanted

  • Thanks. At the moment I think thats a bit over my head and I will leave it for later. Thanks for all your input on this thread.

  • Lifetime Qt Champion

    You're welcome !

    If your primary problem is solved then please update the thread title prepending [solved] so other forum users may know as solution has been found :)

Log in to reply