[Aptitude-devel] aptitude-qt code design

Sune Vuorela nospam at vuorela.dk
Tue Jun 8 08:58:45 UTC 2010


On 2010-06-07, Daniel Burrows <dburrows at google.com> wrote:
>> Example of displaying package list:
>> 1) The view is created
>> 2) The widget is asking model what should be displayed on the list
>> 3) The model ask the service for the informations
>> 4) Informations are displayed on the list
>
>   I don't understand what happens in step 3.

Qt's model-view things is a bit hard to wrap your hed around first time
you have to do it, but basically, it is a class to make some arbitrary
structure or collection fit in one of Qt's view classes.

Normally, a model can be either list, table or tree based (and
similar for the view).

If you are curious about the Qt model-view,
http://doc.qt.nokia.com/latest/model-view-programming.html is the full
documentation for the 'how it works'. I didn't understand it until I
actually used it a couple of times.

A bit of code about wrapping a simple list of objects as table model:
(subclassing QAbstractTableModel)

QVariant  myclass:: data ( const QModelIndex & index, int role ) const {
    if(Qt::DisplayRole==role) {
    	if(index.column()==0) {
	    return m_list.at(index.row())->name();
	} else if (index.column()==1) {
	    return m_list.at(index.row())->age();
	}
    }
    //we don't handle other roles for this model
    return QVariant();
}

int myclass::columnCount() const {
    return 2;
}

int myclass::rowCount() const {
    return m_list.size();
}


And then somewhere else, do QTableView* view = new QTableView();
view->setModel(new myclass(datalist));

and you magically have a table of name,age.

A model can also be 'lazily filled' as needed if there is either very
much data, or if the data is expensive to fetch.

In the given case by Piotr, the access to m_list should just instead
call into somewhere upwards in the aptitude code.

/Sune




More information about the Aptitude-devel mailing list