Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QList::value(int i) implementation question
Forum Updated to NodeBB v4.3 + New Features

QList::value(int i) implementation question

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 276 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JonB
    wrote on 10 Jul 2019, 13:38 last edited by JonB 7 Oct 2019, 15:19
    #1

    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) const docs (https://doc.qt.io/qt-5/qlist.html#value) state

    If 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" when i is 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) when index is out of range (e.g. it calls it with index==0 even when my count() returns 0). So I do need to "emulate" the C++ "default constructor" return result.

    • In Python/PyQt you cannot use QList. You use the native list type, so that is what my itemList is. When index is in range Python itemList[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 itemList is QList<QLayoutItem *>, and QLayoutItem is an abstract type. So my Python cannot go return 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 for value() can go new T(); and that works?) I haven't got templates, and the Python list I create for a QList doesn't have any type information in the way that QList<T> does.

    Anyone??

    G 1 Reply Last reply 10 Jul 2019, 13:44
    0
    • J JonB
      10 Jul 2019, 13:38

      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) const docs (https://doc.qt.io/qt-5/qlist.html#value) state

      If 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" when i is 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) when index is out of range (e.g. it calls it with index==0 even when my count() returns 0). So I do need to "emulate" the C++ "default constructor" return result.

      • In Python/PyQt you cannot use QList. You use the native list type, so that is what my itemList is. When index is in range Python itemList[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 itemList is QList<QLayoutItem *>, and QLayoutItem is an abstract type. So my Python cannot go return 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 for value() can go new T(); and that works?) I haven't got templates, and the Python list I create for a QList doesn't have any type information in the way that QList<T> does.

      Anyone??

      G Offline
      G Offline
      Gojir4
      wrote on 10 Jul 2019, 13:44 last edited by Gojir4 7 Oct 2019, 13:45
      #2

      @JonB if T is 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.

      J 1 Reply Last reply 10 Jul 2019, 15:34
      5
      • G Gojir4
        10 Jul 2019, 13:44

        @JonB if T is 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.

        J Offline
        J Offline
        JonB
        wrote on 10 Jul 2019, 15:34 last edited by JonB 7 Oct 2019, 15:47
        #3

        @Gojir4
        That could be very helpful/good news for me!

        At present I think I shoved in return None to get past it crashing and it might be working, I wasn't sure. I'd be happy if all I have to do is return None for nullptr :)

        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 was QList<QLayoutItem> itemList;, no pointers, a list of QLayoutItems. One way or another I was thinking I had to create/return some ("empty") concrete instance of an QLayoutItem, maybe new T(), ....

        OK, this all makes sense now if I can just return nullptr for my default item!

        1 Reply Last reply
        1

        1/3

        10 Jul 2019, 13:38

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved