Results 1 to 8 of 8

Thread: Drawing a selection rectangle

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:rocessEvents() does not work in a MouseMoveEvent)
    Thanks in advance for your help.

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing a selection rectangle

    i m not sure, but would this work for you?

    Qt Code:
    1. YourWidget::mousePressEvent(QMouseEvent *e)
    2. {
    3. point1 = e->globalPos();//get global position according to ur parent-child relationship
    4. }
    5.  
    6. YourWidget::mouseReleaseEvent(QMouseEvent *e)
    7. {
    8. point2 = e->globalPos();//get global position according to ur parent-child relationship
    9. QPainter painter(this);
    10. painter->drawRect(point1, point2);
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing a selection rectangle

    Quote Originally Posted by talk2amulya View Post
    i m not sure, but would this work for you?

    Qt Code:
    1. YourWidget::mousePressEvent(QMouseEvent *e)
    2. {
    3. point1 = e->globalPos();//get global position according to ur parent-child relationship
    4. }
    5.  
    6. YourWidget::mouseReleaseEvent(QMouseEvent *e)
    7. {
    8. point2 = e->globalPos();//get global position according to ur parent-child relationship
    9. QPainter painter(this);
    10. painter->drawRect(point1, point2);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Hmmmm, I believed paintings should only occur in a paintEvent...

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  6. The following user says thank you to aamer4yu for this useful post:

    talk2amulya (1st April 2009)

  7. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Drawing a selection rectangle

    hey caius,

    sorry abt a wrong and hasty post..i didnt really think about it

  8. #7
    Join Date
    Jan 2009
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing a selection rectangle

    I have this in my inherited GraphicsScene:
    Qt Code:
    1. void MyGraphicsScene::mousePressEvent ( QGraphicsSceneMouseEvent * event )
    2. {
    3. event->ignore();
    4.  
    5. // Reset selectionArea
    6. setSelectionArea(QPainterPath());
    7.  
    8. // Always remember to call parents mousePressEvent
    9. QGraphicsScene::mousePressEvent(event);
    10. }
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void MyGraphicsScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
    2. {
    3. event->ignore();
    4.  
    5. bool ctrl = (event->modifiers() == Qt::ControlModifier);
    6.  
    7. QPainterPath tmpPath = selectionArea();
    8. if(tmpPath.isEmpty())
    9. {
    10. // if ctrl pressed, then toggle selection
    11. emit select(event->scenePos(), ctrl);
    12. }
    13. else
    14. {
    15. // if ctrl pressed, then add selection
    16. emit select(tmpPath, ctrl);
    17. }
    18.  
    19. // Always remember to call parents mousePressEvent
    20. QGraphicsScene::mouseReleaseEvent(event);
    21.  
    22. }
    To copy to clipboard, switch view to plain text mode 
    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.

  9. #8
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Drawing a selection rectangle

    Quote Originally Posted by zarkzervo View Post
    Hope this helps and I didn't misunderstand your question.
    You didn't but since I do not use QGraphicsScene...

Similar Threads

  1. [SOLVED] QTreeView drawing selection with icons
    By Timewarp in forum Qt Programming
    Replies: 7
    Last Post: 7th February 2013, 07:52
  2. How to remove the dotted rectangle of selection
    By jiveaxe in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2010, 22:00
  3. QTableView, no selection rectangle
    By alisami in forum Qt Programming
    Replies: 13
    Last Post: 25th December 2008, 19:50
  4. Problems with QString
    By cyberboy in forum Qt Programming
    Replies: 2
    Last Post: 13th October 2008, 08:18
  5. How to paint a selection rectangle on a pixmap?
    By SkripT in forum Qt Programming
    Replies: 6
    Last Post: 8th January 2006, 19:52

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.