QList::value(int i) implementation question
-
This question could possibly be about documentation or C++ or Python. So i'm posting it here!
Foreground to question:
For the overload
T QList::value(int i) constdocs (https://doc.qt.io/qt-5/qlist.html#value) stateIf the index i is out of bounds, the function returns a default-constructed value.
and https://doc.qt.io/qt-5/containers.html#default-constructed-value
The documentation of certain container class functions refer to default-constructed values. [...] For most value types, this simply means that a value is created using the default constructor.
I need to know whether the documentation is indeed correct in saying that
QList::value(int i)will return such a "default constructed value" wheniis out of range, and how it manages to do it when the type,T, is an abstract type?Background to question:
- From another of my posts (https://forum.qt.io/topic/104653/how-to-do-a-no-break-qhboxlayout), I have to translate a Qt sample (https://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html) into Python/PyQt. That includes:
QList<QLayoutItem *> itemList; QLayoutItem *FlowLayout::itemAt(int index) const { return itemList.value(index); }-
I have verified that, for whatever reason, the Qt layout/widget framework does call my
FlowLayout::itemAt(int index)whenindexis out of range (e.g. it calls it withindex==0even when mycount()returns 0). So I do need to "emulate" the C++ "default constructor" return result. -
In Python/PyQt you cannot use
QList. You use the nativelisttype, so that is what myitemListis. Whenindexis in range PythonitemList[index]works fine for C++itemList.value(index). But when it's out of range I need to do what the C++QList::value(out_of_range)would do, the "default constructor". -
But the type of
itemListisQList<QLayoutItem *>, andQLayoutItemis an abstract type. So my Python cannot goreturn QLayoutItem()to emulate, any more than you could in C++ --- I get a Python/PyQt run-time error saying "cannot create abstract instance/object".
So how does
QList::value()do it, and how can I? As I write this I have a feeling this might not be an issue in C++ because of how templates work, I don't know. (Maybe the code forvalue()can gonew T();and that works?) I haven't got templates, and the PythonlistI create for aQListdoesn't have any type information in the way thatQList<T>does.Anyone??
-
This question could possibly be about documentation or C++ or Python. So i'm posting it here!
Foreground to question:
For the overload
T QList::value(int i) constdocs (https://doc.qt.io/qt-5/qlist.html#value) stateIf the index i is out of bounds, the function returns a default-constructed value.
and https://doc.qt.io/qt-5/containers.html#default-constructed-value
The documentation of certain container class functions refer to default-constructed values. [...] For most value types, this simply means that a value is created using the default constructor.
I need to know whether the documentation is indeed correct in saying that
QList::value(int i)will return such a "default constructed value" wheniis out of range, and how it manages to do it when the type,T, is an abstract type?Background to question:
- From another of my posts (https://forum.qt.io/topic/104653/how-to-do-a-no-break-qhboxlayout), I have to translate a Qt sample (https://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html) into Python/PyQt. That includes:
QList<QLayoutItem *> itemList; QLayoutItem *FlowLayout::itemAt(int index) const { return itemList.value(index); }-
I have verified that, for whatever reason, the Qt layout/widget framework does call my
FlowLayout::itemAt(int index)whenindexis out of range (e.g. it calls it withindex==0even when mycount()returns 0). So I do need to "emulate" the C++ "default constructor" return result. -
In Python/PyQt you cannot use
QList. You use the nativelisttype, so that is what myitemListis. Whenindexis in range PythonitemList[index]works fine for C++itemList.value(index). But when it's out of range I need to do what the C++QList::value(out_of_range)would do, the "default constructor". -
But the type of
itemListisQList<QLayoutItem *>, andQLayoutItemis an abstract type. So my Python cannot goreturn QLayoutItem()to emulate, any more than you could in C++ --- I get a Python/PyQt run-time error saying "cannot create abstract instance/object".
So how does
QList::value()do it, and how can I? As I write this I have a feeling this might not be an issue in C++ because of how templates work, I don't know. (Maybe the code forvalue()can gonew T();and that works?) I haven't got templates, and the PythonlistI create for aQListdoesn't have any type information in the way thatQList<T>does.Anyone??
@JonB if
Tis an abstract type, it must be a pointer in C++, so the "default constructed value" will be a null pointer (nullptr) in this case.From https://doc.qt.io/qt-5/containers.html#default-constructed-value:
But for primitive types like int and double, as well as for pointer types, the C++ language doesn't specify any initialization; in those cases, Qt's containers automatically initialize the value to 0.
-
@JonB if
Tis an abstract type, it must be a pointer in C++, so the "default constructed value" will be a null pointer (nullptr) in this case.From https://doc.qt.io/qt-5/containers.html#default-constructed-value:
But for primitive types like int and double, as well as for pointer types, the C++ language doesn't specify any initialization; in those cases, Qt's containers automatically initialize the value to 0.
@Gojir4
That could be very helpful/good news for me!At present I think I shoved in
return Noneto get past it crashing and it might be working, I wasn't sure. I'd be happy if all I have to do is returnNonefornullptr:)Do you know, I'm just looking at the code and thinking I was mistaken, I didn't look closely enough.... It's
QList<QLayoutItem *> itemList;here. That's a list of pointers. I'm getting so used to irritating Python now, I think I thought it wasQList<QLayoutItem> itemList;, no pointers, a list ofQLayoutItems. One way or another I was thinking I had to create/return some ("empty") concrete instance of anQLayoutItem, maybenew T(), ....OK, this all makes sense now if I can just return
nullptrfor my default item!