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. My TableView Doesn't work
Qt 6.11 is out! See what's new in the release blog

My TableView Doesn't work

Scheduled Pinned Locked Moved QML and Qt Quick
12 Posts 3 Posters 5.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi everybody;
    Who can help me to solve my problem.

    cpp file :

    @

    QQmlApplicationEngine engine;

    QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("Select doc_number,first_name,last_name from document");
    model->setHeaderData(0, Qt::Horizontal, "ID");
    model->setHeaderData(1, Qt::Horizontal, "First Name");
    model->setHeaderData(2, Qt::Horizontal, "Last Name");

                   engine.rootContext()->setContextProperty("myModel", model);
    

    @

    And this is my QML file :

    @
    TableView {

        anchors.fill: parent
        model: myModel
    
    }
    

    @

    After run Tableview is empty !

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you setup the database connection properly ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        [quote author="SGaist" date="1400012262"]Hi,

        Did you setup the database connection properly ?[/quote]

        Hi again and thank you for your reply SGaist
        My setup is good work see :

        @
        bool OpenConnection(){
        try
        {

                    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
                    db.setHostName("localhost");
                    db.setDatabaseName("test");
                    db.setUserName("root");
                    db.setPassword("");
                    //Exception error rules.
                    qDebug() << QSqlDatabase::database().lastError();
                    QString vMessege;
                    vMessege = QSqlDatabase::database().lastError().text();
                    QMessageBox msgBox;
                    msgBox.setWindowTitle("Database SQL Exception Error !");
                    //msgBox.setIcon(QMessageBox::Icon::Critical);
                    msgBox.setText(vMessege);
                    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
                    msgBox.setDefaultButton(QMessageBox::No);
        
                    if (!db.open()) {
                            msgBox.exec&#40;&#41;;
                            return false;
                    }
        
            }
        
            catch (...)
            {
        
        
            }
        
            return true;
        

        }
        @

        and my main file :

        @
        if (OpenConnection())
        {

                  QSqlQueryModel *model = new QSqlQueryModel;
                       model->setQuery("Select doc_number,first_name,last_name from document");
                       model->setHeaderData(0, Qt::Horizontal, "ID");
                       model->setHeaderData(1, Qt::Horizontal, "First Name");
                       model->setHeaderData(2, Qt::Horizontal, "Last Name");
                       engine.rootContext()->setContextProperty("myModel", model);
        
        
          }
        
        engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
        

        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You should check what model->lastError() returns

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            [quote author="SGaist" date="1400053480"]You should check what model->lastError() returns[/quote]

            Check this ScreenShoot , anything is okey but doesn't work !
            http://persianupload.com/kleeja/do.php?imgf=140005525273092.jpg

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              And you are sure the table contains something ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                CMIIW, I think QSqlQueryModel doesn't work out of the box with QML.
                The reason is because QML model (as of now) has no notion of columns, but roles.
                In other words, you must set rolenames to display for each column for your tableview, and your model must return data in terms of roles.
                (see QAbstractItemModel::data(const QModelIndex & index, int role = Qt::DisplayRole) with your roles set from Qt::UserRole onwards.

                "This Wiki Entry":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML showed how to wrap your model in a way compatible with QML

                Why this very unintuitive discrepancy happened I do not know, and I surely want to know why as well

                Hope this helps

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  I'm getting old… lack is right, using c++ models with QML requires a bit of work.

                  You can find the documentation about it "here":http://qt-project.org/doc/qt-5/qtquick-modelviewsdata-cppmodels.html

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    I test this code for check is good work...

                    @
                    QStringList dataList;
                    dataList.append("Item 1");
                    dataList.append("Item 2");
                    dataList.append("Item 3");
                    dataList.append("Item 4");
                    engine.rootContext()->setContextProperty("myModel", dataList);

                    @

                    but now i want to show from database. ...

                    @
                    QSqlQueryModel *model = new QSqlQueryModel;
                    model->setQuery("Select doc_number,first_name,last_name from document");
                    model->setHeaderData(0, Qt::Horizontal, "ID");
                    model->setHeaderData(1, Qt::Horizontal, "First Name");
                    model->setHeaderData(2, Qt::Horizontal, "Last Name");
                    engine.rootContext()->setContextProperty("myModel", model);
                    @

                    doesn't work !!! Why ! what do i need on my QSqlQueryModel code ?

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lack
                      wrote on last edited by
                      #10

                      I already explained above.
                      It's not gonna work as-is.

                      QSqlQueryModel works with rows and COLUMNS.
                      QML views works with rows and ROLES.

                      A Stringlist is just a list, it will work. What you need to do, is make your model look like a list with roles.

                      Read "How_to_use_a_QSqlQueryModel_in_QML":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        [quote author="lack" date="1400243967"]I already explained above.
                        It's not gonna work as-is.

                        QSqlQueryModel works with rows and COLUMNS.
                        QML views works with rows and ROLES.

                        A Stringlist is just a list, it will work. What you need to do, is make your model look like a list with roles.

                        Read "How_to_use_a_QSqlQueryModel_in_QML":http://qt-project.org/wiki/How_to_use_a_QSqlQueryModel_in_QML[/quote]

                        Hi again and thank you for reply... but i try it on Qt 5.2 doesn't work !
                        Do you test it for yourself ? I need another example.

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lack
                          wrote on last edited by
                          #12

                          What have you tried and doesn't work? We need more information. 'Doesn't work' is not helpful.

                          If you have read the article, right at the end he showed a generic class extending QSqlQueryModel that is usable from QML.

                          I tested it against Qt 5.2, and yes it does work. Granted, with little work as QAbstractItemModel::setRoleNames was deprecated, you have to reimplement QAbstractItemModel::roleNames instead, but that is 1 minute work.

                          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