QGraphicsView:QGraphicstextitem show error after lose focus

The reason for this bug is that after the TextItem loses focus,
the focusItem in QGraphicsScene is already empty.
When QGraphicsScene responds to the inputMethodEvent event again,
since focusItem is already empty,focusItem will no longer deliver events.

Fixes:QTBUG-85088
Pick-to:5.15

Change-Id: I329054333c2adec133d35318761612aca3afcf0d
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
bb10
Zhang Hao 2020-06-30 10:08:36 +08:00
parent 18cda2f669
commit 3983afd48f
2 changed files with 40 additions and 2 deletions

View File

@ -4143,8 +4143,12 @@ void QGraphicsScene::wheelEvent(QGraphicsSceneWheelEvent *wheelEvent)
void QGraphicsScene::inputMethodEvent(QInputMethodEvent *event)
{
Q_D(QGraphicsScene);
if (d->focusItem && (d->focusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod))
if (d->focusItem && (d->focusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod)) {
d->sendEvent(d->focusItem, event);
return;
}
if (d->lastFocusItem && d->lastFocusItem != d->focusItem && (d->lastFocusItem->flags() & QGraphicsItem::ItemAcceptsInputMethod))
d->sendEvent(d->lastFocusItem, event);
}
/*!

View File

@ -288,6 +288,7 @@ private slots:
void taskQTBUG_15977_renderWithDeviceCoordinateCache();
void taskQTBUG_16401_focusItem();
void taskQTBUG_42915_focusNextPrevChild();
void taskQTBUG_85088_previewTextfailWhenLostFocus();
private:
QRect m_availableGeometry = QGuiApplication::primaryScreen()->availableGeometry();
@ -3867,7 +3868,7 @@ void tst_QGraphicsScene::inputMethod()
item->eventCalls = 0;
QCoreApplication::sendEvent(&scene, &event);
QCOMPARE(item->eventCalls, 0);
QCOMPARE(item->eventCalls, callFocusItem ? 1 : 0);
item->queryCalls = 0;
scene.inputMethodQuery(Qt::InputMethodQuery(0));
@ -4932,5 +4933,38 @@ void tst_QGraphicsScene::taskQTBUG_42915_focusNextPrevChild()
QTest::keyEvent(QTest::Click, &view, Qt::Key_Tab);
}
void tst_QGraphicsScene::taskQTBUG_85088_previewTextfailWhenLostFocus()
{
QString str = "simpleTextItem";
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsTextItem *simpleTextItem = new QGraphicsTextItem;
simpleTextItem->setFlag(QGraphicsTextItem::ItemIsFocusScope);
simpleTextItem->setTextInteractionFlags(Qt::TextEditorInteraction);
simpleTextItem->setPlainText(str);
scene.addItem(simpleTextItem);
scene.setFocusItem(simpleTextItem);
view.setGeometry(100, 100, 600, 400);
view.show();
QInputMethodEvent inputEvent;
QString &preedictStr = const_cast<QString &>(inputEvent.preeditString());
preedictStr = str;
QApplication::sendEvent(&scene, &inputEvent);
QCOMPARE(simpleTextItem->toPlainText(), str);
// focusItem will lose focus
QMouseEvent pressEvent(QEvent::MouseButtonPress, QPointF(0, 0),
Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
QApplication::sendEvent(view.viewport(), &pressEvent);
preedictStr.clear();
inputEvent.setCommitString(str);
QApplication::sendEvent(&scene, &inputEvent);
QCOMPARE(simpleTextItem->toPlainText(), str + str);
}
QTEST_MAIN(tst_QGraphicsScene)
#include "tst_qgraphicsscene.moc"