Search code examples
c++qtqwidgetqscrollareaqframe

QScrollArea with custom QFrame


I'm using Qt 5.8. I have my custom QFrame (call it MyQFrame), which consists of several other QWidgets and can (and will) contain other instance of my custom MyQFrame. I set minimum size for MyQFrame. MyQFrame uses QGridLayout as layout.

This MyQFrame is placed inside of QScrollArea in QTabWidget. QScrollArea is set to be resizable (setWidgetResizable(true)). In MyQFrame I tried to set size policy to expanding (setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);) but it was without effect.

This is (abstraction) how I create QTabWidget and QScrollArea:

QTabWidget* tabWidget = new QTabWidget();
QScrollArea* firstTab = new QScrollArea();
firstTab->setWidgetResizable(true);
tabWidget->addTab(firstTab, "first tab");
MyQFrame* myFrame1 = new MyQFrame();
firstTab->setWidget(myFrame1);
// ... adding other MyQFrames inside this one using methods in MyQFrame ...

I cannot get scroll area working. Instead of expanding parents of nested MyQFrames and adding scroll bar is last child getting overlaped by other MyQFrames. Looks like I'm missing something like setWidgetResizable for MyQFrames, but this method is only in QScrollArea. I have no idea what should I try now. I'm still pretty new to this.

It should be more clear from image below.

overlaping


Solution

  • As I said, I use QGridLayout in MyQFrame. I had to set size constraint for this layout to minimum size like this: layout->setSizeConstraint(QLayout::SetMinimumSize);. Now it's working how I wanted.