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