Congratulations to our 2022 Qt Champions!

[SOLVED] QTextEdit auto scroll source code

  • I'm implementing a subclass of QTextEdit and I need some functionality very close to the auto-scrolling which the widget already has. This is implemented in the QTextEdit.cpp source code, using a timer started when mouse is moving and stopped when it is released, emitting scroll requests on timerEvent().

    But looking at the code, there is a part I don't understand - and maybe someone familiar with widget code can help explain.

    This appears within the timerEvent() code:
    const QPoint globalPos = QCursor::pos();
    pos = d->viewport->mapFromGlobal(globalPos);
    QMouseEvent ev(QEvent::MouseMove, pos, mapTo(topLevelWidget(), pos), globalPos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);

    What does this part do? Why is a mouse move event created and simulated here?

  • Moderators

    Notice that timerEvent doesn't contain any code to stop the timer.

    The code you posted creates a mouse move event for the QTextEdit even outside of the widget. This can happen if you start to select the text and go outside the boundaries of the widget. It keeps selecting and scrolling.

    The called mouseMoveEvent in turn checks if the cursor is back in the viewport of the widget and if so it stops the timer (and thus scrolling).

  • Thank you very much for you quick reply Chris. That makes sense.

    There is some code following this extract that adjusts the timeout of the timer but I understand now that in the case you describe the delta would also be 0 so the start() line of the timer wouldn't be called.

Log in to reply