Results 1 to 3 of 3

Thread: resize Rectangle using mouse events

  1. #1
    Join Date
    Jul 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default resize Rectangle using mouse events

    Hi

    I'm trying to resize a rectangle and move it with the mouse.

    at the moment i managed to move it with the mouse but i have a little problem resizing it.

    When I resize it from the top of the rectangle, I have no problem but when I resize it from the bottom, I manage to do it as long as I don't release the mouse button but once release it, I can't resize it anymore until I move the rectangle.

    In addition, there are traces in the window when I decrease the height of the bottom rectangle.

    can you help me find where the bug is coming from?


    main.cpp
    Qt Code:
    1. #include "fenetre.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Fenetre w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    fenetre.h

    Qt Code:
    1. #ifndef FENETRE_H
    2. #define FENETRE_H
    3.  
    4. #include <QGraphicsScene>
    5. #include <QMainWindow>
    6.  
    7. #include "myrectangle.h"
    8. #include <QGraphicsView>
    9.  
    10. #include <QMouseEvent>
    11. #include <QObject>
    12.  
    13. namespace Ui {
    14. class Fenetre;
    15. }
    16.  
    17. class Fenetre : public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit Fenetre(QWidget *parent = nullptr);
    23. ~Fenetre();
    24.  
    25. bool eventFilter(QObject *obj, QEvent *event);
    26.  
    27. protected:
    28.  
    29.  
    30. private:
    31. Ui::Fenetre *ui;
    32.  
    33.  
    34. MyRectangle *rectangle;
    35.  
    36. signals:
    37. void clickedMouse(int x, int y);
    38. };
    39.  
    40. #endif // FENETRE_H
    To copy to clipboard, switch view to plain text mode 

    fenetre.cpp

    Qt Code:
    1. #include "fenetre.h"
    2. #include "ui_fenetre.h"
    3.  
    4. Fenetre::Fenetre(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::Fenetre)
    7. {
    8. ui->setupUi(this);
    9.  
    10. QBrush greenBrush(Qt::green);
    11. QBrush blueBrush(Qt::blue);
    12. QPen pen(Qt::black);
    13. pen.setWidth(2);
    14.  
    15. rectangle = new MyRectangle ();
    16.  
    17. QColor c (242,251,235);
    18. QBrush brush (c, Qt::SolidPattern);
    19.  
    20. scene = new QGraphicsScene(this);
    21. scene->setBackgroundBrush(brush);
    22. scene->addItem(rectangle);
    23.  
    24. view = new QGraphicsView(scene);
    25. scene->setSceneRect(0, 0, 400, 400);
    26. ui->graphicsView->setScene(scene);
    27. view->viewport()->installEventFilter(this);
    28.  
    29. setCentralWidget(view);
    30. view->show();
    31.  
    32. connect(this, SIGNAL(clickedMouse(int, int)), rectangle, SLOT(recevedClickedMouse(int, int)));
    33. }
    34.  
    35. Fenetre::~Fenetre()
    36. {
    37. delete ui;
    38. }
    39.  
    40. bool Fenetre::eventFilter(QObject *obj, QEvent *event)
    41. {
    42. if (event->type() == QEvent::MouseMove)
    43. {
    44. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    45. statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
    46.  
    47. // emit clickedMouse(mouseEvent->pos().x(), mouseEvent->pos().y());
    48. }
    49. else if (event->type() == QEvent::MouseButtonPress)
    50. {
    51. QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
    52. statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
    53.  
    54. QPoint pointRec2 = view->mapFromScene(rectangle->pos().x(), rectangle->pos().y());
    55. qDebug() << "OKKKKKK 2222 BBBBBB view->mapFromScene(myRect.x() = " << pointRec2.x() << "myRect.y() " << pointRec2.y(); //okkk IMPORTANT
    56.  
    57. ///////////////////////////
    58. emit clickedMouse(mouseEvent->pos().x(), mouseEvent->pos().y());
    59. }
    60.  
    61. return false;
    62. }
    To copy to clipboard, switch view to plain text mode 

    myrectangle.h

    Qt Code:
    1. #ifndef MYRECTANGLE_H
    2. #define MYRECTANGLE_H
    3.  
    4. #include <QPainter>
    5. #include <QGraphicsItem>
    6. #include <QDebug>
    7.  
    8. #include <QApplication>
    9. #include <QColor>
    10. #include <QPen>
    11.  
    12. #include <QGraphicsSceneHoverEvent>
    13. #include <QObject>
    14.  
    15. class MyRectangle : public QObject, public QGraphicsItem
    16. {
    17. Q_OBJECT
    18.  
    19. public:
    20. MyRectangle();
    21. QRectF boundingRect() const;
    22. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    23.  
    24. QColor color; // the hover event handlers will toggle this between red and black
    25. QPen pen; // the pen is used to paint the red/black border
    26.  
    27. private:
    28. qreal width;
    29. qreal height;
    30.  
    31. int mouseDownX;
    32. int mouseDownY;
    33.  
    34. bool pressedToSize;
    35. int avant = 0;
    36.  
    37. protected:
    38. void mousePressEvent(QGraphicsSceneMouseEvent *event);
    39. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
    40. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
    41.  
    42. void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    43. void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
    44. void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    45.  
    46. protected slots:
    47. void recevedClickedMouse(int x, int y);
    48. };
    49. #endif // MYRECTANGLE_H
    To copy to clipboard, switch view to plain text mode 

    myrectangle.cpp

    Qt Code:
    1. #include "myrectangle.h"
    2.  
    3. MyRectangle::MyRectangle()
    4. {
    5. width = 100;
    6. height = 50;
    7.  
    8. pressedToSize = false;
    9. setFlag(ItemIsMovable);
    10.  
    11. setAcceptHoverEvents(true);
    12. color = Qt::yellow;
    13. pen.setColor(color);
    14. }
    15.  
    16. QRectF MyRectangle::boundingRect() const
    17. {
    18. return QRectF(0, 0, width, height);
    19. }
    20.  
    21. void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    22. {
    23. QRectF rec = boundingRect();
    24. QBrush brush(Qt::blue);
    25.  
    26. if(pressedToSize)
    27. {
    28. brush.setColor(color);
    29. }
    30. else
    31. {
    32. brush.setColor(color);
    33. }
    34.  
    35. painter->fillRect(rec, brush);
    36. painter->drawRect(rec);
    37. }
    38.  
    39.  
    40. void MyRectangle::mousePressEvent(QGraphicsSceneMouseEvent *event)
    41. {
    42. if(event->pos().y() < 10 || event->pos().y() > height - 10)
    43. {
    44. pressedToSize = true;
    45. QApplication::setOverrideCursor(Qt::SizeVerCursor);
    46. mouseDownX = event->pos().x();
    47. mouseDownY = event->pos().y();
    48. }
    49. else
    50. {
    51. QApplication::setOverrideCursor(Qt::OpenHandCursor);
    52. }
    53. update();
    54.  
    55. QGraphicsItem::mousePressEvent(event);
    56. }
    57.  
    58. void MyRectangle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    59. {
    60. if(pressedToSize)
    61. {
    62. if((mouseDownY <= 10)) // En haut du rectangle
    63. {
    64. qDebug() << "haut ";
    65. height -= event->pos().y();
    66. // setPos(pos().x() , pos().y() + event->pos().y() );
    67. moveBy(0, event->pos().y());
    68. }
    69.  
    70. else if( (avant >= height -10)) // En bas du rectangle
    71. {
    72. qDebug() << "bas ";
    73. height = event->pos().y();
    74. // setPos(pos().x() , pos().y());
    75. // moveBy(0, 0);
    76. }
    77. }
    78.  
    79. avant = event->pos().y();
    80. update();
    81. if(!pressedToSize)
    82. {
    83. qDebug() << "!!!!!pressedToSize ";
    84. QGraphicsItem::mouseMoveEvent(event);
    85. }
    86. }
    87.  
    88. void MyRectangle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    89. {
    90. pressedToSize = false;
    91. if((event->pos().y() >= 0 && (event->pos().y() <= 10)) || ((event->pos().y() <= height) && (event->pos().y() >= (height - 10)) ))
    92. {
    93. QApplication::setOverrideCursor(Qt::SizeVerCursor);
    94. }
    95. else
    96. {
    97. QApplication::setOverrideCursor(Qt::OpenHandCursor);
    98. }
    99. update();
    100.  
    101. QGraphicsItem::mouseReleaseEvent(event);
    102. }
    103.  
    104. void MyRectangle::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
    105. {
    106. color = Qt::red;
    107. if((event->pos().y() > 0 && (event->pos().y() < 10)) || ((event->pos().y() < height) && (event->pos().y() > (height - 10)) ))
    108. {
    109. QApplication::setOverrideCursor(Qt::SizeVerCursor);
    110. }
    111. else
    112. {
    113. QApplication::setOverrideCursor(Qt::OpenHandCursor);
    114. }
    115. update();
    116.  
    117. }
    118.  
    119. void MyRectangle::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
    120. {
    121. color = Qt::red;
    122. if((event->pos().y() > 0 && (event->pos().y() < 10)) || ((event->pos().y() < height) && (event->pos().y() > (height - 10)) ))
    123. {
    124. QApplication::setOverrideCursor(Qt::SizeVerCursor);
    125. }
    126. else
    127. {
    128. QApplication::setOverrideCursor(Qt::OpenHandCursor);
    129. }
    130. update();
    131. }
    132.  
    133. void MyRectangle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
    134. {
    135. color = Qt::green;
    136. if((event->pos().y() >= 0 && (event->pos().y() <= 10)) || ((event->pos().y() <= height) && (event->pos().y() >= height - 10) ))
    137. {
    138. QApplication::setOverrideCursor(Qt::SizeVerCursor);
    139. }
    140. else
    141. {
    142. QApplication::setOverrideCursor(Qt::ArrowCursor);
    143. }
    144. update();
    145. }
    146.  
    147. void MyRectangle::recevedClickedMouse(int x, int y)
    148. {
    149. qDebug() << "7777 x = " << x << "y = " << y << endl;
    150. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,690
    Thanks
    257
    Thanked 750 Times in 740 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: resize Rectangle using mouse events

    In your MyRectangle constructor, you have not called either of the base class constructors (QObject, QGraphicsItem), both of which expect a pointer to a parent object (QObject or QGraphicsItem). In any case, there -is- a QGraphicsObject class which inherits from both QObject and QGraphicsItem and you should be using that as your base class if you need signals and slots in your rectangle.

    There is a logic error in your code - the mouseReleaseEvent is the only place you set pressToSize back to false. If you press the mouse inside your rectangle, and then release it -outside- the rectangle, then this release event never gets called because the rectangle no longer has the input focus. You should set it to false as the first thing in the mousePressEvent.
    <=== 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. #3
    Join Date
    Jul 2020
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: resize Rectangle using mouse events

    Thank you very much for your answer.
    If I do what you say, I get to resize the bottom of the rectangle and at the same time it moves it.
    So I found the solution by adding only the function: prepareGeometryChange () at the start of the mouseMoveEvent function.
    now it is working properly.

Similar Threads

  1. How to Draw and resize Rectangle
    By ranjithreddykommareddy in forum Newbie
    Replies: 5
    Last Post: 22nd December 2015, 11:22
  2. resize Rectangle box on imge using mouse events
    By ranjithreddykommareddy in forum Newbie
    Replies: 6
    Last Post: 21st December 2015, 18:07
  3. Replies: 3
    Last Post: 8th October 2011, 10:46
  4. Replies: 2
    Last Post: 9th May 2011, 03:04
  5. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 07:13

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.