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. Using QSqlQueryModel in QML without reimplement it [Solved]
Forum Updated to NodeBB v4.3 + New Features

Using QSqlQueryModel in QML without reimplement it [Solved]

Scheduled Pinned Locked Moved QML and Qt Quick
9 Posts 4 Posters 7.1k 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.
  • L Offline
    L Offline
    luca
    wrote on last edited by
    #1

    Hi all,
    I'm starting with QML and I'm trying with a simple "example":http://developer.qt.nokia.com/wiki/How_to_use_a_QSqlQueryModel_in_QML.

    Now I'd like to know if it's possible to use QSqlQueryModel without the need of subclass it.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Yes, you can use QSqlQueryModel directly, but note that you can only access the first column of data.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        My qml file is:
        @
        import QtQuick 1.0
        import Qt 4.7

        Rectangle {
        color: "lightblue"
        Component {
        id: name_delegate
        Text {
        text: "ciao"
        }
        }

        ListView {
            clip: true
            anchors.margins: 10
            anchors.fill: parent
            model: queryModel
            delegate: name_delegate
        }
        

        }
        @

        in my main window constructor I have:
        @
        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
        {
        ui->setupUi(this);

        QSqlQueryModel *queryModel;
        queryModel = new QSqlQueryModel(this);
        queryModel->setQuery("select descrizione from lista_dati;");
        
        m_declarativeView = new QDeclarativeView(this);
        m_declarativeView->rootContext()->setContextProperty("queryModel", queryModel);
        setCentralWidget(m_declarativeView);
        m_declarativeView->setSource(QString("qrc:rappresentazionegraficamagazzino.qml"));
        m_declarativeView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
        

        }
        @

        How can I access the query column?

        1 Reply Last reply
        0
        • L Offline
          L Offline
          luca
          wrote on last edited by
          #4

          No one can explain how to access QSqlQueryModel directly?

          For me it's also usefull to have some documentation about...

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DenisKormalev
            wrote on last edited by
            #5

            As Andre said you can access only first column of data. To access all columns you need to subclass it and make all columns visible at first column via different roles.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Then again, his query only returns a single column of data anyway, so that should not be a huge issue.

              It seems to me at first sight, that what you are doing is correct. Perhaps "this page":http://doc.trolltech.com/4.7.1/qdeclarativemodels.html#qabstractitemmodel in the documentation is of help: you need to use the role names. The default role name for the DisplayRole is display.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on last edited by
                #7

                [quote author="Andre" date="1302851464"]The default role name for the DisplayRole is display. [/quote]

                Thanks Andre. I needed this. Now It works.

                Where should I find the role name you posted me in the documentation?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  luca
                  wrote on last edited by
                  #8

                  [quote author="Luca" date="1302950174"][quote author="Andre" date="1302851464"]The default role name for the DisplayRole is display. [/quote]

                  Thanks Andre. I needed this. Now It works.

                  Where should I find the role name you posted me in the documentation?[/quote]

                  I found it in the link you posted...

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    antonio
                    wrote on last edited by
                    #9

                    Hi Luca, Andre someone may update the link that talks about role names? I'm facin the same problem.
                    thanks

                    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