SparkyRaccoon
SparkyRaccoon

Reputation: 51

QScrollArea messing up with QGridLayout : QGridLayout hidden and no scroll

I am trying to make a QGridLayout scrollable. It may contain several custom widgets, the number of widgets is not fixed. QGridLayout must be scrollable when there are more than x widgets, x being an arbitrary number.

The problem is, when I use QScrollArea, QScrollArea seems to hide the whole layout (only the background color of scroll area is shown). When I use QGridLayout alone, my view is - of course - not scrollable but everything works as it should do.

I am probably missing something, my guesses are:

Here's a piece of the involved code :

    QScrollArea *scrollArea = new QScrollArea;
    QWidget *resultsPage = new QWidget;
    booksGrid = new QGridLayout;
    booksGrid->setSizeConstraint(QLayout::SetMinAndMaxSize);
    resultsPage->setLayout(booksGrid);
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(resultsPage);
    mainWidget->addWidget(scrollArea);

Also, booksGrid is declared as a class attribute, mainWidget is a QStackedWidget.

Any help is welcomed, let me know if you need more information !

Upvotes: 2

Views: 1639

Answers (2)

SparkyRaccoon
SparkyRaccoon

Reputation: 51

The solution - resultsPage was a personalized widget containing several other widgets, with unspecified sizes, arranged in a QGridLayout.

Widgets were encapsulated like this : QMainWidget -> QScrollArea -> personalized QWidget resultsPage -> QGridLayout -> personalized QWidgets result(s) with unspecified size

In the end the only thing I had to do was to set a fixed size in the constructor of the QWidget result with setFixedSize(int w, int h);

Upvotes: 1

Scheff's Cat
Scheff's Cat

Reputation: 20141

As I couldn't see anything suspicious in your code fragment, I made an MCVE to reproduce your issue:

#include <QtWidgets>

int main(int argc, char **argv)
{
  qDebug() << "Qt Version: " << QT_VERSION_STR;
  // main application
  QApplication app(argc, argv);
  // setup GUI
  QMainWindow qWin;
  QScrollArea qScrArea;
  QWidget qScrView;
  QGridLayout qGrid;
  enum { nCols = 4 };
#define MAKE_LABEL(I) \
  QLabel qLbl##I(QString::fromUtf8("Label "#I)); \
  qGrid.addWidget(&qLbl##I, I / nCols, I % nCols)
  MAKE_LABEL(0);  MAKE_LABEL(1);  MAKE_LABEL(2);  MAKE_LABEL(3);  MAKE_LABEL(4);
  MAKE_LABEL(5);  MAKE_LABEL(6);  MAKE_LABEL(7);  MAKE_LABEL(8);  MAKE_LABEL(9);
  MAKE_LABEL(10); MAKE_LABEL(11); MAKE_LABEL(12); MAKE_LABEL(13); MAKE_LABEL(14);
  MAKE_LABEL(15); MAKE_LABEL(16); MAKE_LABEL(17); MAKE_LABEL(18); MAKE_LABEL(19);
  MAKE_LABEL(20); MAKE_LABEL(21); MAKE_LABEL(22); MAKE_LABEL(23); MAKE_LABEL(24);
  MAKE_LABEL(25); MAKE_LABEL(26); MAKE_LABEL(27); MAKE_LABEL(28); MAKE_LABEL(29);
  MAKE_LABEL(30); MAKE_LABEL(31); MAKE_LABEL(32); MAKE_LABEL(33); MAKE_LABEL(34);
  MAKE_LABEL(35); MAKE_LABEL(36); MAKE_LABEL(37); MAKE_LABEL(38); MAKE_LABEL(39);
#undef MAKE_LABEL
  qScrView.setLayout(&qGrid);
  qScrArea.setWidget(&qScrView);
  qWin.setCentralWidget(&qScrArea);
  qWin.show();
  // run-time loop
  return app.exec();
}

Compiled and tested in VS2013, Qt 5.9.2 on Windows 10 (64 bit):

a) Snapshot of testQScrollArea-Widget (after start) b) Snapshot of testQScrollArea-Widget (after resize) c) Snapshot of testQScrollArea-Widget (after scroll)

The snapshots are taken after start (a), after resizing (b), and after scrolling (c).

For me, everything looks and works like expected.

You may compile and test the sample on your side as well. If it shows the same broken behavior like your application then something is wrong in your Qt version (otherwise something in your application).

Upvotes: 1

Related Questions