Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with QSqlTableModel
Qt 6.11 is out! See what's new in the release blog

Problem with QSqlTableModel

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.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 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?

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

      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?

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

        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.

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

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

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

            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?

            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