Results 1 to 6 of 6

Thread: QGraphicsScene and View

  1. #1
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default QGraphicsScene and View

    Hello guys, this is yet another topic about coordinates.
    im making a tetris game and i'm having some trouble setting up the scene.
    i'm aware of the documentation, but learning another library on my own is quite new to me, and im kind of lost in all those words.

    consider the following code

    Qt Code:
    1. myRect::myRect(QGraphicsItem *parent) : QGraphicsItem(parent)
    2. {
    3. setFlag(QGraphicsItem::ItemIsFocusable);
    4. }
    5.  
    6. QRectF myRect::boundingRect() const
    7. {
    8. return QRectF(0,0,50,50);
    9. }
    10.  
    11. void myRect::keyPressEvent(QKeyEvent *event)
    12. {
    13. switch( event->key())
    14. {
    15. case Qt::Key_Left:{
    16. setPos(x()-10,y());
    17. checkBounds();
    18. break;
    19. }
    20. case Qt::Key_Right:{
    21. setPos(x()+10,y());
    22. checkBounds();
    23. break;
    24. }
    25. case Qt::Key_Up:{
    26. setPos(x(),y()-10);
    27. checkBounds();
    28. break;
    29. }
    30. case Qt::Key_Down:{
    31. setPos(x(),y()+10);
    32. checkBounds();
    33. break;
    34. }
    35. }
    36. update();
    37. }
    38.  
    39. void myRect::checkBounds()
    40. {
    41. if (x() < 0)
    42. {
    43. setPos(0, y());
    44. }
    45. else if (x() + boundingRect().right() > scene()->width())
    46. {
    47. setPos(scene()->width() - boundingRect().width(), y());
    48. }
    49.  
    50. if (y() < 0)
    51. {
    52. setPos(x(), 0);
    53. }
    54. else if ( y()+ boundingRect().bottom() > scene()->height())
    55. {
    56. setPos(x(), scene()->height() - boundingRect().height());
    57. }
    58. }
    59.  
    60. void myRect::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    61. {
    62. painter->setBrush(Qt::yellow);
    63. painter->drawRoundRect(0,0,50,50,5,5);
    64. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->setupUi(this);
    6. scene = new QGraphicsScene();
    7. ui->view->setScene(scene);
    8. scene->setSceneRect(0,0,ui->view->frameSize().width(),
    9. ui->view->frameSize().height());
    10. rect = new myRect;
    11. scene->addItem(rect);
    12. rect->setPos(scene->width()/2,0);
    13. rect->setFocus();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    so my question(s) is this.

    i'm under the impresion my scene should have the same dimensions and the view.
    but when i add my item on the scene it aint possitioned in the middle.
    can someone explain to me whats going on here?

    also.

    lets say i want to have 8 columns on my board and 16 rows for block movement.

    so my I block could be placed 2 times horizontally rotated on the board, and 8 times vertically.

    example.jpg

    excuse me for my paint skills.

    can you help me out with setting that up?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene and View

    The proper size for your scene is more likely ui->view->viewport()->size(). Just remember widgets get their size set only after they become visible for the first time. So you need to set the scene size when the view is visible, e.g. during the view's resizeEvent. Alternatively make your scene have fixed size and scale it to the view in resize event using scaleToFit().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: QGraphicsScene and View

    Hmm, well tbh that didn't help me much.

    I used my designer to make the widget fixed size, and used the designer as well to create a QGraphicsview, which i also made fixed size.
    So i wont have any resize events afaik.

    for example my widget is 300x520,
    and when i do
    scene->setSceneRect(0,0,ui->view->frameSize().width(),ui->view->frameSize().height());

    i have a feeling my scene aint identical to my view.

    this game will be as blunt as possible, so i dont need any magic here.

    can you just help me understand how to set up the board as i mentioned in the first post?

    thx

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene and View

    Quote Originally Posted by bikonja View Post
    I used my designer to make the widget fixed size, and used the designer as well to create a QGraphicsview, which i also made fixed size.
    So i wont have any resize events afaik.
    You will have at least one.

    scene->setSceneRect(0,0,ui->view->frameSize().width(),ui->view->frameSize().height());
    Because you ignored what I have written in my previous post. After construction the widget size is invalid. So it's frame size is likely invalid as well. You had checked it before writing this post, right?

    can you just help me understand how to set up the board as i mentioned in the first post?
    I have already done that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Re: QGraphicsScene and View

    yeah, thx alot.

    you didn't answer neither of my questions in a precise answer. you are quoting me documentaion which i mentioned i have problems reading with.

    so no, you didnt allready explain it to me.


    Added after 38 minutes:


    also i can't find scaleToFit() anywhere.
    Last edited by bikonja; 13th February 2015 at 18:14.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsScene and View

    Quote Originally Posted by bikonja View Post
    yeah, thx alot.

    you didn't answer neither of my questions in a precise answer. you are quoting me documentaion which i mentioned i have problems reading with.

    so no, you didnt allready explain it to me.
    Did you implement resizeEvent() for your view? Did you check the actual size of the scene?

    I gave you a precise recipe what to do to make your code work (set the scene size to the size of the viewport from within a resize event). And I didn't quote any documentation, I was writing that post from my tablet while riding in a bus, quoting documentation in such conditions is not that comfortable.


    Added after 38 minutes:


    also i can't find scaleToFit() anywhere.
    Sorry, that is fitInView(), I was writing the method name from the top of my head. I assumed that even if I mistyped the name, you'd find the right name anyway as it is the only method which has "fit" in its name.

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. #define SCALETOFIT
    4.  
    5. class GraphicsView : public QGraphicsView {
    6. public:
    7. GraphicsView() : QGraphicsView() { item = 0; }
    8. protected:
    9. void resizeEvent(QResizeEvent *re) {
    10. #ifndef SCALETOFIT
    11. scene()->setSceneRect(QRect(QPoint(0,0), viewport()->size()));
    12. if(!item) {
    13. item = scene()->addRect(QRect(0,0,50,50), QPen(Qt::black), QBrush(Qt::red));
    14. item->setPos(sceneRect().width()/2, 0);
    15. }
    16. #else
    17. if(!item) {
    18. item = scene()->addRect(QRect(0, 0, 1, 1), Qt::NoPen, QBrush(Qt::red));
    19. item->setPos(3, 0);
    20. }
    21. fitInView(sceneRect(), Qt::KeepAspectRatio);
    22. #endif
    23. }
    24. void keyPressEvent(QKeyEvent *ke) {
    25. if(!item) return;
    26. switch(ke->key()) {
    27. case Qt::Key_Right: if(item->x() < sceneRect().width()-1) item->moveBy(1, 0); break;
    28. case Qt::Key_Left: if(item->x() > 0) item->moveBy(-1, 0); break;
    29. case Qt::Key_Up: if(item->y() > 0) item->moveBy(0, -1); break;
    30. case Qt::Key_Down: if(item->y() < sceneRect().height()-1) item->moveBy(0, 1); break;
    31. default: break;
    32. }
    33. }
    34. private:
    35. };
    36.  
    37. int main(int argc, char **argv) {
    38. QApplication app(argc, argv);
    39. GraphicsView view;
    40. view.setScene(new QGraphicsScene);
    41. #ifdef SCALETOFIT
    42. view.scene()->setSceneRect(QRect(0, 0, 8, 16));
    43. #endif
    44. view.show();
    45. return app.exec();
    46. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 27th January 2013, 17:42
  2. Fixed QGraphicsScene in view
    By jano_alex_es in forum Newbie
    Replies: 1
    Last Post: 10th May 2012, 01:29
  3. QGraphicsScene and View scrolling problem
    By ithinkso in forum Newbie
    Replies: 2
    Last Post: 2nd June 2011, 11:35
  4. Replies: 1
    Last Post: 19th April 2011, 14:53
  5. Problems with setCursor in QGraphicsScene/View
    By lightning2911 in forum Qt Programming
    Replies: 3
    Last Post: 26th August 2010, 11:04

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.