[Interest] C++/QML integration with QAbstractTableModel and QML Repeater

Christoffer Holmstedt christoffer.holmstedt at gmail.com
Fri Sep 26 08:55:16 CEST 2014


Ok, I got help on IRC from "sletta" a few days ago and I have a solution
available on Github for those that end up in this mailing list post when
searching [1] (that is in the mailing list archive). The answers below are
valid for Qt 5.3 and QtQuick 2.3 which I have used in my development
probably some earlier versions as well and maybe some upcoming versions. To
answer my questions in my first post:

1) Yes, the repeater only increases index.row() (see answer to #2 below)

2) Yes, QtQuick / QML only supports Lists and not the QAbstractTableModel.
I solved this by mapping my implementation of the QAbstractListModel to the
underlying std::vector of std::vector by introducing two helper functions
(index.row() mapped to row and column). [2]

3) I was wrong, I had to change in the c++ code (see answer to #2 above)

[1] https://github.com/christofferholmstedt/MineSweeper/releases/tag/v0.1.1
[2]
https://github.com/christofferholmstedt/MineSweeper/blob/development/boardmodel.cpp#L151

Hope it can help someone in the future.
-- 
Christoffer Holmstedt

2014-09-15 15:34 GMT+02:00 Christoffer Holmstedt <
christoffer.holmstedt at gmail.com>:

> Hi
> Just started to learn how Qt works with QtQuick (Qt 5.3.1). I have
> successfully tried different examples available in the docs but there is
> one thing I can't get my head around and that is when subclassing
> QAbstractTableModel (QAbstractListModel works just fine).
>
> Code available on github [1] (run qmlscene for expected result).
>
> What I'm trying to achieve is a simple 2D grid of squares. With a QList<T>
> and QAbstractListModel the Repeater it just works, but when I changed the
> data structure to a std::vector<std::vector<T>>> and QAbstractTableModel I
> only get the first element in the outer vector. Simple debug output from my
> BoardModel::data member function suggests that index.column() is always 0
> when used with the QML Repeater view.
>
> 1) Is the assumption above correct that the Repeater view only increases
> the index.row() value when requesting data?
>
> 2) Is this expected behaviour for all QML views? (even the tableview
> requires the columns to be specific roles afaik).
>
> 3) My best bet now is that it's in the front-end/QML code I need to change
> to another view type/element to properly request data in all rows AND
> columns. How is this best done?
>
> Any suggestion pointing me in the right direction is greatly appreciated.
>
> [1] https://github.com/christofferholmstedt/MineSweeper
>
> Regards
> --
> Christoffer Holmstedt
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140926/77f4e3e1/attachment.html>


More information about the Interest mailing list