Results 1 to 4 of 4

Thread: Move Rectangle

  1. #1
    Join Date
    Jan 2016
    Location
    Oberhausen, Germany
    Posts
    5
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Move Rectangle

    hello,

    I currently devoloping a point and click adventure as school project.
    But I have a problem with the MouseMoveEvent.

    I don't know how to implement that, the rectangle moves where I click with the mouse.

    I hope someone understands my problem.

    Thanks in advance

    Best regards

    Basti1990

    PS code:

    Qt Code:
    1. // HEADER FILE
    2.  
    3. #ifndef GAME_H
    4. #define GAME_H
    5.  
    6. #include <QEvent>
    7. #include <QKeyEvent>
    8. #include <QApplication>
    9. #include <QGraphicsRectItem>
    10. #include <QGraphicsScene>
    11. #include <QGraphicsView>
    12. #include <QPainter>
    13. #include <QPointer>
    14. #include <QVariant>
    15. #include <QDebug>
    16. #include <QBrush>
    17. #include <QPen>
    18.  
    19.  
    20.  
    21.  
    22. class Game : public QWidget
    23. {
    24. Q_OBJECT
    25.  
    26. public:
    27. explicit Game(QWidget *parent = 0);
    28. ~Game();
    29.  
    30. private:
    31. QGraphicsRectItem *rectangle;
    32.  
    33. protected:
    34. void mouseMoveEvent(QMouseEvent *mousevent);
    35.  
    36. };
    37.  
    38. #endif // GAME_H
    39.  
    40.  
    41. // CPP FILE
    42.  
    43. #include "game.h"
    44.  
    45. Game::Game(QWidget *parent)
    46. : QWidget(parent)
    47. {
    48. scene = new QGraphicsScene;
    49. view = new QGraphicsView;
    50.  
    51. view->setScene(scene);
    52.  
    53. rectangle = new QGraphicsRectItem;
    54. rectangle->setRect(0,0,100,100);
    55. QPen blackpen(Qt::black);
    56. blackpen.setWidth(6);
    57.  
    58. rectangle->setPen(blackpen);
    59. rectangle->setBrush(QBrush(Qt::white));
    60.  
    61. rectangle->setFlag(QGraphicsItem::ItemIsFocusable);
    62. rectangle->setFocus();
    63.  
    64. scene->addItem(rectangle);
    65. scene->setBackgroundBrush(Qt::blue);
    66.  
    67. // view->setWindowFlags(Qt::FramelessWindowHint);
    68. view->show();
    69. }
    70.  
    71. Game::~Game()
    72. {}
    73.  
    74.  
    75.  
    76. void Game::mouseMoveEvent(QMouseEvent *mousevent)
    77. {
    78. // SomeThing ?
    79. }
    80.  
    81.  
    82. // MAIN FILE
    83.  
    84. #include "game.h"
    85. #include "menue.h"
    86. #include <QApplication>
    87.  
    88. int main(int argc, char *argv[])
    89. {
    90. QApplication a(argc, argv);
    91. Game w;
    92. w.setVisible(false);
    93.  
    94. return a.exec();
    95. }
    To copy to clipboard, switch view to plain text mode 

    PPS sorry for my terrible english

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle

    It might be better to implement the mouse handling in the graphicsview or in the scene.
    In the former case you can map the mouse coordinates of the view manually to the scene, in the latter that has already been taken care of.

    Once you have the cooridinates of the click in scene coordinates, you can use rectangle->setPos() to move it to that position

    Cheers,
    _

  3. #3
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Move Rectangle

    • You have hidden your Game widget.
    • Your graphicsView does not have a parent, so it is the top level widget.
    • You are implementing mouseMoveEvent on the Game widget (which is hidden!) so of course you won't receive the event.



    First thing:
    • Replace w.setVisible(false) from your main function with w.show()
    • Make graphics view the child of Game widget. i.e. replace view = new QGraphicsView; with view = new QGraphicsView(this); If you do this you don't need to show the view manually as you are currently doing.



    Once the parent child hierarchy is fixed, you should start getting the mouse events and you can move your rectangle!

    Regards
    Vikram

  4. #4
    Join Date
    Jan 2016
    Location
    Oberhausen, Germany
    Posts
    5
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Move Rectangle

    Hi,
    Thanks for the quick replies,
    I'll take care about this.

    I'll report me later.

    Best regards
    Basti1990

Similar Threads

  1. use of Rectangle javascript
    By ravandi in forum Qt Quick
    Replies: 15
    Last Post: 23rd February 2016, 09:46
  2. Replies: 2
    Last Post: 25th December 2014, 16:15
  3. Problem in Move Move Event Handler.
    By redgoof in forum Qt Programming
    Replies: 0
    Last Post: 7th April 2010, 12:45
  4. Getting the bounding rectangle
    By ioannis in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2009, 01:41
  5. Move Rectangle on mouse Move
    By vermarajeev in forum Qt Programming
    Replies: 24
    Last Post: 14th May 2007, 06:34

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.