QPlainTextEdit: Don't block signals on page step

Signals of the vertical scroll bar were blocked when scrolling with
pageUp/pageDown keys or clicking on the scroll range outside the
scroll bar. This has lead to inconsistent signal emissions:

The vertical scroll bar's valueChanged signal was emitted only on
single steps and when the scroll bar was moved with the mouse.
When it was moved in page steps by clicking on the scroll range or
pressing pageUp/pageDown, it was not emitted.

This patch removes the signal blocker for page step movements, which
was added in 94fd108ea42a99dacefa819bc3fd4363fb95e886 to replace
explicit calls to blockSignals(), which were added in
c14d442b08ac81327b173b0a220c7b1840667899.

The patch also adds test function to tst_QPlainTextEdit to check if
the valueChanged signal is emitted correctly.

Fixes: QTBUG-8682
Fixes: QTBUG-25365
Change-Id: I4385a5387c91497f623978e35bbbe3e06f473afe
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
(cherry picked from commit 09c3cd8503102c70c7d5bf41c0b662b2c86b4ad0)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Axel Spoerl 2023-02-17 12:42:50 +01:00 committed by Qt Cherry-pick Bot
parent c71131987e
commit 52f8fd2e68
2 changed files with 29 additions and 12 deletions

View File

@ -658,10 +658,7 @@ void QPlainTextEditPrivate::setTopBlock(int blockNumber, int lineNumber, int dx)
lineNumber = maxTopLine - block.firstLineNumber();
}
{
const QSignalBlocker blocker(vbar);
vbar->setValue(newTopLine);
}
vbar->setValue(newTopLine);
if (!dx && blockNumber == control->topBlock && lineNumber == topLine)
return;
@ -677,10 +674,7 @@ void QPlainTextEditPrivate::setTopBlock(int blockNumber, int lineNumber, int dx)
control->topBlock = blockNumber;
topLine = lineNumber;
{
const QSignalBlocker blocker(vbar);
vbar->setValue(block.firstLineNumber() + lineNumber);
}
vbar->setValue(block.firstLineNumber() + lineNumber);
if (dx || dy) {
viewport->scroll(q->isRightToLeft() ? -dx : dx, dy);
@ -1038,10 +1032,7 @@ void QPlainTextEditPrivate::_q_adjustScrollbars()
if (firstVisibleBlock.isValid())
visualTopLine = firstVisibleBlock.firstLineNumber() + topLine;
{
const QSignalBlocker blocker(vbar);
vbar->setValue(visualTopLine);
}
vbar->setValue(visualTopLine);
hbar->setRange(0, (int)documentSize.width() - viewport->width());
hbar->setPageStep(viewport->width());

View File

@ -157,6 +157,7 @@ private slots:
void appendTextWhenInvisible();
void placeholderVisibility_data();
void placeholderVisibility();
void scrollBarSignals();
private:
void createSelection();
@ -1944,5 +1945,30 @@ void tst_QPlainTextEdit::placeholderVisibility()
QTRY_VERIFY(plainTextEdit_d->placeholderVisible == placeholderVisible);
}
void tst_QPlainTextEdit::scrollBarSignals()
{
QPlainTextEdit plainTextEdit;
QString longText;
for (uint i = 0; i < 500; ++i)
longText += "This is going to be a very long text for scroll signal testing.\n";
plainTextEdit.setPlainText(longText);
QScrollBar *vbar = plainTextEdit.verticalScrollBar();
plainTextEdit.show();
QVERIFY(QTest::qWaitForWindowExposed(&plainTextEdit));
QSignalSpy spy(vbar, &QScrollBar::valueChanged);
QTest::keyClick(vbar, Qt::Key_Down);
QTRY_COMPARE(spy.count(), 1);
QTest::keyClick(vbar, Qt::Key_PageDown);
QTRY_COMPARE(spy.count(), 2);
QTest::keyClick(vbar, Qt::Key_PageDown);
QTRY_COMPARE(spy.count(), 3);
QTest::keyClick(vbar, Qt::Key_Up);
QTRY_COMPARE(spy.count(), 4);
QTest::keyClick(vbar, Qt::Key_PageUp);
QTRY_COMPARE(spy.count(), 5);
}
QTEST_MAIN(tst_QPlainTextEdit)
#include "tst_qplaintextedit.moc"