Drawing a rectangle between two mouse clicks
In this recipe, we will be learning to display a rectangle between the two points where the mouse button is clicked and released on the form.
How to do it...
It is a very simple application, where the mousePressEvent()
and mouseReleaseEvent()
methods are used to find the x and y coordinates of the location where the mouse is pressed and released, respectively. Thereafter, the drawRect()
method is invoked to draw the rectangle from the coordinates where the mouse button is clicked to the coordinates where the mouse button is released. The step-by-step procedure for creating this application is as follows:
- Let's create an application based on the
Dialog without Buttons
template. - Add a
QLabel
widget to the form by dragging and dropping aLabel
widget on the form. - Set the
text
property of theLabel
widget toClick the mouse and drag it to draw a rectangle of the desired size
. - Save the application by name as
demoDrawRectangle.ui
. The form will now appear...