Drawing a selection rectangle
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.
Re: Drawing a selection rectangle
i m not sure, but would this work for you?
Code:
{
point1 = e->globalPos();//get global position according to ur parent-child relationship
}
{
point2 = e->globalPos();//get global position according to ur parent-child relationship
painter->drawRect(point1, point2);
}
Re: Drawing a selection rectangle
Quote:
Originally Posted by
talk2amulya
i m not sure, but would this work for you?
Code:
{
point1 = e->globalPos();//get global position according to ur parent-child relationship
}
{
point2 = e->globalPos();//get global position according to ur parent-child relationship
painter->drawRect(point1, point2);
}
Hmmmm, I believed paintings should only occur in a paintEvent...
Re: Drawing a selection rectangle
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 :)
Re: Drawing a selection rectangle
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 :)
Re: Drawing a selection rectangle
hey caius,
sorry abt a wrong and hasty post..i didnt really think about it :o
Re: Drawing a selection rectangle
I have this in my inherited GraphicsScene:
Code:
{
event->ignore();
// Reset selectionArea
// Always remember to call parents mousePressEvent
}
and
Code:
{
event->ignore();
bool ctrl = (event->modifiers() == Qt::ControlModifier);
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
}
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.
Re: Drawing a selection rectangle
Quote:
Originally Posted by
zarkzervo
Hope this helps and I didn't misunderstand your question.
You didn't but since I do not use QGraphicsScene...