QML TableView questions

Tomaz Canabrava tcanabrava at kde.org
Wed Feb 17 10:28:47 PST 2016


On Wed, Feb 17, 2016 at 7:40 AM, Willem Ferguson <
willemferguson at zoology.up.ac.za> wrote:

> I am using the existing class DiveImportedModel to adapt the existing code
> for mobile use. I use a TableView QML item to view the data but it apperas
> that QML hates the existing model that is aimed at a QTableView widget. I
> suspect this is because the roles have not been defined appropriately for
> the QML. So I am trying to access the existing roles and I cannot.
>
> C++ CODE:
>     d_importedModel = new DiveImportedModel(this);
>     clear_table(&downloadTable);
>     printf("** # columns = %d\n",d_importedModel->columnCount());
>
    printf("** Title of 1st column = %s\n",(d_importedModel->headerData(0,
> Qt::Horizontal, Qt::DisplayRole)).toString().toLatin1().data());
>

please, use qDebug()
qDebug() << "Columns = " << d_importedModel->columnCount();
qDebug() << "Title of 1st column = " << d_importedModel->headerData(0,
Qt::Horizontal)).toString();

easier to read. :)

QHash<int, QByteArray> roleValues = d_importedModel->roleNames();//
> *************** here I get the roles.
>     printf("** # items in hash = %d\n",roleValues.size());
>     QHashIterator<int, QByteArray> i(roleValues);
>     int j = 1;
>     while (i.hasNext()) {
>         i.next();
> (1)        QByteArray ba = i.key();
> (2)        printf("** %d - (%d) ",j++,(i.key()).length());
>     }
>
> OUTPUT:
> ** # columns = 3
> ** Title of 1st column = Date/time
> ** # items in hash = 6
> ** 1 -
> ** 2 -
> ** 3 -
> ** 4 -
> ** 5 -
> ** 6 -
>
> WARNINGS/ERRORS:
> (1) note: initializing argument 1 of ‘QByteArray::QByteArray(const char*,
> int)’
>      QByteArray(const char *, int size = -1);
>
> (2) error: request for member ‘length’ in ‘i.QHashIterator<Key,
> T>::key<int, QByteArray>()’, which is of non-class type ‘const int’
> printf("** %d - (%d) ",j++,(i.key()).length());
>
> From the above it is clear that a valid importedModel object is created.
> BUT: messages above indicate that the key() method of QHash returns an
> INTEGER, not a QByteArray.
>
> My questions:
> 1) In the above, how do I visualise the existing roles?
>

you don't really see the roles on QML. I usually prefer to do a different
thing:
create a DiveComputerObjectHelper : public QObject, export it's properties.
create the model with only one role: DiveComputerObjectHelper* and export
only that to the roles, name it 'divecomputer'

then on the qml side:

model : my_dc_models;

delegate : Rectangle {
      Text {
            divecomputer.name // here the divecomputer is the
model.currentIndex.divecomputer, easily accessible.
      }
}


2) Within the broader QML context, where should one synchronise the roles
> with the existing heading titles? Could one do it in a similar way to the
> headerData() method of the QAbstractTableModel by using re-implementing the
> roleNames() method?
> Kind regards,
> willem
>
>
> _______________________________________________
> subsurface mailing list
> subsurface at subsurface-divelog.org
> http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.subsurface-divelog.org/pipermail/subsurface/attachments/20160217/f115bff8/attachment-0001.html>


More information about the subsurface mailing list