Problem with QSqlTableModel



  • Hi guys,

    I am having some trouble with QSqlTableModel. Here is my code:

    @void skGIS::sqlModel(QString usr,QString pwd, QString dsn) {
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    db.setDatabaseName(dsn);
    db.setUserName(usr);
    db.setPassword(pwd);
    db.setHostName("");

    if ( db.open() ) {
    this->setCursor(Qt::WaitCursor);

    QSqlTableModel *model = new QSqlTableModel(ui.twQuery,db);
    // twQuery is of type 'QTableView'
    model->setTable("A_STATR");

    if ( !model->select() ) {
    //if (model->lastError().type() != QSqlError::NoError) {
    QMessageBox msgBox;
    QString err( model->lastError().text() ;)
    msgBox.setText( err );
    msgBox.exec();
    }

    ui.twQuery->setModel(model);

    this->setCursor(Qt::ArrowCursor);
    db.close();
    }
    else {
    QMessageBox msgBox;
    //QString err( db.lastError().text() );
    msgBox.setText( "Connection Error" );
    msgBox.exec();
    }

    }@

    I'm using Visual Studio 2010 with qt 4.7.2 and I have already performed a query on the table "A_STATR" with QSqlQuery successfully. When I jump into the function
    @model->select()@

    I see the following code:

    @bool QSqlTableModel::select()
    {
    Q_D(QSqlTableModel);
    QString query = selectStatement();
    if (query.isEmpty())
    return false;

    revertAll();
    QSqlQuery qu(query, d->db);
    setQuery(qu);
    
    if (!qu.isActive() || lastError().isValid()) {
        // something went wrong - revert to non-select state
        d->initRecordAndPrimaryIndex();
        return false;
    }
    return true;
    

    }@

    When I look at "query" I can see that it looks like

    "SELECT "FIELD_A", "FIELD_B", "FIELD_C", "FIELD_D", "FIELD_E",
    "FIELD_A", "FIELD_B", "FIELD_C", "FIELD_D", "FIELD_E",
    "FIELD_A", "FIELD_B", "FIELD_C", "FIELD_D", "FIELD_E",
    "FIELD_A", "FIELD_B", "FIELD_C", "FIELD_D", "FIELD_E",
    "FIELD_A", "FIELD_B", "FIELD_C", "FIELD_D", "FIELD_E",
    "FIELD_A", "FIELD_B", "FIELD_C", "FIELD_D", "FIELD_E",
    ...
    FROM A_STATR

    so the columns of the table are repeated like 50 times or so.

    Have I forgotten something?



  • i think you make a typo at line 19 in your first block of code. (";" is before ")" )
    But for your question, is there a loop somewhere in your code wich we dont see?



  • yeah I made a typing error in line 19, but it won't be that easy for you ;)

    To answer your question: No, there is no loop in the code. The function sqlModel() is a public slot of the skGIS-Class which is connected to the clicked()-signal of a button on the ui.



  • Sorry, cant help you here.
    And yes i understand that the typo is not the problem :)



  • Another thing i want to mention:

    The data won't be displayed in the SqlTableView, unless I remove line 27, but at least it is displayed now.
    But I still have this problem with the duplicated columns. Any ideas?


Log in to reply
 

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