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



  • 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
    {
    
    
    
    
    }
    

    @



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



  • 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 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
    {
    
    
    
    
    }
    

    @



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



  • 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 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();
    

    }

    @



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



  • 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



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



  • [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.


Log in to reply
 

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