QListWidget::addItems() Data type question
-
wrote on 29 Mar 2020, 16:59 last edited by
Hi,
as a newbie in C++ programming, I'm wondering why the following code lines work:
QStringList *list = new QStringList(QStringList() << "Item1" << "Item2" << "Item3"); list->append("Item4"); // this line just to demonstrate that the string list can be changed so it can't be a const, right? QListWidget *listWid = new QListWidget; listWid->addItems(*list); // <-- QListWidget::addItems(const QStringlist &list)
Question 1:
How can "*list" be a "const reference to a variable (list)" ? I mean, as the list is editable, it shouldn't be const, right?
Question 2:
I just passed the variable where the list pointer points to as an argument, not a reference of it while using "*list", right? So how can the compiler accept this code?Thank you in anticipation!
Kind regards -
Hi,
First thing, there's no need to allocate a QStringList on the heap.
The * operator dereferences what is pointed to by the pointer.
The const means that the variable passed cannot be modified in the method that you passed it to and the reference that a reference to it will be passed rather than the whole variable content copied.
-
Adding
const
is an operation that compiler can do whenever it needs to. Addingconst
does not "harm" the object in any way so adding it is a safe operation. This doesn't go the other way around. Removingconst
is an unsafe operation and compiler doesn't do it on its own. It requires an explicitconst_cast
to tell compiler that you know what you're doing.To answer your questions:
-
list
is a pointer.*list
is the variable it points to and so it can be passed to a function requiring a reference. As mentioned earlier you can pass a reference where a const reference is required. Compiler will treat the object as const automatically. This wouldn't go the other way - you can't pass a const object where a nono-const object is required. -
Consider function
void foo(const int&)
. You can give it aconst int&
. You can also give it aconst int
and, as mentioned before, you can give it anint
and the const will be added for you. If you have aint* bar
then*bar
is of typeint
so it can be passed whereconst int&
is needed. Same here. A const reference to a list is needed and when you dereference the pointer you get a list type and const is added to it for you.
Btw. Don't write code like that. You're not deleting the pointer so it leaks. Also in the first line you create a list, append some elements to it and then create dynamically another list and call a copy constructor. That's very wasteful. There's rarely a reason to allocate containers on the heap. Simply do
QStringList list {"Item1", "Item2", "Item3"}; ... listWid->addItems(list);
-
-
wrote on 30 Mar 2020, 08:01 last edited by
Hey guys,
thank you for your fast replies.The point was that I did not know that it is possible to re-declare a variable as "const" afterwards.
I mean, in a call-by-value condition, I would have understood that the new copy of my variable is function-internally declared const. But with passing the origin variable by reference, compiler addes the "const" status to my already existing variable, right?
So, shouldn't be the consequence that:
a) I can't change the stringlist after this function call or does compiler "un-const" the list after returning from the function again? Removing an item after this call didn't throw an error...
b) As I inserted the list-values by reference to the stringlist, shouldn't manipulating the stringlist after this function call affect the listwidget? Otherwise I wouldn't understand the sense why to pass the list by reference and why it has to be re-declared as "const"...And one more, maybe stupid question - sorry for that but I'm interessted in it and google won't give me an answer, maybe noone has ever asked that :-)
You both tell me not do use pointers that are allocated on the heap on QStringLists and other containers. Is there any kind of "rule" which class objects are "large enough" to allocate them on the heap? How do you manage this?Thank you really much for your support.
Greetings, Binary -
Hey guys,
thank you for your fast replies.The point was that I did not know that it is possible to re-declare a variable as "const" afterwards.
I mean, in a call-by-value condition, I would have understood that the new copy of my variable is function-internally declared const. But with passing the origin variable by reference, compiler addes the "const" status to my already existing variable, right?
So, shouldn't be the consequence that:
a) I can't change the stringlist after this function call or does compiler "un-const" the list after returning from the function again? Removing an item after this call didn't throw an error...
b) As I inserted the list-values by reference to the stringlist, shouldn't manipulating the stringlist after this function call affect the listwidget? Otherwise I wouldn't understand the sense why to pass the list by reference and why it has to be re-declared as "const"...And one more, maybe stupid question - sorry for that but I'm interessted in it and google won't give me an answer, maybe noone has ever asked that :-)
You both tell me not do use pointers that are allocated on the heap on QStringLists and other containers. Is there any kind of "rule" which class objects are "large enough" to allocate them on the heap? How do you manage this?Thank you really much for your support.
Greetings, Binary@Binary91-0 said in QListWidget::addItems() Data type question:
compiler addes the "const" status to my already existing variable, right?
No, it does not.
If you do not specify a by-value parameter as const you can change the variable which was passed as parameter:void foo(int &value) ; int myValue; foo(myValue) // Here foo can change myValue void foo(const int &value) // This one can't
-
Hey guys,
thank you for your fast replies.The point was that I did not know that it is possible to re-declare a variable as "const" afterwards.
I mean, in a call-by-value condition, I would have understood that the new copy of my variable is function-internally declared const. But with passing the origin variable by reference, compiler addes the "const" status to my already existing variable, right?
So, shouldn't be the consequence that:
a) I can't change the stringlist after this function call or does compiler "un-const" the list after returning from the function again? Removing an item after this call didn't throw an error...
b) As I inserted the list-values by reference to the stringlist, shouldn't manipulating the stringlist after this function call affect the listwidget? Otherwise I wouldn't understand the sense why to pass the list by reference and why it has to be re-declared as "const"...And one more, maybe stupid question - sorry for that but I'm interessted in it and google won't give me an answer, maybe noone has ever asked that :-)
You both tell me not do use pointers that are allocated on the heap on QStringLists and other containers. Is there any kind of "rule" which class objects are "large enough" to allocate them on the heap? How do you manage this?Thank you really much for your support.
Greetings, Binarya) I can't change the stringlist after this function call or does compiler "un-const" the list after returning from the function again? Removing an item after this call didn't throw an error...
No. It's not that the compiler modifies your variable's constness. It's just that when you pass it to a function that takes const reference it is treated inside of it like a const reference i.e. it is guaranteed that the function won't change the value of that variable.
b) As I inserted the list-values by reference to the stringlist, shouldn't manipulating the stringlist after this function call affect the listwidget? Otherwise I wouldn't understand the sense why to pass the list by reference and why it has to be re-declared as "const"...
Again no. You pass parameters by const reference to avoid a copy on the interface level, but you don't know what happens inside. In case of a list there's space allocated for a new value in the buffer and then the value you passed is copied in. If you passed the parameter by value instead of by reference there would be two copies - one at the interface level when passing a parameter and another to put the value inside the list. As for constness of the reference - this is a contract with you as the user of that function. It tells you that you can give this function a variable and it won't modify it so you can be sure your variable is in the same state before and after that call.
Maybe it would be helpful to get one thing straight - your processor has no notion of constness. There's just memory, addresses and some arithmetic. Everything else is just language abstraction that gets compiled away. So to your processor an
int
and aconst int
is just a 32bit data in a register. It doesn't even know that there was someconst
in the source code or what it would even mean. On the language levelconst
is an API construct. It is used so you can tell others how they can use your variable and to tell you how to use other's variables i.e. when it is okay to write to it. When you have a function that takes const parameter all it does is tell you that it won't modify that variable. It doesn't restrict you on what you can give it, const or no const, it just says that your variable won't be modified inside the function. When a parameter is non-const reference the contract is "if you give me something I might modify it". Because of that you can't give it a const variable - it would try to modify something that is const and that would be an error so this won't compile.You both tell me not do use pointers that are allocated on the heap on QStringLists and other containers. Is there any kind of "rule" which class objects are "large enough" to allocate them on the heap? How do you manage this?
There are no hard rules, but there's something to keep in mind - stack allocations are a lot faster but there's limited amount of space on it (by default something around 1MB per thread on Windows). The heap on the other side is large (it's the whole RAM you have) but access to it is slow, because allocations on it go through the OS. So a rule of thumb is use stack as much as possible for small and local things - variables, parameters, small objects etc. Most containers fall under this definition because they are small - few bytes usually. The underlying data is allocated dynamically on the heap internally so there's no point in paying two times for dynamic allocation when all you need is the few bytes for the container. Also something to keep in mind is that a stack is just that - a stack. It means that when you put something on it in a scope, let's say inside a function, it will be taken off of it when you leave that scope (exit the function). So remember that stack variables are scope local. Heap allocations are managed entirely by you, independently of the scope. You decide when to allocate and when to delete it, but it's also your responsibility not to leak anything (allocate without deleting at some point).
-
a) I can't change the stringlist after this function call or does compiler "un-const" the list after returning from the function again? Removing an item after this call didn't throw an error...
No. It's not that the compiler modifies your variable's constness. It's just that when you pass it to a function that takes const reference it is treated inside of it like a const reference i.e. it is guaranteed that the function won't change the value of that variable.
b) As I inserted the list-values by reference to the stringlist, shouldn't manipulating the stringlist after this function call affect the listwidget? Otherwise I wouldn't understand the sense why to pass the list by reference and why it has to be re-declared as "const"...
Again no. You pass parameters by const reference to avoid a copy on the interface level, but you don't know what happens inside. In case of a list there's space allocated for a new value in the buffer and then the value you passed is copied in. If you passed the parameter by value instead of by reference there would be two copies - one at the interface level when passing a parameter and another to put the value inside the list. As for constness of the reference - this is a contract with you as the user of that function. It tells you that you can give this function a variable and it won't modify it so you can be sure your variable is in the same state before and after that call.
Maybe it would be helpful to get one thing straight - your processor has no notion of constness. There's just memory, addresses and some arithmetic. Everything else is just language abstraction that gets compiled away. So to your processor an
int
and aconst int
is just a 32bit data in a register. It doesn't even know that there was someconst
in the source code or what it would even mean. On the language levelconst
is an API construct. It is used so you can tell others how they can use your variable and to tell you how to use other's variables i.e. when it is okay to write to it. When you have a function that takes const parameter all it does is tell you that it won't modify that variable. It doesn't restrict you on what you can give it, const or no const, it just says that your variable won't be modified inside the function. When a parameter is non-const reference the contract is "if you give me something I might modify it". Because of that you can't give it a const variable - it would try to modify something that is const and that would be an error so this won't compile.You both tell me not do use pointers that are allocated on the heap on QStringLists and other containers. Is there any kind of "rule" which class objects are "large enough" to allocate them on the heap? How do you manage this?
There are no hard rules, but there's something to keep in mind - stack allocations are a lot faster but there's limited amount of space on it (by default something around 1MB per thread on Windows). The heap on the other side is large (it's the whole RAM you have) but access to it is slow, because allocations on it go through the OS. So a rule of thumb is use stack as much as possible for small and local things - variables, parameters, small objects etc. Most containers fall under this definition because they are small - few bytes usually. The underlying data is allocated dynamically on the heap internally so there's no point in paying two times for dynamic allocation when all you need is the few bytes for the container. Also something to keep in mind is that a stack is just that - a stack. It means that when you put something on it in a scope, let's say inside a function, it will be taken off of it when you leave that scope (exit the function). So remember that stack variables are scope local. Heap allocations are managed entirely by you, independently of the scope. You decide when to allocate and when to delete it, but it's also your responsibility not to leak anything (allocate without deleting at some point).
wrote on 30 Mar 2020, 09:39 last edited by Binary91 0Ah, now I think I got it!
I did not know the internal management of QListWidget. So, to repeat, a QListWidget always copies the list I passed to it and stores this copy into a new allocated RAM.
Now, to avoid another copy for the function itsself to work with, I pass the source list by reference. As this is a potential risk to my source list, the Qt's function guarantees not to harm my list, and this guarantee is done by declaring the function parameter as "const". But "const" in this sense does not mean to change my variable into a const (this is still not possible, I hope), but instead it is like a "function character" to tell the compiler to take a look at the function algorithm and to ensure that it will not manipulate the variable.I hope, I got this right. If not, tell me please :-)
"There are no hard rules, but there's something to keep in mind - stack allocations are a lot faster but there's limited amount of space on it (by default something around 1MB per thread on Windows). The heap on the other side is large (it's the whole RAM you have) but access to it is slow, because allocations on it go through the OS. So a rule of thumb is use stack as much as possible for small and local things - variables, parameters, small objects etc."
Ok, I will notice that and I think I'll have to study the internal management of containers. I didn't know that Qt internally allocated heap for my container items. So, what would happen if I create a general object list containing 5 QWidgets, for example in a QVector? The container itsself will be allocated on the stack, but the items inside are already allocated dynamically. Will Qt then internally re-allocate them dynamically after storing them into a list container?Greetings, Binary
-
Ah, now I think I got it!
I did not know the internal management of QListWidget. So, to repeat, a QListWidget always copies the list I passed to it and stores this copy into a new allocated RAM.
Now, to avoid another copy for the function itsself to work with, I pass the source list by reference. As this is a potential risk to my source list, the Qt's function guarantees not to harm my list, and this guarantee is done by declaring the function parameter as "const". But "const" in this sense does not mean to change my variable into a const (this is still not possible, I hope), but instead it is like a "function character" to tell the compiler to take a look at the function algorithm and to ensure that it will not manipulate the variable.I hope, I got this right. If not, tell me please :-)
"There are no hard rules, but there's something to keep in mind - stack allocations are a lot faster but there's limited amount of space on it (by default something around 1MB per thread on Windows). The heap on the other side is large (it's the whole RAM you have) but access to it is slow, because allocations on it go through the OS. So a rule of thumb is use stack as much as possible for small and local things - variables, parameters, small objects etc."
Ok, I will notice that and I think I'll have to study the internal management of containers. I didn't know that Qt internally allocated heap for my container items. So, what would happen if I create a general object list containing 5 QWidgets, for example in a QVector? The container itsself will be allocated on the stack, but the items inside are already allocated dynamically. Will Qt then internally re-allocate them dynamically after storing them into a list container?Greetings, Binary
I hope, I got this right. If not, tell me please :-)
Yeah, that's close enough. There's basically some data object somewhere in the RAM. When you refer to it via your original variable it is non-const i.e. you can write to it. When you pass it to the function via const reference it creates another "view" so to speak, of that same area in RAM, only this time this view (or variable, whatever you call it) is treated as read only (const).
Ok, I will notice that and I think I'll have to study the internal management of containers.
That's how most containers work. Basically the
sizeof(yourContainer)
does not change (it can't really, it's a static type size) and the storage for data it holds is dynamically allocated/reallocated/deleted as an implementation detail of fuctions likeappend
,push_back
,erase
,clear
etc.So, what would happen if I create a general object list containing 5 QWidgets, for example in a QVector?
You can't. To put an object into a QVector it needs to be copyable. QObjects are not copyable (they have deleted copy constructors and assignment operators). In most cases you store pointers to widgets in containers, not widgets.
The container itsself will be allocated on the stack, but the items inside are already allocated dynamically. Will Qt then internally re-allocate them dynamically after storing them into a list container?
Your container holds some memory on the stack. in that memory it has a size counter and a pointer to dynamically allocated area of memory. In that memory it stores whatever the type of your container is. When you put something in a container it reallocates the memory to fit the new data and then calls a copy constructor to put the new thing in. It does not modify the original in any way. In case of pointers to widgets it just copies those pointers, so you get two containers containing pointers to the same widget object. Kinda like:
QWidget* w1 = new QWidget(); QWidget* w2 = w1; //now they point to the same thing but there's still only one widget
Except you copy entire container of those pointers, not just one. In case of
QStringList
you copy strings like you copy pointers, by value, i.e. you get another container with a fresh copy of those string values. -
I hope, I got this right. If not, tell me please :-)
Yeah, that's close enough. There's basically some data object somewhere in the RAM. When you refer to it via your original variable it is non-const i.e. you can write to it. When you pass it to the function via const reference it creates another "view" so to speak, of that same area in RAM, only this time this view (or variable, whatever you call it) is treated as read only (const).
Ok, I will notice that and I think I'll have to study the internal management of containers.
That's how most containers work. Basically the
sizeof(yourContainer)
does not change (it can't really, it's a static type size) and the storage for data it holds is dynamically allocated/reallocated/deleted as an implementation detail of fuctions likeappend
,push_back
,erase
,clear
etc.So, what would happen if I create a general object list containing 5 QWidgets, for example in a QVector?
You can't. To put an object into a QVector it needs to be copyable. QObjects are not copyable (they have deleted copy constructors and assignment operators). In most cases you store pointers to widgets in containers, not widgets.
The container itsself will be allocated on the stack, but the items inside are already allocated dynamically. Will Qt then internally re-allocate them dynamically after storing them into a list container?
Your container holds some memory on the stack. in that memory it has a size counter and a pointer to dynamically allocated area of memory. In that memory it stores whatever the type of your container is. When you put something in a container it reallocates the memory to fit the new data and then calls a copy constructor to put the new thing in. It does not modify the original in any way. In case of pointers to widgets it just copies those pointers, so you get two containers containing pointers to the same widget object. Kinda like:
QWidget* w1 = new QWidget(); QWidget* w2 = w1; //now they point to the same thing but there's still only one widget
Except you copy entire container of those pointers, not just one. In case of
QStringList
you copy strings like you copy pointers, by value, i.e. you get another container with a fresh copy of those string values.wrote on 30 Mar 2020, 16:24 last edited by Binary91 0@Chris-Kawa Thank you really much for this detailed support. Now I think it is clear for me.
What do you think is the best way to manage multidimensional string-lists that have to be viewed in 3 seperate QListWidgets that depend on each other?
Example:
Main QListWidget (#1) has 3 items. When the user clicks on one of those items, the second QListWidget displays the associated items, let's say again 3 items. When the user clicks on one of those 3 "sub-items", again 3 more items should be displayed in the third QListWidget.Till now, I'm using complicated, multi-dimensional QLists like this:
A simple QStringList for level #1, a QList<QStringList> for level #2 and a QList<QList<QStringList>> for level #3.
I catch user selection using "currentRowChanged", start a complicated for-loop to walk through the levels and choose the associated QStringList, clear the QListWidget and add the new List.Is there an easier and more performant way to manage this? Would QVector be a better solution for that? Or is a completely different approach better?
-
@Chris-Kawa Thank you really much for this detailed support. Now I think it is clear for me.
What do you think is the best way to manage multidimensional string-lists that have to be viewed in 3 seperate QListWidgets that depend on each other?
Example:
Main QListWidget (#1) has 3 items. When the user clicks on one of those items, the second QListWidget displays the associated items, let's say again 3 items. When the user clicks on one of those 3 "sub-items", again 3 more items should be displayed in the third QListWidget.Till now, I'm using complicated, multi-dimensional QLists like this:
A simple QStringList for level #1, a QList<QStringList> for level #2 and a QList<QList<QStringList>> for level #3.
I catch user selection using "currentRowChanged", start a complicated for-loop to walk through the levels and choose the associated QStringList, clear the QListWidget and add the new List.Is there an easier and more performant way to manage this? Would QVector be a better solution for that? Or is a completely different approach better?
@Binary91-0 To avoid linear search you can use a QMap<QString, QstringList> for second and third list widgets. Key is the string selected in the previous list widget, value is the list of strings asotiated with that key.
-
wrote on 31 Mar 2020, 07:34 last edited by JonB
@jsulm is probably the quickest/simplest suggestion to suffice you. However, being purist, be aware that as it stands it "cuts a corner": if you can have the same string text at different levels of the tree with different children (i.e. the fact that they are the same string is a "coincidence"), it will not work without some further massaging.
You could just invent a proper data structure to hold you data correctly. Create a
class
/struct
, namedNode
, which holds aQString text
for its string plus aQList<Node>
for its children. That is what your data really is. Finding a node by text at a given level is a linear search through its parent's child list (only).I would start by looking at whatever data structure you currently have for your items and their sub-items and take it from there.
-
wrote on 31 Mar 2020, 09:04 last edited by
@jsulm This sounds great! I did not test it so far, but after reading the documentation part, it seems to exactly fit my intentions.
@JonB Good aspect. Fortunatelly, no literally identical values will appear in the lists. The node system is very interessting. I think, for this purpose with only displaying the next level list-widget, maybe the node system is not needed, but it sounds like a good way to automatically walk through complex lists.
In addition, I will maybe use a combination of both. What do you think about inheriting QString in a new class and adding a specifier like a pointer to the next level string-list to it. Then, on the one hand, I get my fast interaction by QMap and as a backup (e. g. for storage and loading procedures of the whole data) I will always have unique connections.How would you guys manage the local storage of such three-dimensional data structures? I know database management from PHP server-based, but I have never done such stuff with Qt in a local directory. Is it a possible and "easy to learn" method for this purpose or do you suggest any other storage method?
-
@jsulm This sounds great! I did not test it so far, but after reading the documentation part, it seems to exactly fit my intentions.
@JonB Good aspect. Fortunatelly, no literally identical values will appear in the lists. The node system is very interessting. I think, for this purpose with only displaying the next level list-widget, maybe the node system is not needed, but it sounds like a good way to automatically walk through complex lists.
In addition, I will maybe use a combination of both. What do you think about inheriting QString in a new class and adding a specifier like a pointer to the next level string-list to it. Then, on the one hand, I get my fast interaction by QMap and as a backup (e. g. for storage and loading procedures of the whole data) I will always have unique connections.How would you guys manage the local storage of such three-dimensional data structures? I know database management from PHP server-based, but I have never done such stuff with Qt in a local directory. Is it a possible and "easy to learn" method for this purpose or do you suggest any other storage method?
wrote on 31 Mar 2020, 09:33 last edited by@Binary91-0 said in QListWidget::addItems() Data type question:
What do you think about inheriting QString in a new class and adding a specifier like a pointer to the next level string-list to it.
No! Your structure, with a string text plus children, is not itself a
QString
, and does not behave anything like one. So inheritance here would be wrong, use encapsulation (memberQString
plus children, as I wrote). -
wrote on 31 Mar 2020, 16:54 last edited by
Ok, I understand.
But if I am right, there is still no direct way to get a connection from a selected list item to the node, right? For example, I create a complex node hirarchy and pass the string-values of those nodes to the QListWidget, including duplicates. How is it possible now to directly associate a selected list item with the corresponding node? Do I simultaniously have to mirror the list structure in my nodes and catch the "currentRow" signal to walk through my node structure till I found the corresponding item?
If this was the way I need to go, I'm asking myself how to easily react to changes in the list widget, e.g. changing the sort-mode from ascending alphabetically to descending alphabetically. Do I have to start a complex sort mechanism that walks through my nodes and sorts them by a given identifier or stuff like that?I mean, the perfect way would be to directly inherit from a list widget item or from QString to "infiltrate" the list so I can directly react to user selection with the corresponding node.
Or am I missing the point?
-
One possible answer is that a list widget is not the best tool for this task. QListWidget is sort of a entry level widget if you don't need anything fancy. Usually when whatever customization needs to be done going for a lower level solution turns out to be far more suitable. You could implement an item model with a tree structure of nodes and then have 3 QListViews that point to different levels of the tree. On selection change you would simply get the selected row index and switch the views to point to the right tree node. I discourage you from using strings as indices. It's slow and gets complicated with non-unique strings. It's better to just deal with QModelIndexes. Handling sorting and filtering in this case could be done via QSortFilterProxyModel, which would handle the translation of indices from what's in the view to what's in the model. See Model/View Programming for details and specifically Proxy Models section for handling filters and sorting.
-
wrote on 4 Apr 2020, 17:48 last edited by
Ok, sorry for the belated reply, but it took me some hours to carefully read the whole topic to Model/View Programming. Thank you for that hint, I think I know understood the basic principle of this concept!
I'm trying to solve my issue with a tree model now, what gives me most flexibility. For this, I'm coding an example as described in the link you posted (Simple Tree Model Example).
Here, I have a problem understanding what we already discussed eralier: The syntax of a function call with a const reference of X within this function call:int TreeItem::row() const { if (m_parentItem) return m_parentItem->m_childItems.indexOf(const_cast<TreeItem*>(this)); return 0; }
What is that "const_cast" good for? My google research told me that, in a const function like the above, the "this"-pointer is a const pointer to a CONST object. Instead, in a non-const function, the "this"-pointer was a const pointer to a NON-CONST object. Am I right?
Why do I have to use this const_cast now to call a const function? I mean, the "indexOf"-function is const, so why shouldn't it be possible to pass a const object?Sorry, but I really try to understand that stuff..
-
@Binary91-0 said in QListWidget::addItems() Data type question:
I mean, the "indexOf"-function is const, so why shouldn't it be possible to pass a const object?
Because m_childItems contains TreeItem pointers, not const TreeItem pointers.
-
@Binary91-0 said in QListWidget::addItems() Data type question:
I mean, the "indexOf"-function is const, so why shouldn't it be possible to pass a const object?
Because m_childItems contains TreeItem pointers, not const TreeItem pointers.
wrote on 4 Apr 2020, 18:08 last edited by@Christian-Ehrlicher So, it is not possible to pass either TYPE &var or const TYPE &var to a function that declares its argument itsself as const? What is the sense of that?
-
Lifetime Qt Championwrote on 4 Apr 2020, 18:12 last edited by Christian Ehrlicher 4 Apr 2020, 18:13
@Binary91-0 said in QListWidget::addItems() Data type question:
that declares its argument itsself as const?
I don't understand - m_childItems is of type QList<TreeItem*> - so indexOf() expects a TreeItem pointer, not a const TreeItem pointer. Therefore you have to cast the const this pointer to a non-const pointer. An automatic conversion from a const to a non-const value is not allowed (otherwise const would be completely useless when you think about it)
-
@Binary91-0 said in QListWidget::addItems() Data type question:
that declares its argument itsself as const?
I don't understand - m_childItems is of type QList<TreeItem*> - so indexOf() expects a TreeItem pointer, not a const TreeItem pointer. Therefore you have to cast the const this pointer to a non-const pointer. An automatic conversion from a const to a non-const value is not allowed (otherwise const would be completely useless when you think about it)
wrote on 4 Apr 2020, 19:52 last edited by Binary91 0 4 Apr 2020, 21:46@Christian-Ehrlicher Ok, I know that I am wrong, but I try to explain what I do not understand:
The syntax of indexOf is:indexOf(const T &value) const
In my case, "T" is a TreeItem*, so the argument would be a "const TreeItem* &" what means a reference to a pointer of a const TreeItem, or not?
EDIT:
Ah, no. "T" is a Pointer to a TreeItem, and the indexOf()-function requires a const T &, so that means it requires a reference to a const pointer to a TreeItem, BUT NOT a const pointer to a CONST TreeItem, right?
And what does the const ensure in this function? Does it ensure that the pointer will not point to another QTreeItem after the function call or does it ensure that the QTreeItem will not be modified in any way? Or both?
1/33