QT Learning Pathway (46): Custom Model II

Source: Internet
Author: User
Tags rowcount
QT Learning Pathway (46): Custom model of the second 2010-01-19 00:35:56 tags: qt c + + qt Tutorial Tutorial original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://devbean.blog.51cto.com/448512/267972 The previous example has given a clearer way to customize the model, which is to cover the few functions we need. However, the previous example is simply a presentation of data, which means that the data is read-only. So how can you read and write data? That's the example of coming in.   This example is also from C++gui programming with Qt 4, 2nd edition this book. Let's look at the code first: Citymodel.h class Citymodel:public Qabstracttablemodel
{
Q_object

Public
Citymodel (Qobject *parent = 0);

void setcities (const qstringlist &citynames);
int RowCount (const qmodelindex &parent) const;
int ColumnCount (const qmodelindex &parent) const;
Qvariant data (const QMODELINDEX &index, int role) const;
BOOL SetData (const qmodelindex &index, const qvariant &value, int role);
qvariant headerdata (int section, qt::orientation Orientation, int role) const;
Qt::itemflags flags (const QMODELINDEX &index) const;

Private
int offsetOf (int row, int column) const;

Qstringlist cities;
qvector< int> distances;
}; Citymodel.cpp Citymodel::citymodel (Qobject *parent)
: Qabstracttablemodel (parent)
{
}

int Citymodel::rowcount (const QMODELINDEX & Parent) const
{
return Cities.count ();
}

int Citymodel::columncount (const QMODELINDEX & Parent) const
{
return Cities.count ();
}

Qvariant Citymodel::d ata (const qmodelindex &index, int role) const
{
if (!index.isvalid ()) {
return Qvariant ();
}

if (role = = Qt::textalignmentrole) {
return Int (Qt::alignright | Qt::alignvcenter);
} else if (role = = Qt::D isplayrole) {
if (index.row () = = Index.column ()) {
return 0;
}
int offset = offsetOf (Index.row (), Index.column ());
return Distances[offset];
}
return Qvariant ();
}

qvariant citymodel::headerdata (int section, qt::orientation Orientation, int role) const
{

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.