Important: Please read the Qt Code of Conduct -

Drag Button Screen Tear?

  • I implemented drag button by inheriting the QPushButton class and overriding mouseMoveEvent like so:

    void DragButton::mouseMoveEvent(QMouseEvent* e) 
        if (e->buttons() == Qt::LeftButton)
            QPoint extent = QPoint(width() * 0.5, height() * 0.5);
            move(mapToParent(e->pos() - extent));
            emit mouseMove(this);

    But whenever I drag it I get this, what looks like, screen tearing on the button? For the life of me I can't figure out why.
    alt text

  • Lifetime Qt Champion


    Are you also doing custom painting ?

  • @SGaist I first had a button with a circular mask on it and it still did the tearing.

    But I've revisited the class recently and decided to look into this. The particular button in the image inherits DragButton and uses the paintEvent function to draw ellipses instead of applying a circular mask to the button. Lke this:

    void NodeButton::paintEvent(QPaintEvent* event)
        QPainter p(this);
        QBrush br = QBrush(Qt::gray, Qt::SolidPattern);
        p.drawEllipse(0, 0, width(), height());
        br = QBrush(color, Qt::SolidPattern);
        p.drawEllipse(2, 2, width() - 4, height() - 4);

    I've read things are rendered with masks. So if I draw an item larger than the width and height of the button in the custom paint function it won't show. Could the masks position be lagging behind the position of the ellipse thus causing it to cut off the side of the ellipse?

    Maybe it's also worth noting that the button is in QMainWindow -> QDockWidget -> CustomWidget -> Inherited QPushButton

  • Lifetime Qt Champion

    Wouldn't it be more efficient to create a pixmap with that ellipse when resizing your widget and only draw that pixmap in the paint event ?

  • @SGaist
    Doesn't solve the problem.

    I have numerous draggable buttons all of which could overlap and be different colors. I tried saving an ellipse to a 50, 50 pixmap and recoloring it but nothing I tried could recolor it.

    I don't know about the efficiency. Depends how qt implemented it. Pretty negligible. I did profile both of them in visual studios. They're really really close in performance. But I don't think efficiency is the problem.

    Perhaps there would be a large benefit using a pixmap instead if my buttons were larger in size.

    Edit: Initially did a 50, 50 pixmap test and the pixmap was slower. But it should have been 30, 30. I retested and the pixmap is like .2% faster.

Log in to reply