Results 1 to 4 of 4

Thread: QGraphicsScene appears way too small only when QGraphicsView is embedded in QWidget

  1. #1
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default QGraphicsScene appears way too small only when QGraphicsView is embedded in QWidget

    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
    Qt Code:
    1. #include "test.h"
    2. #include "ui_test.h"
    3. #include "PlayerWidget.h"
    4. #include <QGraphicsProxyWidget>
    5.  
    6. test::test(QWidget *parent) :
    7. QWidget(parent),
    8. ui(new Ui::test),
    9. m_pokerScene{ nullptr },
    10. m_backgroundImage { new QGraphicsPixmapItem( QPixmap(":/resources/images/GreenTabletop 1920x1080.png")) },
    11. m_deck{ new Deck() }, //TODO: memory leak???
    12. m_pot{ 0 },
    13. m_deckPos{ QPointF(690, 150) },
    14. m_flopCard1Pos{ QPointF(430, 350) },
    15. m_flopCard2Pos{ QPointF(560, 350) },
    16. m_flopCard3Pos{ QPointF(690, 350) },
    17. m_turnCardPos{ QPointF(820, 350) },
    18. m_riverCardPos{ QPointF(950, 350) },
    19. m_tableName{ "" },
    20. m_maxPlayers{ 9 },
    21. m_buyInChipAmount{ 2000 },
    22. m_timeToAct{ 20000 }, // in milliseconds
    23. m_smallBlind{ 25 },
    24. m_bigBlind{ 50 }
    25. {
    26. ui->setupUi(this);
    27.  
    28. // Set up my QGraphicsScene of a poker table
    29. // and add items to it below
    30. m_pokerScene = new QGraphicsScene();
    31.  
    32. m_pokerScene->addItem(m_backgroundImage);
    33.  
    34. for (auto& card : m_deck->getDeck())
    35. {
    36. m_pokerScene->addItem(&card);
    37. card.setPos(m_deckPos);
    38. }
    39.  
    40. PlayerWidget* player1 = new PlayerWidget;
    41. QGraphicsProxyWidget *player1Proxy = m_pokerScene->addWidget(player1);
    42. player1Proxy->setPos(0,0);
    43.  
    44. m_deck->getDeck()[0].setPos(m_flopCard1Pos);
    45. m_deck->getDeck()[1].setPos(m_flopCard2Pos);
    46. m_deck->getDeck()[2].setPos(m_flopCard3Pos);
    47. m_deck->getDeck()[3].setPos(m_turnCardPos);
    48. m_deck->getDeck()[4].setPos(m_riverCardPos);
    49.  
    50. // tableArea is my QGraphicsView, embedded in this QWidget,
    51. // and is part of a Vertical Layout (see photo)
    52. ui->tableArea->setScene(m_pokerScene);
    53. ui->tableArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    54. ui->tableArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
    55.  
    56. // This call is where the problem seems to be,
    57. // below it are a few different ways I've tried so far
    58. // If I comment it out, the graphic appears zoomed in
    59. // by what looks like 50%, and I can scroll around to the normal edges
    60.  
    61. ui->tableArea->fitInView( m_pokerScene->sceneRect(), Qt::KeepAspectRatio );
    62.  
    63. // This one looks the closest to correct, but I don't know why! Still not right
    64. // ui->tableArea->fitInView( QRectF(0, 0, 1, 51), Qt::KeepAspectRatio );
    65.  
    66. // This one makes the graphic a little larger than the active, non-commented out call
    67. // ui->tableArea->fitInView( QRectF(0, 0, 900, 400), Qt::KeepAspectRatio );
    68.  
    69. // This one also shows the graphic zoomed in by what looks like 50%, and I can scroll
    70. // ui->tableArea->fitInView( QRectF(0, 0, 0, 0), Qt::KeepAspectRatio );
    71.  
    72. // This one has same result as the active, non-commented out call
    73. // ui->tableArea->fitInView( m_backgroundImage, Qt::KeepAspectRatio );
    74.  
    75.  
    76. // These last seven lines are here to show the same QGraphicsScene
    77. // in a new QGraphicsView that's unrelated to my poker widget
    78. // and works like I expect and want it to (see attached photos)
    79.  
    80. QGraphicsView* testView = new QGraphicsView;
    81. testView->setScene(m_pokerScene);
    82. testView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    83. testView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff );
    84. testView->fitInView( m_pokerScene->sceneRect(), Qt::KeepAspectRatio );
    85. testView->setWindowTitle("How It Should Look When Part Of My QWidget");
    86. testView->show();
    87. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsScene appears way too small only when QGraphicsView is embedded in QWidg

    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.
    Last edited by d_stranz; 15th April 2020 at 19:31.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    jzacks524 (15th April 2020)

  4. #3
    Join Date
    Apr 2020
    Posts
    7
    Thanks
    1
    Qt products
    Qt5

    Default Re: QGraphicsScene appears way too small only when QGraphicsView is embedded in QWidg

    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?Screen Shot 2020-04-15 at 11.02.43 AM.jpg

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QGraphicsScene appears way too small only when QGraphicsView is embedded in QWidg

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 15th April 2020, 05:56
  2. Replies: 2
    Last Post: 16th April 2013, 18:49
  3. Replies: 4
    Last Post: 28th February 2012, 12:04
  4. Replies: 1
    Last Post: 23rd February 2010, 09:29
  5. QWidget::pos Appears Unreliable
    By mbrusati in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2009, 18:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.