[Qt-interest] Initialize const Qt container class

Ian Thomson Ian.Thomson at iongeo.com
Mon Oct 25 17:44:53 CEST 2010


You can define your data in a struct and then copy it in to a QVector of 
the correct allocated size by accessing the QVector::data pointer.

Joshua Grauman wrote:
> Hello all,
> 
> I realize this may be just a C++ question, but I was wondering if maybe Qt 
> had some additions that helped here. I often have some const data 
> structures that I want to initialize with data and don't have a good 
> (readable) way to do it.
> 
> Here's what I'd like to do (with standard c types):
> 
> struct item
> {
>    char string[20];
>    int list[10];
> };
> 
> //the following is nice and readable, easy to modify, etc.
> const struct item items[4] = {
> {"string1",{1,2,3,5,6,0}},
> {"string2",{4,0}},
> {"string3",{2,5,0}},
> {"",{0}}
> };
> 
> This makes it very easy to read and change my data initialization. But I'd 
> like to do this with Qt classes (QString and QList<int>) instead. I don't 
> know of any clean way like this to initialize everything in that case and 
> I was wondering if anyone had any slick tricks to do this or something 
> like it (really it's the QList<int> that's the problem, the QString will 
> initialize easy). If not, how do people initialize their (const) data 
> structures like this? (Note, I know how to initialize data structures in 
> the standard way using <<, but was looking for something "cleaner"). I 
> don't mind if there's a non const way to do this, but that would be nice 
> since the data won't change. Thanks!
> 
> Josh
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list