PDA

View Full Version : QGraphicsScene appears way too small only when QGraphicsView is embedded in QWidget



jzacks524
15th April 2020, 15:07
Hello, any Qt gurus out there who can help me figure out what I'm doing wrong here? I've been reading the documentation and searching online but have been banging my head against the wall with this for a few hours...

I'm working on a little poker app, and I made a window (my class name "test" derived from QWidget) using QT Designer that has a QGraphicsView embedded in it, along with my control buttons underneath. The graphics area and control/chat area are laid out together in a Vertical Layout.

I've added a background image (1920x1080p) and a few playing cards to the scene, but when I try and display it on my widget while calling fitInView(), the whole scene appears very tiny and centered (see attached photos).

As a test, in my widget constructor I made a totally unrelated QGraphicsView set to the exact same scene, and this one works as expected (see photos) while using fitInView().

I'm hoping there is something simple that I'm misunderstanding and would really appreciate some help/advice!

Below is my widget constructor code along with some photos:

test.cpp

#include "test.h"
#include "ui_test.h"
#include "PlayerWidget.h"
#include <QGraphicsProxyWidget>

test::test(QWidget *parent) :
QWidget(parent),
ui(new Ui::test),
m_pokerScene{ nullptr },
m_backgroundImage { new QGraphicsPixmapItem( QPixmap(":/resources/images/GreenTabletop 1920x1080.png")) },
m_deck{ new Deck() }, //TODO: memory leak???
m_pot{ 0 },
m_deckPos{ QPointF(690, 150) },
m_flopCard1Pos{ QPointF(430, 350) },
m_flopCard2Pos{ QPointF(560, 350) },
m_flopCard3Pos{ QPointF(690, 350) },
m_turnCardPos{ QPointF(820, 350) },
m_riverCardPos{ QPointF(950, 350) },
m_tableName{ "" },
m_maxPlayers{ 9 },
m_buyInChipAmount{ 2000 },
m_timeToAct{ 20000 }, // in milliseconds
m_smallBlind{ 25 },
m_bigBlind{ 50 }
{
ui->setupUi(this);

// Set up my QGraphicsScene of a poker table
// and add items to it below
m_pokerScene = new QGraphicsScene();

m_pokerScene->addItem(m_backgroundImage);

for (auto& card : m_deck->getDeck())
{
m_pokerScene->addItem(&card);
card.setPos(m_deckPos);
}

PlayerWidget* player1 = new PlayerWidget;
QGraphicsProxyWidget *player1Proxy = m_pokerScene->addWidget(player1);
player1Proxy->setPos(0,0);

m_deck->getDeck()[0].setPos(m_flopCard1Pos);
m_deck->getDeck()[1].setPos(m_flopCard2Pos);
m_deck->getDeck()[2].setPos(m_flopCard3Pos);
m_deck->getDeck()[3].setPos(m_turnCardPos);
m_deck->getDeck()[4].setPos(m_riverCardPos);

// tableArea is my QGraphicsView, embedded in this QWidget,
// and is part of a Vertical Layout (see photo)
ui->tableArea->setScene(m_pokerScene);
ui->tableArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
ui->tableArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );

// This call is where the problem seems to be,
// below it are a few different ways I've tried so far
// If I comment it out, the graphic appears zoomed in
// by what looks like 50%, and I can scroll around to the normal edges

ui->tableArea->fitInView( m_pokerScene->sceneRect(), Qt::KeepAspectRatio );

// This one looks the closest to correct, but I don't know why! Still not right
// ui->tableArea->fitInView( QRectF(0, 0, 1, 51), Qt::KeepAspectRatio );

// This one makes the graphic a little larger than the active, non-commented out call
// ui->tableArea->fitInView( QRectF(0, 0, 900, 400), Qt::KeepAspectRatio );

// This one also shows the graphic zoomed in by what looks like 50%, and I can scroll
// ui->tableArea->fitInView( QRectF(0, 0, 0, 0), Qt::KeepAspectRatio );

// This one has same result as the active, non-commented out call
// ui->tableArea->fitInView( m_backgroundImage, Qt::KeepAspectRatio );


// These last seven lines are here to show the same QGraphicsScene
// in a new QGraphicsView that's unrelated to my poker widget
// and works like I expect and want it to (see attached photos)

QGraphicsView* testView = new QGraphicsView;
testView->setScene(m_pokerScene);
testView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
testView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
testView->fitInView( m_pokerScene->sceneRect(), Qt::KeepAspectRatio );
testView->setWindowTitle("How It Should Look When Part Of My QWidget");
testView->show();
}

d_stranz
15th April 2020, 16:03
You need to implement resizeEvent() for your test widget and move the fitInView() call to there. When your widget is first being constructed (and before it is shown via show()) it has no size, so fitInView() there does nothing. In your resizeEvent() you should also test isVisible() before trying to resize the view.

I don't know if you have a layout on your "test" widget, but that's another thing you'll need if you want resizing to work properly.

jzacks524
15th April 2020, 17:12
Awesome, thank you very much, this seems to do the trick (once I take now care of the apparently infamous "white border problem" and only allow the user to resize the window only diagonally according to the aspect ratio).

When you say "I don't know if you have a layout on your 'test' widget" - I think I do, do you mean on the top level in the Qt Designer (as show in the screenshot below)? Or that I should have a parent for "test", which I don't right now?13394

d_stranz
15th April 2020, 18:31
The QHBoxLayout you have at the top of the hierarchy is the layout for the entire test widget, so you're good. When you resize the test widget, the layout will cause everything in it to resize as well.