View Full Version : Drawing a selection rectangle
Caius Aérobus
1st April 2009, 16:15
Hello,
I would like to start a selection process as a MousePressEvent is detected and define the definitive selection area as a MouseReleaseEvent is detected.
But I would like to draw the selection area as teh user is still moving the mouse with the left button pushed.
My problem is that I would like a paint event be processed immediatly after any MouseMoveEvent was detected, and then draw both the old and the new selection using Xor mode.
So 2 questions:
- is this the right way to make it?
- if yes how could I force processing a paint event afer every MouseMoveEvent (of course QCoreApplication::processEvents() does not work in a MouseMoveEvent)
Thanks in advance for your help.
talk2amulya
1st April 2009, 17:30
i m not sure, but would this work for you?
YourWidget::mousePressEvent(QMouseEvent *e)
{
point1 = e->globalPos();//get global position according to ur parent-child relationship
}
YourWidget::mouseReleaseEvent(QMouseEvent *e)
{
point2 = e->globalPos();//get global position according to ur parent-child relationship
QPainter painter(this);
painter->drawRect(point1, point2);
}
Caius Aérobus
1st April 2009, 17:41
i m not sure, but would this work for you?
YourWidget::mousePressEvent(QMouseEvent *e)
{
point1 = e->globalPos();//get global position according to ur parent-child relationship
}
YourWidget::mouseReleaseEvent(QMouseEvent *e)
{
point2 = e->globalPos();//get global position according to ur parent-child relationship
QPainter painter(this);
painter->drawRect(point1, point2);
}
Hmmmm, I believed paintings should only occur in a paintEvent...
talk2amulya
1st April 2009, 17:47
QPainter's common use is there, that doesnt mean u cant use it elsewhere :) i dont know if its the most optimized way of doing it, just felt like the right way and done on the fly, so bear with me :)
aamer4yu
1st April 2009, 20:16
Drawing in the mouse events wont be a good idea.
Reason : the next paint event call will erase it !
For your need, have a look at QRubberBand class. Fits very well to ur needs :)
talk2amulya
1st April 2009, 20:35
hey caius,
sorry abt a wrong and hasty post..i didnt really think about it :o
zarkzervo
2nd April 2009, 07:53
I have this in my inherited GraphicsScene:
void MyGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
event->ignore();
// Reset selectionArea
setSelectionArea(QPainterPath());
// Always remember to call parents mousePressEvent
QGraphicsScene::mousePressEvent(event);
}
and
void MyGraphicsScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
event->ignore();
bool ctrl = (event->modifiers() == Qt::ControlModifier);
QPainterPath tmpPath = selectionArea();
if(tmpPath.isEmpty())
{
// if ctrl pressed, then toggle selection
emit select(event->scenePos(), ctrl);
}
else
{
// if ctrl pressed, then add selection
emit select(tmpPath, ctrl);
}
// Always remember to call parents mousePressEvent
QGraphicsScene::mouseReleaseEvent(event);
}
I just inherited QGraphicsScene locally just to re-implement these two functions. This is the complete code I use in these two functions. You may not need all, but what I do, is to emit the signal with the path (selection rectangle) and if the user has pressed ctrl (adding elements)
Hope this helps and I didn't misunderstand your question.
Caius Aérobus
4th April 2009, 15:47
Hope this helps and I didn't misunderstand your question.
You didn't but since I do not use QGraphicsScene...
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.