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 QSqlField.type() (when used with QSqlTableModel)
Forum Updated to NodeBB v4.3 + New Features

Problem with QSqlField.type() (when used with QSqlTableModel)

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.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.
  • A Offline
    A Offline
    alexbottoni
    wrote on last edited by
    #1

    I'm trying to determine the datatype of the columns (fields) of a PostgreSQL database table as rendered by QSqlTableModel. I'm using QSqlField.type() for this:

    ("model" is a QSqlTableModel)
    @
    QSqlRecord rec = model->record(1);
    QSqlField f = rec.field("name");
    QVariant v = f.value();
    qDebug() << "Field Value for row 0 and column 1/name (should be Alex)" << v.toString();
    QSqlField f2 = rec.field("name");
    QVariant t = f2.type();
    qDebug() << "Field Type for column 1/name (should be char)" << t.toString();
    QSqlField f3 = rec.field("id");
    t = f3.type();
    qDebug() << "Field Type for column 0/id (should be int)" << t.toString();
    @

    Unfortunately, this is what I get from the code above:

    @
    Field Value for row 0 and column 1/name (should be Alex) "Alex"
    Field Type for column 1/name (should be char) ""
    Field Type for column 0/id (should be int) "0"
    @

    Does anybody know what is going wrong here?

    Thanks

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      This is wrong:

      @
      QVariant t = f2.type();
      @

      "QSqlField::type() ":/doc/qt-4.8/qsqlfield.html#type returns a QVariant::Type, not a QVariant!

      Change your code to this:

      @
      QVariant::Type t = f2.type();
      @

      The qDebug() output should print 10, which is QVariant::String.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alexbottoni
        wrote on last edited by
        #3

        Thanks! Now it works as expected:

        @
        Field Value for row 1 and column 1 (should be Alex) "Alex"
        Field Type for column 1/name (should be char) QVariant::QString
        Field Type for column 0/id (should be int) QVariant::int
        @

        BTW, the qDebug line should be like this:

        @
        qDebug() << "Field Type for column 1/name (should be char)" << t;
        @

        (No .toString() casting)

        1 Reply Last reply
        0
        • F Offline
          F Offline
          foxyz
          wrote on last edited by
          #4

          QVariant::Type is an enum (int) type

          I just know coding and coding

          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