Important: Please read the Qt Code of Conduct -

Qt equivalent of some Win32 code in OnMouseMove (mouseMoveEvent in Qt)

  • In the OnMouseMove() mf of a Win32 custom control which I am re-writing for Qt, there is some code that looks like this:

    //----- Get the original erase area -----//
    //----- Draw the markers -----//
    erasergn.CreateRectRgn(0,0,0,0); //Dummy rgn
    erasergn.CombineRgn(&oldrgn, &newrgn, RGN_DIFF);
    dc.FillRgn(&erasergn, &brush); // Background colour brush

    The idea being to erase the previously drawn markers after the new ones are drawn

    What should I be doing in Qt?

    PS is there any Qt equivalent of the Windows OnEraseBackGround() ?
    PPS only three weeks Qt experience so far so please be gentle!

    Many thanks

  • Lifetime Qt Champion

    Painting can only be done inside the paintEvent():
    The mouse move events can be captured with QWidget::mouseMoveEvent()

  • So I can't paint inside the mouseMoveEvent() mf? Oh dear that's quite a problem.

    What I'm doing is dragging a shape and as it is dragged the previously drawn version must be erased and the new one drawn which is what that code does (there's more to it than that but you get the idea).

    So given that you seem to be saying "You can't get there from here" - how on earth do I handle this?


  • Lifetime Qt Champion

    You just keep the shape rect / x,y as members of the class.
    Then in MouseMove you alter its position and call update()
    Then paintEvent is called it it draw the shapes in the pos.

    To erase the old shape, the code
    // this you can do in mousePress Qt also has region
    erasergn.CreateRectRgn(0,0,0,0); //Dummy rgn
    erasergn.CombineRgn(&oldrgn, &newrgn, RGN_DIFF);

    // this goes to PaintEvent
    dc.FillRgn(&erasergn, &brush); // Background colour brush
    (change to use QPainter methods ofc)

  • Lifetime Qt Champion


    Just a side question since you are mentioning a shape and movement, depending on what you want to do, did you consider the use of the Graphics View Framework ?

  • I did look at it but at the time didn't think it would be relevant.

    Right now I'm considering how much work it will be to work out how to only paint the invalidated region/rect versus the cost of just re-drawing the whole control.


Log in to reply