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. How to Using tableView model in C++ code with UI in QML
Forum Updated to NodeBB v4.3 + New Features

How to Using tableView model in C++ code with UI in QML

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

    Hi everybody;

    Who can help me to solved my problem :)
    I want show data from database into qml Tableview

    How can i create model and show my database data into my qml ui.

    main file :

    @
    QQmlApplicationEngine engine;
    MyData data;
    engine.rootContext()->setContextProperty("MyData", &data);
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

    @

    This is my QML file :

    @
    TableView {
    model: MyData.KZ()
    anchors.margins: 12
    anchors.fill: parent
    TableViewColumn {
    role: "id"
    title: "ID"
    width: 120
    }
    TableViewColumn {
    role: "firstname"
    title: "First Name"
    width: 120
    }
    TableViewColumn {
    role: "lastname"
    title: "Last Name"
    width: 120
    }
    }
    @

    This is my function in Widget mode :

    @
    public:
    void KZ() {

    if (OpenConnection())
    {
    
    }
    
    else
    {
    
    
     QSqlQueryModel * model = new QSqlQueryModel();
     QSqlQuery * query = new QSqlQuery();
     query->prepare("Select doc_number,first_name,last_name from document");
     query->exec();
    
     model->setQuery(*query);
     ui->tableView_2->setModel(model);
     ui->tableView_2->model()->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
     ui->tableView_2->model()->setHeaderData(1, Qt::Horizontal, QObject::tr("First Name"));
     ui->tableView_2->model()->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Name"));
    
    }
    

    }
    @

    I need to write again this function for QML..

    my function is wrong for qml :

    @
    public:
    Q_INVOKABLE QVariant KZ() {

    if (OpenConnection())
    {
    

    QSqlQueryModel * model = new QSqlQueryModel();
    QSqlQuery * query = new QSqlQuery();
    query->prepare("Select doc_number,first_name,last_name from document");
    query->exec();
    model->setQuery(*query);

     ui->tableView_2->setModel(model);
     ui->tableView_2->model()->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
     ui->tableView_2->model()->setHeaderData(1, Qt::Horizontal, QObject::tr("First Name"));
     ui->tableView_2->model()->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Name"));
    

    return .... what ?
    }

    else
    {
    
    
    
    
    }
    

    @

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      I am not sure but you should be able to just use your QSqlQueryModel and set that as the QML TableView model!?
      You just need to set QSqlQueryModel as a property or have a getter from QML to c++ to use it.
      small example:
      @
      TableView {
      model: cppInterface.getMyModel() // Q_INVOKABLE function of type QSqlQueryModel
      ...
      }
      @

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

        [quote author="Xander84" date="1399759722"]I am not sure but you should be able to just use your QSqlQueryModel and set that as the QML TableView model!?
        You just need to set QSqlQueryModel as a property or have a getter from QML to c++ to use it.
        small example:
        @
        TableView {
        model: cppInterface.getMyModel() // Q_INVOKABLE function of type QSqlQueryModel
        ...
        }
        @[/quote]

        I know Xander84 :) But I want write c++ function to return model for qml table view.

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

          Simply return the QSqlQueryModel instance from a Q_INVOKABLE function. So, what your return in your last snippet in the opening post is model.

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

            [quote author="Andre" date="1399882697"]Simply return the QSqlQueryModel instance from a Q_INVOKABLE function. So, what your return in your last snippet in the opening post is model.[/quote]

            Thank you for reply ! exactly my problem is return model.
            How can I return my model ? I need an example.

            Please show me in my function :
            of course this is for Q Widget Application.

            @
            public:
            Q_INVOKABLE QVariant KZ() {

            if (OpenConnection())
            {
            

            QSqlQueryModel * model = new QSqlQueryModel();
            QSqlQuery * query = new QSqlQuery();
            query->prepare("Select doc_number,first_name,last_name from document");
            query->exec();
            model->setQuery(*query);

             ui->tableView_2->setModel(model);
             ui->tableView_2->model()->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
             ui->tableView_2->model()->setHeaderData(1, Qt::Horizontal, QObject::tr("First Name"));
             ui->tableView_2->model()->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Name"));
            

            return .... what ?
            }

            else
            {
            
            
            
            
            }
            

            @

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

              fill in "model" where you now say ".... what?", and it should work. Notice that the return value of the function can also just be QAbstractItemModel*, instead of QVariant.

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

                [quote author="Andre" date="1399883578"]fill in "model" where you now say ".... what?", and it should work. Notice that the return value of the function can also just be QAbstractItemModel*, instead of QVariant.[/quote]

                I want say after

                model->setQuery(*query);

                set model and header thene return this model for qml.

                Please Notice to below codes :

                @
                ui->tableView_2->setModel(model);
                ui->tableView_2->model()->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
                ui->tableView_2->model()->setHeaderData(1, Qt::Horizontal, QObject::tr("First Name"));
                ui->tableView_2->model()->setHeaderData(2, Qt::Horizontal, QObject::tr("Last Name"));
                @

                these are for QWidget application but i want to convert these to qml application ! i need to rewrite there on a function and then return with Q_INVOKABLE to my qml.

                My problem is instance of table view model.

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

                  Why don't you just delete the parts you no longer need? That is: if you are not using the tableView any more, there is no need to have code to instantiate it.

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

                    [quote author="Andre" date="1399884232"]Why don't you just delete the parts you no longer need? That is: if you are not using the tableView any more, there is no need to have code to instantiate it.[/quote]

                    You mean is this code can work alone ?

                    @
                    Q_INVOKABLE QVariant KZ() {

                        QSqlQueryModel * model = new QSqlQueryModel();
                        QSqlQuery * query = new QSqlQuery();
                        query->prepare("Select doc_number,first_name,last_name from document");
                        query->exec();
                    
                        return model->query();
                    

                    }

                    @

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

                      Almost. You want to return model itself, not_ model->query()_;

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

                        Thank you for your reply
                        So... Please if you can check my project for help me to solved it :

                        Check my qml file in line 94 and my kambiz.h file in line : 64

                        I can't solve this :(

                        http://www.pal4dream.net/up/do.php?id=1288

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

                          No, you'll have to do your own debugging. This forum does not provide a free debugging service.

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

                            [quote author="Andre" date="1399887061"]No, you'll have to do your own debugging. This forum does not provide a free debugging service.[/quote]

                            Thank you for helping I try to solve it.

                            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