QScrollArea respektieren nicht contentMargins Einstellung
-
27-10-2019 - |
Frage
QScrollArea , aus irgendeinem Grund, ignoriert die contentMargins
Einstellung, wenn ich Set QGraphicsView als Widget. Mit Blick auf das Snippet unten, kann jemand bitte sagen, ob ich etwas falsch mache oder es könnte ein Fehler in der SDK sein?
Snippet 1 (funktioniert perfekt):
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 (Es ist wie setContentMargins () Anruf ignoriert vollständig ):
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();
Danke.
Lösung
Es sieht aus wie Sie die Struktur, wie man Nest einer QGraphicsView und einem QGraphicsScene sind verwirrend. (Vielleicht war es nur ein Tippfehler?)
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(sa), sa);
sollte geändert werden
QGraphicsView *widgetToScroll = new QGraphicsView(new QGraphicsScene(), sa);
oder
QGraphicsView *widgetToScroll = new QGraphicsView();
sa->setWidget(widgetToScroll);
Wenn Sie eine QWidget zu einem Layout hinzufügen, ändern Sie das übergeordnete Widget. Wenn Sie ein Widget (oder QGraphicsView) zu einem QScrollArea gesetzt, Sie, dass Eltern-Widget ändern. Siehe Objekt Bäume & Eigentümer für weitere Informationen. Also wollten, wenn Sie Ihre QGraphicsView in einem QScrollArea, um Ihren Code würde wie folgt aussehen:
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();
Als Randnotiz ...
Wenn QGraphicsViews mit einem QGraphicsScene verwenden, anstatt die Ränder der Einrichtung eines QScrollArea des setContentsMargins verwenden, verwende ich die QGraphicsView automatische Scrollen und gerade die Szene rect setzen einen größeren Spielraum zu haben, dass die Größe meines Inhalt wie folgt:
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();
Die QGraphicsView enthält ziemlich viel mehr als nur automatische Scrollen, wenn nötig. Sie können alles in der es die Größe und ein bisschen mehr. Es ist für 2D-Layout, Interaktionen und Animationen. Siehe Qt Grafik-Ansicht-Framework unter http://doc.qt.io/qt-5/ graphicsview.html für weitere Informationen.
Hier gibt es mehr Informationen, die nützlich sein kann, wenn die Margen und Polsterungen mit: Das Box-Modell von QStyleSheets verwendet .
Andere Tipps
To make the content margins work properly for a QScrollArea widget I subclass it and manually set the viewport margins (which is a protected method in QT 4.7)
// Extended class
class QScrollAreaWithMargins : public QScrollArea
{
public:
virtual void resizeEvent(QResizeEvent *event) override
{
// Define content margins here
setViewportMargins(5, 0, 0, 0); // <<<<< SET MARGINS HERE
QScrollArea::resizeEvent(event);
}
};
// Usage
//...
mEditorScrollArea = new QScrollAreaWithMargins();
//...