Results 1 to 4 of 4

Thread: How do I use a QStringList in a constructor?

  1. #1
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default How do I use a QStringList in a constructor?

    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:
    Qt Code:
    1. QStandardItemModel* createModel(QObject* parent=0, QStringList filenames);
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I use a QStringList in a constructor?

    Hi,

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

    Qt Code:
    1. QStandardItemModel* createModel(QStringList filenames, QObject* parent=0);
    To copy to clipboard, switch view to plain text mode 

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

    hth,
    Felix

  3. The following user says thank you to FelixB for this useful post:

    OzQTNoob (3rd February 2012)

  4. #3
    Join Date
    Feb 2012
    Location
    Australia
    Posts
    39
    Thanks
    15
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How do I use a QStringList in a constructor?

    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.
    Qt Code:
    1. QStandardItemModel* model = new QStandardItemModel(countfnames, 2, parent);
    To copy to clipboard, switch view to plain text mode 

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

    Cheers
    Oz
    Last edited by OzQTNoob; 3rd February 2012 at 08:23.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I use a QStringList in a constructor?

    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.

Similar Threads

  1. what does the constructor do here?
    By LB4229 in forum Qt Programming
    Replies: 7
    Last Post: 22nd June 2011, 03:19
  2. Qpainter in Constructor
    By augusbas in forum Newbie
    Replies: 4
    Last Post: 28th October 2010, 00:45
  3. QImage constructor
    By Lele in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2007, 12:06
  4. Replies: 7
    Last Post: 2nd June 2006, 12:48
  5. What default constructor?
    By bitChanger in forum General Programming
    Replies: 5
    Last Post: 15th February 2006, 19:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.