[app] Workaround for positionViewAtIndex not working for the first time

This is probably a bug in QQuickListView - it first calculates the
item position and then starts instantiating the delegates. The very
first time the item position always turns out to be zero because the
average item size isn't known yet. So if we are trying to position the
list at a non-zero index and instead we got positioned at zero, try it
again.
This commit is contained in:
Slava Monich 2016-11-24 19:38:53 +02:00
parent 32013a8ac5
commit 77be4b68e8
2 changed files with 29 additions and 2 deletions

View file

@ -48,6 +48,7 @@ BooksListWatcher::BooksListWatcher(QObject* aParent) :
iListView(NULL),
iCenterMode(-1),
iPositionIsChanging(false),
iCanRetry(true),
iResizeTimer(new QTimer(this))
{
iResizeTimer->setSingleShot(true);
@ -62,6 +63,7 @@ void BooksListWatcher::setListView(QQuickItem* aView)
if (iListView) iListView->disconnect(this);
iListView = aView;
iCenterMode = -1;
iCanRetry = true;
if (iListView) {
connect(iListView,
SIGNAL(widthChanged()),
@ -139,13 +141,36 @@ void BooksListWatcher::positionViewAtIndex(int aIndex)
}
}
iPositionIsChanging = true;
QMetaObject::invokeMethod(iListView, LISTVIEW_POSITION_VIEW_AT_INDEX,
Q_ARG(int,aIndex), Q_ARG(int,iCenterMode));
positionViewAtIndex(aIndex, iCenterMode);
if (iCanRetry) {
// This is probably a bug in QQuickListView - it first calculates
// the item position and then starts instantiating the delegates.
// The very first time the item position always turns out to be
// zero because the average item size isn't known yet. So if we
// are trying to position the list at a non-zero index and instead
// we got positioned at zero, try it again. It doesn't make sense
// to retry more than once though.
if (aIndex > 0 && getCurrentIndex() == 0) {
// Didn't work from the first try, give it another go
HDEBUG("retrying...");
positionViewAtIndex(aIndex, iCenterMode);
}
iCanRetry = false;
}
iPositionIsChanging = false;
updateCurrentIndex();
}
}
void BooksListWatcher::positionViewAtIndex(int aIndex, int aMode)
{
if (iListView) {
QMetaObject::invokeMethod(iListView,
LISTVIEW_POSITION_VIEW_AT_INDEX,
Q_ARG(int,aIndex), Q_ARG(int,aMode));
}
}
qreal BooksListWatcher::contentX()
{
return getRealProperty(LISTVIEW_CONTENT_X);

View file

@ -66,6 +66,7 @@ private:
qreal contentY();
qreal getRealProperty(const char *name, qreal defaultValue = 0.0);
int getCurrentIndex();
void positionViewAtIndex(int aIndex, int aMode);
void updateCurrentIndex();
void updateSize();
@ -92,6 +93,7 @@ private:
QQuickItem* iListView;
int iCenterMode;
bool iPositionIsChanging;
bool iCanRetry;
QTimer* iResizeTimer;
};