Sql-model-roleNames in TableView and ListView not working similiar?



  • Hello!

    I've a problem displaying sql-models derived from QSqlQueryModel and QSqlRelationalTableModel.
    The model itself is working, role names are generated and it works displaying it in a ListView. For testing I was using the class posted here.

    The main.cpp uses following code:

    int main(int argc, char *argv[])
    {
      QScopedPointer<QGuiApplication> app(new QGuiApplication(argc, argv));
      QScopedPointer<QQuickView> view(new QQuickView());
    
      SQLiteModel *sqmodel = new SQLiteModel();
    
      QFile sql(":///common/sql/overview.sql");
      if(!sql.open(QIODevice::ReadOnly | QIODevice::Text)) {
          qDebug() << "ajskdhfkjh";
      }
    
      QString query = sql.readAll();
      sqmodel->setQuery( query );
    
      QQmlContext *ctxt = view->rootContext();
      ctxt->setContextProperty("overviewModel", sqmodel);
    
      view->setSource(QUrl(QStringLiteral("qrc:/main.qml")));
      view->showNormal();
    
      return app->exec();
    }
    

    The QML-file looks like this:

    import QtQuick 2.3
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    
    Item {
        width: 640
        height: 640
        visible: true
        Column {
            TableView {
                height:320
                width: 640
                model: overviewModel
    
                TableViewColumn {
                    title: "Name"
                    role: Name
                }
                TableViewColumn {
                    title: "Amount"
                    role: AlltimeAmount
                }
                TableViewColumn {
                    title: "Total"
                    role: AlltimeTotal
                }
            }
    
            ListView {
                id: content
                height:2048
                width: 640
                model: overviewModel
                delegate:
                    Item {
                    width: 320
                    height: 320
                    Column {
                        Label {
                            width: parent.width
                            fontSizeMode: Text.Fit
                            text: Name
                        }
    
                        Grid {
                            id: grid
                            columns: 3
                            spacing: 6
    
                            Label { text: qsTr("Time") }
                            Label { text: qsTr("Amount") }
                            Label { text: qsTr("Value") }
    
                            Label { text: qsTr("Alltime") }
                            Label { text: AlltimeAmount }
                            Label { text: AlltimeTotal }
    
                            Label { text: qsTr("Year") }
                            Label { text: YearAmount }
                            Label { text: YearTotal }
    
                            Label { text: qsTr("Month") }
                            Label { text: MonthAmount }
                            Label { text: MonthTotal }
    
                            Label { text: qsTr("LastWeek") }
                            Label { text: LastWeekAmount }
                            Label { text: LastWeekTotal }
    
                            Label { text: qsTr("Week") }
                            Label { text: WeekAmount }
                            Label { text: WeekTotal }
    
                            Label { text: qsTr("Day") }
                            Label { text: DayAmount }
                            Label { text: DayTotal }
                        }
                    }
                }
            }
        }
    }
    

    The ListView is populated as expected, the TableView gives a "ReferenceError: [RoleName] is not defined"

    Someone may give me a hint what I did wrong?


  • Moderators

    @Tschikkn The TableViewColumn's role property expects a string. So just put double quotes around your roles in TableViewColumn and it should work.



  • Sometimes it could be that simple... Thank you!



  • Maybe you would like to take a look at my branch on github has all that stuff and then some.
    https://github.com/JosephMillsAtWork/QmlSql

    but it looks like this might be solved ?



  • @JosephMills Yes, it is solved. A wrapper class is not needed, I'm using qml just to display information on screen. Only did a mistake reading the same thing a hundred times and not recognizing that a string was expected. So p3c0's answer was the simple but needed solution.


Log in to reply
 

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