Results 1 to 3 of 3

Thread: Best practices concerning QStandardItemModel

  1. #1
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Best practices concerning QStandardItemModel

    Hello,

    I have a set of custom widgets that each contain a QTableView and an underlying QStandardItemModel. Some of these widgets are dependant on the models contained within other widgets. Currently, I am simply passing around QStandardItemModel pointers to achieve this. As such, the dependant widgets need to know what each model it's using "looks" like (headers, # of rows/columns, etc.).

    Note: that the dependant widgets also need to respond to changes in the data they are depending on. Currently this is achieved by connecting a signal from a widget to a private slot of a dependant widget.

    A possible change would be to subclass QStandardItemModel and provide a data specific interface to the model. For example, if I had a table that stored car information, clients would use:
    Qt Code:
    1. Car *car = model->getCar(row)
    2. int numSeats = car->numSeats;
    To copy to clipboard, switch view to plain text mode 
    instead of:
    Qt Code:
    1. QStandardItem *item = model->item(row, col);
    2. int numSeats = item->data(Qt::DisplayRole).toInt();
    To copy to clipboard, switch view to plain text mode 
    I have several tables, so creating a wrapper interface for each one seems a little overkill, but at the same time the way I am currently doing things seems a little error prone.

    What is the best practice in this case?
    Any suggestions and ideas appreciated!

    Thanks

  2. #2
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Best practices concerning QStandardItemModel

    It sounds like you're trying to recreate the model-view wheel that Qt already provides: Model View Programming.

    The benefit to using models is that you could have several views share the data contained in one model - if data changes in the underlying model, then all of the views attached to that model get updated as well.

    Hope this helps.

    I forgot to add that if several views need tailored information from the model, then you can develop proxy models for each view to hide/customize the information. That may help alleviate the messiness you're experiencing.

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

    tim47 (27th January 2010)

  4. #3
    Join Date
    Jan 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Best practices concerning QStandardItemModel

    Thanks for the suggestions.
    Using proxy models has helped resolve some of the issues I was having before.

Similar Threads

  1. Good practices for export<->import SQLite datas
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2009, 05:24
  2. Replies: 1
    Last Post: 28th September 2008, 22:00
  3. query about best practices
    By Raajesh in forum Qt Programming
    Replies: 3
    Last Post: 13th June 2008, 19:47
  4. Signals/slots best practices?
    By gfunk in forum Qt Programming
    Replies: 5
    Last Post: 18th August 2007, 19:50
  5. Qt <-> Java - Best Practices?
    By mentat in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2006, 03:32

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.