Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Sql-model-roleNames in TableView and ListView not working similiar?

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tschikkn
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

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

      157

      1 Reply Last reply
      1
      • T Offline
        T Offline
        Tschikkn
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JosephMills
          wrote on last edited by
          #4

          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 ?

          T 1 Reply Last reply
          0
          • J JosephMills

            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 ?

            T Offline
            T Offline
            Tschikkn
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved