QAbstractTableModel C++ and tableView QML



  • Guys, morning!
    Im trying to put a QAbstractTableModel made in c++ in a view made with QML, but isnt drawing nothing in the view.
    This is my code:
    main.cpp

         fieldModel model;
        engine.rootContext()->setContextProperty("myModel",&model);
    
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    

    main.qml

    ApplicationWindow {
        visible: true
        title: qsTr("Naval Battle")
        color: "black"
    
        TableView{
                id: _tableView
                model: myModel
                width: (Screen.width)/2
                height: (Screen.height)/2
                headerVisible: false
                itemDelegate: Rectangle{
                            width: 40
                            height: 40
                            color: "blue"}
        }
    }
    

    Im reading the doc of TableView QML but, there only show how to draw a column, a row or a cell. Im thinking that the view will apply to all cells my itemDelegate property?


  • Lifetime Qt Champion

    Hi,

    Maybe a silly question but, is your model populated with something ?



  • Colums must be declared using TableViewColumn.



  • @SGaist Yes. I managed to do using a Grid, since it was not possible to make the tableview. Now I have my view, but i was looking at the GridView doc, but dont know... if is better keep the current code or try something different.
    This is my actual qml code:

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Window 2.2
    
    ApplicationWindow {
        visible: true
        width: Screen.width /2
        height: Screen.height /2
        Grid {
            id : tabuleiro
            columns: 10
            rows: 10
            anchors.fill: parent
            spacing : 1
    
            Repeater {
                delegate : Rectangle {
                    width: Math.min(tabuleiro.width, tabuleiro.height) / 10
                    height: width
                    color : isWater ? "blue" : "red";
                }
                model : myModel;
            }
        }
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.