Results 1 to 10 of 10

Thread: Prevent QPixmap from being moved out of QGraphicsView

  1. #1
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Prevent QPixmap from being moved out of QGraphicsView

    I'd like to write very simple game.
    Here's a part of code:
    Qt Code:
    1. QGraphicsPixmapItem *pixmap=scene->addPixmap(QPixmap("img.png"));
    2. ui->graphicsView->setScene(scene);
    To copy to clipboard, switch view to plain text mode 
    pixmap is a creature that can be moved by player using WSAD keys.
    I did it by this way:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event){
    2. switch(event->key()){
    3. case Qt::Key_D:
    4. pixmap->setPos(pixmap->pos().x()+1, pixmap->pos().y());
    5. break;
    6. case Qt::Key_A:
    7. pixmap->setPos(pixmap->pos().x()-1, pixmap->pos().y());
    8. break;
    9. (...)
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 
    Now i want to prevent pixmap from being moved out of QGraphicsView.
    How can I do it?
    Thanks in advance.

  2. #2
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    try to use
    bool QGraphicsItem::collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const [virtual]

  3. #3
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    Thanks, but what should I pass as arguments of this function?
    Last edited by Macok; 12th March 2009 at 10:44.

  4. #4
    Join Date
    Mar 2009
    Posts
    3
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Prevent QPixmap from being moved out of QGraphicsView

    Hello, here are some thoughts:
    if you're making a sprite-based game, you should either:
    - have a QGraphicsScene that has the size of the game and use QGraphicsView scrollbars/scrolling to pan/zoom the scene
    - use a fixed Screen-size QGraphicsScene and do do your own code to 'scroll' the items (or the 'insert at right edga, scroll all items left, and remove at left edge type of thing)

    I assume you're using 1, so the QGraphicsScene has the size of the game area and your 'player' sprite is in there.
    In this case:

    A. when moving the player, just don't move it out of the scene
    QRect sceneRect = scene()->sceneRect().toRect();
    player->setPos(qBound(sceneRect.left(), player->pos(), sceneRect.right() - player_width));
    this keeps the X coordinate between scene rect boundaries (assuming the item has its origin in the top-left corner)

    B. the QGraphicsView can scroll to see the player
    just call player->ensureVisible(); and the view will scroll

    Good luck ;-)

  5. The following user says thank you to koral for this useful post:

    Macok (12th March 2009)

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    Take a look at QGraphicsItem::itemChange(). Notice the example.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    Macok (12th March 2009)

  8. #6
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    Thanks for the replies.

    @jpn
    Based on that example I wrote something like this:
    Qt Code:
    1. void MainWindow::keyPressEvent(QKeyEvent *event){
    2. switch(event->key()){
    3. case Qt::Key_D:
    4. pixmap->setPos(pixmap->pos().x()+5, pixmap->pos().y());
    5. break;
    6. case Qt::Key_A:
    7. pixmap->setPos(pixmap->pos().x()-5, pixmap->pos().y());
    8. break;
    9. case Qt::Key_W:
    10. pixmap->setPos(pixmap->pos().x(), pixmap->pos().y()-5);
    11. break;
    12. case Qt::Key_S:
    13. pixmap->setPos(pixmap->pos().x(), pixmap->pos().y()+5);
    14. break;
    15. }
    16.  
    17. if(!scene->sceneRect().contains(pixmap->pos())){
    18. box.setText("Player is out of the screen!");
    19. box.exec();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    But it doesn't work.
    I think the problem is that everytime I move pixmap, QGraphicsScene expands, so scene->sceneRect().contains(pixmap->pos()) will always return true.

  9. #7
    Join Date
    Dec 2008
    Location
    Czech
    Posts
    44
    Thanks
    2
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    bool QGraphicsItem::collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const [virtual]
    as a QGraphicsItem you can pass QGraphicsRectItem(scene->sceneRect())
    It means that you create graphics item with sboundary same as of graphicsscene has and then you check if your pixmap icollides with this RectItem

  10. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    Yes, the scene will expand automatically by default, but shouldn't you define the game board area if that's the desired behavior? Something like:
    Qt Code:
    1. void MyGraphicsView::resizeEvent(QResizeEvent* event)
    2. {
    3. QGraphicsview::resizeEvent(event);
    4. if (scene())
    5. scene()->setSceneRect(QRectF(QPointF(0,0), event->size()));
    6. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  11. #9
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    When I reimplemented QGraphicsView::resizeEvent program crashes imidiately after being started.
    In QtCreator's console I have:
    The program has unexpectedly finished.

  12. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Prevent QPixmap from being moved out of QGraphicsView

    See the backtrace from the debugger.
    J-P Nurmi

Similar Threads

  1. how to prevent QGraphicsView() resizable by user
    By wagmare in forum Qt Programming
    Replies: 3
    Last Post: 24th February 2009, 14:47

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.