PDA

View Full Version : How do I use a QStringList in a constructor?



OzQTNoob
3rd February 2012, 07:49
I have copied an example off of the web where they set up a model (trying to get my head around this stuff) and it all works quite well. I wanted to extend it though so I could have the constructor of the model class use a qstringlist of filenames to initialize the model. Being the absolute beginner in Qt and C++ I am quite confused as to how to proceed when I get the following error:

error: default argument missing for parameter 2 of 'QStandardItemModel* createModel(QObject*, QStringList (*)())'

originally I was trying to do this:

QStandardItemModel* createModel(QObject* parent=0, QStringList filenames); but i would still get the error above.

What I am trying to do is set up a model that is of the form
Filename
-item1
-item2
-item3
--item3a
--item3b

and so on. I have that working for a single case where the constructor only has the parent as input, and where the items are bits of information of course. But i want to be able to use that same structure to model n Filenames and where I dont actually know how many files there are until I get some input back from the UI. My thinking above was that if I had a constructer with 2 parameters where the 2nd is the qstringlist of filenames, then even if I dont populate the items immediately i can at least create my model

Filename1
-item1
-item2
-item3
--item3a
--item3b

Filename2
-item1
-item2
-item3
--item3a
--item3b

etc

any help is appreciated, even if its to tell me that this approach is sooooo wrong
Cheers
Oz

FelixB
3rd February 2012, 07:56
Hi,

change the argument order in your constructor. Arguments with default values must appera *after* the other parameters. This will work:


QStandardItemModel* createModel(QStringList filenames, QObject* parent=0);

btw, I'd change it to "const QStringList& filenames"...

hth,
Felix

OzQTNoob
3rd February 2012, 08:14
That worked Felix. Thankyou. Just out of interest is it a bad way to set up the model? Its all quite baffling at times even with the various documentation around.

And is it bad practice to set up my model so that the items, for example, that I have used in my first post are actually item names? I assume that if I take that path then my data must go in the 2nd column of my model

e.g.
QStandardItemModel* model = new QStandardItemModel(countfnames, 2, parent);

so a user who views the model (a tree) can actually see the name and the values associated with it.

Cheers
Oz

ChrisW67
3rd February 2012, 22:07
G'day OzQtNoob

I'm not exactly sure what you are getting at. If you mean you want to apply a list of row (or column) headings then I suggest you look at the QStandardItemModel::setHorizontalHeaderLabels() (and the vertical equivalent). These labels will appear in the expected place in QTableView for example and are outside of the data area of the model.