Consider following simple example:
Area.hh
#pragma once
class Area;
#include <QScrollArea>
class Area : public QScrollArea {
Q_OBJECT
public:
Area (QWidget *_parent = 0);
};
Area.cc
#include "main.hh"
#include "Area.hh"
#include <QLabel>
Area::Area (QWidget *_parent) :
QScrollArea (_parent)
{
QLabel *label = new QLabel ("Show me please");
setWidget (label);
}
This scroll area should show a label inside it. And it does so well if you just create an Area object and show it like this:
Area *area = new Area();
area->show();
However, if you add a QScrollArea
with Qt Creator and promote it to Area
class, then it shows nothing inside and there are no scrollbars. What can I do to show it properly?
Qt Designer adds an empty widget inside the QScrollArea
, overwriting yours.
To prevent that, use a base QWidget
instead of a QScrollArea
, and promote that widget to an Area
class. Qt's Ui compiler won't considered it to be a QScrollArea
, so it won't generate a call to setWidget
anymore.