atsui
atsui

Reputation: 1018

How can I enforce a 50/50 layout of two QGraphicsViews side-by-side?

I'm trying to lay out two QGraphicsView widgets side by side with a grid layout in Qt Designer like so, but the view unexpectedly resizes on scale and translate calls:

The QGraphicsView is changing when I scale.

I'm not sure what my options are for enforcing a 50/50 split. In Qt Designer, the most relevant setting on the QGraphicsView looks like setting a QSizePolicy, but the QGraphicsView still changes size when I set this to fixed for the horizontal policy.

enter image description here

I think it's clear what I'm trying to do, but maybe my approach is wrong. Should I be using a different layout? Maybe there's something I can do with the QGraphicsView itself?

For reference, here is the relevant part of what is being generated from the .ui file:

void setupUi(QMainWindow* MainWindow)
{
    ...
    QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    sizePolicy.setHorizontalStretch(0);
    sizePolicy.setVerticalStretch(0);
    sizePolicy.setHeightForWidth(MainWindow->sizePolicy().hasHeightForWidth());
    MainWindow->setSizePolicy(sizePolicy);

    centralwidget = new QWidget(MainWindow);
    gridLayout = new QGridLayout(centralwidget);
    graphicsView = new QGraphicsView(centralwidget);
    sizePolicy.setHeightForWidth(graphicsView->sizePolicy().hasHeightForWidth());
    graphicsView->setSizePolicy(sizePolicy);
    gridLayout->addWidget(graphicsView, 0, 0, 1, 1);
    graphicsView2 = new QGraphicsView(centralwidget);
    sizePolicy.setHeightForWidth(graphicsView2->sizePolicy().hasHeightForWidth());
    graphicsView2->setSizePolicy(sizePolicy);
    gridLayout->addWidget(graphicsView2, 0, 1, 1, 1);
    MainWindow->setCentralWidget(centralwidget);
    ...
}

Upvotes: 2

Views: 890

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73379

If you want the two views to always share the horizontal space equally, then this is probably all you need to do:

graphicsView->setSizePolicy( QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
graphicsView2->setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));

Upvotes: 1

Related Questions