Results 1 to 2 of 2

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

  1. #1
    Join Date
    Jul 2020
    Qt products
    MacOS X Unix/X11 Windows

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

    I originally posted this in error to the Qwt sub-forum where it's unlikely to be seen. Please could a moderator remove the version in there?

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

    Qt Code:
    Switch view

    //----- 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

    To copy to clipboard, switch view to plain text mode

    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
    Last edited by perdrix; 5th July 2020 at 12:33. Reason: error in title

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 728 Times in 718 Posts
    Qt products
    Windows Android

    Default Re: Qt equivalent of some Win32 code in OnMouseMove (mouseMoveEvent in Qt)

    It is hard to understand from the few lines of code you have written what you are trying to do, but it sounds a bit like XOR mode drawing. I don't understand what the "switch view", "copy to clipboard", "switch to plain text mode" have to do with redrawing markers. In any case, you should be erasing old markers first before drawing new ones. What happens if a new marker partially overlaps an old one and you draw the new one before erasing the old one?

    First, in Qt you may not draw to the screen anywhere except in the paintEvent(). So trying to directly redraw something in a mouse move event won't work. However, if you represent what you are trying to draw as a QPainterPath, you can update that outside of a paint event, and then redraw it during the paint event.

    Second, because all painting is done in the paintEvent(), there is no separate method corresponding to the WM_ERASEBKGND message. However, there is a QWidget property, QWidget::autoFillBackground(), which when true clears the window background and fills it using the appropriate brush created from the widget's QPalette before control passes into your paintEvent(). If it is false, then this part is skipped and it is up to you to clear the background (or not) in the paintEvent().

    Third, the QPaintEvent argument to QWidget::paintEvent() gives you the QRegion that must be updated and the QRect of the bounding rect for that region. So if you want to optimize your painting, the only place that will need updating will be that region.

    Finally, you can't (or shouldn't) do anything in a paint event which could trigger a new paint event, otherwise you risk getting into an infinite loop.

    QWidget::repaint() (despite the name, doesn't repaint anything) allows you to force a paintEvent() on a specific rect or region. Painting is scheduled by the event loop, so if there are multiple paintEvent() instances pending in the event queue, these will be combined into a single paintEvent() when actually executed.

    So for your custom control, you should be keeping some sort of data structure that has the current positions of all of your markers. When a marker moves, if you don't want to repaint the entire window, you should call repaint for the region where the marker used to be, and then repaint for the new region.

    Alternatively, you could consider basing your control on the Qt Graphics / View framework where you don't have to think about painting and repainting at all except at the level of painting an individual marker within its own coordinate space. (That is, if your marker is a QGraphicsItem circle 10 pixels in diameter, you draw a 10 pixel diameter circle centered at (0,0) inside a 10 x 10 rect). The Graphics / View framework will take care of all other redrawing.

    There are dozens of examples of custom QWidgets, Graphics / View, painting, and event handling in the examples and tutorials that come with Qt. I'd encourage you to look at them for ideas.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 3rd July 2020, 11:25
  2. Qt equivalent of C++ code
    By aesthetically asthmatic in forum Newbie
    Replies: 1
    Last Post: 24th June 2018, 17:20
  3. Replies: 3
    Last Post: 7th January 2017, 03:33
  4. QByteArray processing (seeking equivalent QT code)
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2009, 09:40
  5. pixmap onmousemove event help
    By bluesguy82 in forum Qt Programming
    Replies: 11
    Last Post: 9th August 2006, 14:15


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.