[vlc-devel] [PATCH 3/3] qt: allow getCount to initiate the cache in MLBaseModel
Pierre Lamot
pierre at videolabs.io
Thu Dec 17 13:01:25 UTC 2020
On 2020-12-17 13:40, Romain Vimont wrote:
> On Thu, Dec 17, 2020 at 01:32:50PM +0100, Pierre Lamot wrote:
>> The count property might be accessed before browsing the model.
>
> But if that's the case, m_cache should be nullptr, and the current
> implementation should work, doesn't it?
this is rather about initializing the cache. My use case was something
like:
MyModel {
id: model
ml: medialib
}
Loader {
active: model.count > 0
sourceComponent: WidgetUsingMyModel {
// ommited [...]
}
}
In this case, the count property would stay to 0 as the model is never
browsed, hence the chache is never created
>> ---
>> modules/gui/qt/medialibrary/mlbasemodel.cpp | 7 +++++--
>> 1 file changed, 5 insertions(+), 2 deletions(-)
>>
>> diff --git a/modules/gui/qt/medialibrary/mlbasemodel.cpp
>> b/modules/gui/qt/medialibrary/mlbasemodel.cpp
>> index 4549dacb57..68eb28dac9 100644
>> --- a/modules/gui/qt/medialibrary/mlbasemodel.cpp
>> +++ b/modules/gui/qt/medialibrary/mlbasemodel.cpp
>> @@ -237,7 +237,7 @@ void MLBaseModel::unsetSortCriteria()
>>
>> int MLBaseModel::rowCount(const QModelIndex &parent) const
>> {
>> - if (parent.isValid())
>> + if (!m_mediaLib || parent.isValid())
>> return 0;
>
> Is rowCount() called while medialib is not set yet?
> I'm surprised, because the medialib is passed as a QML property, it
> should be called before any rowCount() I guess.
I don't think there is practical use for this but you can call rowCount
manually from QML (in your onComplete for instance, where the m_medialib
may not be set yet)
in practice, we would rather use the count property
>
>>
>> validateCache();
>> @@ -302,7 +302,10 @@ QVariantList
>> MLBaseModel::getIdsForIndexes(QVariantList indexes) const
>>
>> unsigned MLBaseModel::getCount() const
>> {
>> - if (!m_cache || m_cache->count() == COUNT_UNINITIALIZED)
>> + if (!m_mediaLib)
>> + return 0;
>> + validateCache();
>> + if (m_cache->count() == COUNT_UNINITIALIZED)
>> return 0;
>> return static_cast<unsigned>(m_cache->count());
>> }
>> --
>> 2.25.1
>>
>> _______________________________________________
>> vlc-devel mailing list
>> To unsubscribe or modify your subscription options:
>> https://mailman.videolan.org/listinfo/vlc-devel
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel
More information about the vlc-devel
mailing list