[Qt-interest] Model/View Help

Jeroen De Wachter jeroen.dewachter at barco.com
Mon Jun 28 19:12:23 CEST 2010


Hey Colin,

The choice of using item-based or model-based widgets is really up to 
you, but I find that the model-based approach tends to result in cleaner 
code in your GUI classes, as the model reacts to changes. The item-based 
approach usually means lots of methods in the GUI classes to handle 
changes to the items (both from the data to the GUI and as a result of 
user interaction).

However, if you simply need to populate a table and have it display some 
information, without the need to modify it, then writing a model seems 
like overkill and you may be better off just filling in the information 
from the GUI class using QTableWidget::setItem.

Subclassing the QWidgets (QTableWidget / QTableView) themselves should 
not be necessary in any case, you just need to interact with them.

If you use a model-based approach, you will need to subclass a 
QAbstractItemModel that contains your QList and is able to return the 
data. You can also subclass a QAbstractTableModel instead, if your data 
only needs to be displayed in a table. QAbstractTableModel provides 
default implementations for most of the methods defined in 
QAbstractItemModel that you don't generally want to deal with when 
you're displaying things in a table (like parent/child relationships, 
which you likely don't have).
Just use the row as an index for your QList and use the columns to 
access the different members of your custom class.

I wrote a table model just last week and it only contains the rowCount, 
columnCount and data methods. Just read the documentation on 
QAbstractTableModel. You could also take a look at the addressbook 
example in the itemviews folder.

Kind regards,

Jeroen

Colin Kern wrote:
> Hello all,
>
> I'm new to models/view programming and I have a question.  Let's say I
> have a C++ class that defines some member variables, and I have a
> container storing a bunch of instances of this class (let's say it's a
> QList).  What I want to do is display a table where each row is an
> instance of this class, and each column is showing the value of one of
> the member variables.  What's the best way to do this?  Should I use
> QTableWidget or QTableView? Do I need to subclass one of the Qt model
> classes, or can I use an existing one?  I've been trying to figure out
> how to do this, but every solution I come up with seems more
> complicated than it should be.
>
> Thanks,
> Colin Kern
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>   



DISCLAIMER:
Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you.




More information about the Qt-interest-old mailing list