Results 1 to 20 of 25

Thread: Move Rectangle on mouse Move

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Quote Originally Posted by marcel View Post
    This means that you actually drag the object ( While holding the left button pressed ), so you can use the mouseReleaseEvent to stop dragging and figure out the position where the user dropped the object.
    Not exactly...But something similar. I dont actually drag....

    Ok so far so good. I have got what I want and Marcel it is becuase of your help.Thanks
    Now I have two questions.
    1) As the size of RubberBand rectangle increases, the display becomes very slow. I think the time is delayed because of painting of rubberBand class. Is there a way I can reduce this delay such that I dont have problem moving the rectangle as it becomes large.
    2) As I have other functionalities defined in MouseMove, MousePress, MouseRelease events, not it looks very messy. The idea to include eventFilter was to block all events other than those processed for pasting(mouse moves and clicks). Do you think it was a bad idea????? I know, you think from above post but can you please explain why???

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    1) As the size of RubberBand rectangle increases, the display becomes very slow. I think the time is delayed because of painting of rubberBand class. Is there a way I can reduce this delay such that I dont have problem moving the rectangle as it becomes large.
    How big are we talking about? And how does the code look now? Maybe it is because of that.

    As I have other functionalities defined in MouseMove, MousePress, MouseRelease events, not it looks very messy. The idea to include eventFilter was to block all events other than those processed for pasting(mouse moves and clicks). Do you think it was a bad idea????? I know, you think from above post but can you please explain why???
    Yes, of course it was a bad idea, because those events are needed.
    You cannot block them, because your application will stop functioning properly.
    For example, when you move the rubberband, your widget also gets repaint events, that tell it to repaint the regions occupied by the rubberband.
    If they are accepted by the event filter, then the rubberband doesn't get any repaint events. Hope you understand now.

    You could scan for all the event types in the eventfilter and print out their name, and then you will actually see how many they are.

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    How big are we talking about? And how does the code look now? Maybe it is because of that.
    Big??? Depends on number of objects and the spacing between them. If there are two rectangles one at left end and other at right end, then it occupies the entire area of screen, then there is a dealy.
    I did exactly this
    " In slotOnPasteButton() all you have to do is create the rubber band( QRubberBand ) and set its initial geometry and show it( like it is done in the mousePressEvent form the example ). The mouseMoveEvent has to test is if the QRubberBand is NULL - if not NULL, then set it's geometry, otherwise, do nothing."

    Yes, of course it was a bad idea, because those events are needed.
    You cannot block them, because your application will stop functioning properly.
    For example, when you move the rubberband, your widget also gets repaint events, that tell it to repaint the regions occupied by the rubberband.
    If they are accepted by the event filter, then the rubberband doesn't get any repaint events. Hope you understand now.
    Please see the first example above I have done some modifications.

    So what do you say about above changes

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    How big are we talking about? And how does the code look now? Maybe it is because of that.
    Big??? Depends on number of objects and the spacing between them. If there are two rectangles one at left end and other at right end, the RubberBand rectangle occupies the entire area on screen, then there is a delay.
    I did exactly this
    " In slotOnPasteButton() all you have to do is create the rubber band( QRubberBand ) and set its initial geometry and show it( like it is done in the mousePressEvent form the example ). The mouseMoveEvent has to test is if the QRubberBand is NULL - if not NULL, then set it's geometry, otherwise, do nothing."

    Yes, of course it was a bad idea, because those events are needed.
    You cannot block them, because your application will stop functioning properly.
    For example, when you move the rubberband, your widget also gets repaint events, that tell it to repaint the regions occupied by the rubberband.
    If they are accepted by the event filter, then the rubberband doesn't get any repaint events. Hope you understand now.
    I have this modification in my first post
    Qt Code:
    1. if( (e->type() == QEvent::Paint) || (e->type() == QEvent::Resize) ||
    2. (e->type() == QEvent::FocusIn) || (e->type() == QEvent::FocusOut) )
    3. {
    4. return FALSE;
    5. }
    To copy to clipboard, switch view to plain text mode 
    This is the first condition in eventFilter.
    No I just want to know whether using eventFilter is really dangerous???

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Something like this

    Qt Code:
    1. void RubberBand::onPasteBtn(){
    2. qApp->installEventFilter( this );
    3. qApp->enter_loop();
    4. }
    5. void RubberBand::mousePressEvent(QMouseEvent *event)
    6. {
    7. //other stuffs
    8. }
    9. void RubberBand::mouseMoveEvent(QMouseEvent *event)
    10. {
    11. //other stuffs
    12. }
    13. void RubberBand::mouseReleaseEvent(QMouseEvent *event)
    14. {
    15. //other stuffs
    16. }
    17. bool RubberBand::eventFilter( QObject *obj, QEvent *e ){
    18.  
    19. if( (e->type() == QEvent::Paint) || (e->type() == QEvent::Resize) ||
    20. (e->type() == QEvent::FocusIn) || (e->type() == QEvent::FocusOut) )
    21. {
    22. return FALSE;
    23. }
    24. if ( e->type() == QEvent::KeyPress && ((QKeyEvent *)e)->key() == Qt::Key_Escape ){
    25. // remove this event filter and exit the current event loop
    26. qApp->removeEventFilter( this );
    27. qApp->exit_loop();
    28. delete m_Rubberband;
    29. m_Rubberband = 0;
    30. return TRUE;
    31. }
    32. if( e->type() == QEvent::MouseMove ){
    33. m_pasteRect.setTop( ((QMouseEvent *)e)->pos().x() );
    34. m_pasteRect.setLeft( ((QMouseEvent *)e)->pos().y() );
    35.  
    36. m_pasteRect.setBottom( ((QMouseEvent *)e)->pos().x() + 50 );
    37. m_pasteRect.setRight( ((QMouseEvent *)e)->pos().y() + 50 );
    38.  
    39. if( !m_Rubberband )
    40. m_Rubberband = new QRubberBand(QRubberBand::Rectangle, this);
    41.  
    42. m_Rubberband->setGeometry( m_pasteRect );
    43. m_Rubberband->show();
    44.  
    45. return TRUE;
    46. }
    47. else if( e->type() == QEvent::MouseButtonRelease ){
    48. // remove this event filter and exit the current event loop
    49. qApp->removeEventFilter( this );
    50. qApp->exit_loop();
    51. delete m_Rubberband;
    52. m_Rubberband = 0;
    53. return TRUE; // eat event
    54. }
    55. return TRUE; // block standard event processing
    56. }
    To copy to clipboard, switch view to plain text mode 

    Now in the above code there are both events and eventFilter. So do you still I'm doing something wrong above. Please bear with me. I want to clear my fundas...
    Thanks

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    OK.
    The eventFilter function does exactly what it's name suggests - it FILTERS events - meaning that if you don't want a certain event to reach certain widget, you catch it it.

    The function that handles all events is QObject::event(). Here all events are passed and they are forwarded to the more specialized event handlers ( like mousePressEvent, keyPressEvent, etc ).

    The events go first through event filter, then in the event().
    So, you should not use eventFilter, unless you want to filter some events.
    I believe you should take a look in Assistant at eventFilter() and event().

    Anyway, accepting the mouse events in the event filter makes the event handlers you created useless because they will not be called.

    I assume that you are coming from a long programming period in MFC .
    You shouldn't care about other events, other than mouse move, etc. If you don't need an event, then leave it alone, because you risk having unpredicted behavior in your app ( even if it works well a few times ).

    Conclusion: eventFilter is used to filter( read select, catch ) certain events.
    event() & friends ( mouseMoveEvent, etc ) is used to actually do something when that certain event is sent to your widget. You may choose to accept it( don't forward it to children ) or reject it.
    However, the recommended approach is to use the specialized event handlers. Every event has a specialized event handler.

    Regards

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    The events go first through event filter, then in the event().
    So, you should not use eventFilter, unless you want to filter some events.
    Anyway, accepting the mouse events in the event filter makes the event handlers you created useless because they will not be called.
    Exactly. I accept mouse events in the event filter. Call event_loop to enter in event filter till the user either releases the mouse or press <esc>.Till then all other events are on halt/useless so that I can perform operations only regarding pasting or dragging. Once done pass the control to other event handlers to perform other tasks as in usual manner using
    qApp->removeEventFilter( this );
    qApp->exit_loop();.

    So, again do you think using eventFilter in the above scenario is wrong.
    Waiting for a reply desperately.....

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Yes, this approach is wrong because you are using the wrong function.
    For what you want to do you must use either event() or the specialized event handlers.
    You do not need to filter events and stop certain events.

    Till then all other events are on halt/useless so that I can perform operations only regarding pasting or dragging.
    As I said before each and every "useless" event is necessary for the app to work properly.
    They won't get in your way, so you can just handle the events that interest you.

    Call event_loop to enter in event filter till the user either releases the mouse or press <esc>
    The eventFilter is not a loop, neither is event & friends. The are called repeatedly for every event that the application event loop receives.
    You can easily verify the non-loop affirmation. Use qDebug in mouseMoveEvent() ( or even in eventFilter if you wish ), and print something to the console.
    For mouseMoveEvent you will see the message printed over and over as long as your mouse moves over the widget.

    Therefore there are no loops at this level. The loop is in the application event loop itself. From there are called event filters and the specialized event handlers.

    I hope you understand now what the event work flow is.

    Regards

  9. #9
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    To Patrick:
    I wait now here more as 60 Day http://www.qtforum.de/forum/viewtopic.php?t=3885 from a QT user a simple minimal demo on RubberBand how draw and move CTRL + Mouse move a rectangle to crop image .... only to become QRect.
    Why don't you post it here too. I'm sure someone will be able to help you.
    I looked at that post but I don't understand German .

    Regards

  10. #10
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Ok, I agree with you...But you didnt tell anything about the lag. Why is there so much delay to paint the RubberBand rect???

  11. #11
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    I don't know about that. Maybe if you use move() instead of setGeometry() will make it move faster.

    First try rewriting it as we discussed, and see if the lag still appears.

    Regards

  12. #12
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Quote Originally Posted by marcel View Post
    I don't know about that. Maybe if you use move() instead of setGeometry() will make it move faster.

    First try rewriting it as we discussed, and see if the lag still appears.

    Regards
    Yes I have used move instead of setGeometry() but still there is lag. I have comlpetely removed the previous logic and is using new functionality as we discussed but still there is lag....

  13. #13
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Can you show me again the code?

  14. #14
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Move Rectangle on mouse Move

    Quote Originally Posted by vermarajeev View Post
    Something like this
    Now in the above code there are both events and eventFilter. So do you still I'm doing something wrong above. Please bear with me. I want to clear my fundas...
    Thanks
    RubberBand is a hard stuff..

    I wait now here more as 60 Day http://www.qtforum.de/forum/viewtopic.php?t=3885 from a QT user a simple minimal demo on RubberBand how draw and move CTRL + Mouse move a rectangle to crop image .... only to become QRect.

    I solved , on draw a QWidget.... and is running ok.

    Qt Code:
    1. void Image_Operator::paintEvent(QPaintEvent *e)
    2. {
    3.  
    4. if (display.isNull()) {
    5. return;
    6. }
    7. QString selectionText;
    8. /* color to pen */
    9. QColor textColor = Qt::black;
    10. QColor fillrectcolor = Qt::red;
    11. QColor shapepicture = Qt::white;
    12.  
    13. /////////////////////qDebug() << "### paintEvent go starter...... ";
    14. Load_Actual_Desktop(); /* widget size setting e resolution X11 */
    15. int hi_now = widgetSize.height();
    16. int wi_now = widgetSize.width();
    17. picscaled = display.scaled(wi_now,hi_now,Qt::KeepAspectRatio); /* scaled to widget displayer */
    18. QSize actual_result = picscaled.size(); /* get */
    19. QPainter painter(this);
    20. painter.setRenderHint(QPainter::Antialiasing, true);
    21. painter.drawPixmap(0,0,picscaled);
    22.  
    23. /*
    24.   QFontMetrics fm( this->font() );
    25.   int stringWidth = fm.width(selectionText);
    26.   int stringHeight = fm.ascent();
    27.   */
    28.  
    29. const int TEXT_MARGIN = 4;
    30.  
    31. int bordershade = 26;
    32.  
    33. int textX = 0;
    34. int textY = 0;
    35.  
    36. int Top_startX = QMIN(mousePRESSPoint.x(), mouseRELEASEPoint.x());
    37. int Top_startY = QMIN(mousePRESSPoint.y(), mouseRELEASEPoint.y()) ;
    38.  
    39. int Bot_endX = QMAX(mousePRESSPoint.x(), mouseRELEASEPoint.x());
    40. int Bot_endY = QMAX(mousePRESSPoint.y(), mouseRELEASEPoint.y());
    41.  
    42. /* display rect mesure image crop !!! */
    43. QPoint topLeft( Top_startX , Top_startY );
    44. QPoint bottomRight( Bot_endX , Bot_endY );
    45. QRect selectionIMAGE( topLeft, bottomRight );
    46.  
    47. /* display rect mesure image crop !!! */
    48. /* display rect mesure text crop !!! */
    49. QPoint topLeftT( Top_startX , Top_startY - 20 );
    50. QPoint bottomRightT( Bot_endX - TEXT_MARGIN , Bot_endY - TEXT_MARGIN );
    51. QRect selectionTEXT( topLeftT, bottomRightT );
    52. /* display rect mesure text crop !!! */
    53. /* shade border */
    54. QPoint topLeftout( Top_startX - (bordershade / 2) , Top_startY - (bordershade / 2) );
    55. QPoint bottomRightout( Bot_endX + (bordershade / 2) , Bot_endY + (bordershade / 2) );
    56. QRect selectionOutIMAGE(topLeftout,bottomRightout);
    57. /* shade border */
    58.  
    59.  
    60. /////////qDebug() << "### Top_startX->" << Top_startX << " Bot_endX->" << Bot_endX;
    61. ///////////qDebug() << "### Top_startY->" << Top_startY << " Bot_endY->" << Bot_endY;
    62. int HighteResult = selectionIMAGE.height(); /* correct */
    63. int LargeResult = selectionIMAGE.width(); /* correct */
    64. qreal cento;
    65. qreal ratio;
    66. cento = 100.0;
    67. ratio = (actual_result.width()*cento) / origImageSize.width();
    68. ///////////qDebug() << "### originale foto " << origImageSize.width() << "x" << origImageSize.height();
    69. ////////////qDebug() << "### a video misura " << actual_result.width() << "x" << actual_result.height();
    70. //////////qDebug() << "### taglio uguale " << LargeResult << "x" << HighteResult;
    71. ////////////qDebug() << "### ratio " << ratio;
    72.  
    73. int LargeReal = ( LargeResult / ratio ) * cento;
    74. int HighteReal = ( HighteResult / ratio ) * cento;
    75. selectionText = QString("%1x%2 px (ratio \%%3)")
    76. .arg(LargeReal)
    77. .arg(HighteReal)
    78. .arg(ratio);
    79.  
    80. if ( LargeResult < 9 ) {
    81. currentDragMode = NO_EFFECT;
    82. }
    83.  
    84.  
    85. QPen pen;
    86. pen.setStyle( Qt::SolidLine );
    87.  
    88.  
    89. ///////////// qDebug() << "### currentDragMode -> " << currentDragMode ;
    90.  
    91.  
    92. if ( currentDragMode == DRAW_SELECTION || currentDragMode == DRAW_LINE ) {
    93. OneWorkImage = selectionIMAGE;
    94. pen.setWidth( bordershade );
    95. pen.setColor( shapepicture );
    96. painter.setPen( pen);
    97. painter.drawRect(selectionOutIMAGE);
    98. /* display rect to crop image !!! */
    99. pen.setWidth( 2 );
    100. pen.setColor( fillrectcolor );
    101. painter.setPen( pen);
    102. painter.drawRect(selectionIMAGE);
    103. /* display rect to crop image !!! */
    104. /* display text image ratio text on crop image !!! */
    105. pen.setColor( fillrectcolor );
    106. painter.setPen( pen);
    107. /* display text image ratio text on crop image !!! */
    108. painter.drawText(selectionTEXT,selectionText);
    109. }
    110. //////// Show_Actual_Params(); /* debug display coordinate all */
    111. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Game mouse movement
    By chaosgeorge in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2006, 23:41
  2. how to display full tree item name on mouse move ?
    By rajesh in forum Qt Programming
    Replies: 5
    Last Post: 15th November 2006, 08:41
  3. mouse moving don't produce mouse events
    By coralbird in forum Qt Programming
    Replies: 1
    Last Post: 13th September 2006, 06:13
  4. Replies: 2
    Last Post: 24th July 2006, 18:36
  5. QStackerWidget and mouse events
    By high_flyer in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2006, 19:25

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
  •  
Qt is a trademark of The Qt Company.