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. QDataWidgetMapper and numbers issue
QtWS25 Last Chance

QDataWidgetMapper and numbers issue

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 3.9k Views
  • 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.
  • J Offline
    J Offline
    jarda
    wrote on last edited by
    #1

    Hello all,

    I have QSqlTableModel, connected QTableView and form managed by QDataWidgetMapper.

    SQL table has column with type INTEGER. Some table records have value in this column and some not.

    I have mapped form fields (QLineEdits) for table columns. When I change selected record, all fields are populated well except the field for column with INTEGER type.
    When there is selected record where this column contains data, the field correctly shows the value. But when I change the selection to the record where this column doesn't have data (NULL), the field keeps the value from previously selected record.

    I did some tests and this behavior is only for table column with INTEGER type. When I change the type to CHAR(n), it works well.

    Any idea for this wrong behavior?
    Thanks in advance,
    Jaroslav

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      It could be a bug in Qt -- can you check the data() returned for those positions where the value is missing (i.e. is NULL), namely what the returned variant contains and which type is it? If it's correct, then it could be a QDWM problem, and/or a QItemDelegate one.

      A possible workaround could be installing a custom QItemDelegate subclass on the QDWM and forcibly clean the lineedit in case of a null value, but I suspect that the QDWM is never calling the QID setEditorData...

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jarda
        wrote on last edited by
        #3

        Thanks for quick response Peppe,

        For empty cells, the model returns QVariant with type int, value 0, but isNull() returns true.

        So QDataWidgetMapper should handle it as "empty value". I solved it in delegate setEditorData(). When variant is null I set empty string into edit line.

        But it seems to me that is it a bug in default setEditorData function.

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

          Please check the bugreport system then if there is already a bug describing that issue, and if not, create such a bugreport.

          1 Reply Last reply
          0
          • O Offline
            O Offline
            octal
            wrote on last edited by
            #5

            bq. but I suspect that the QDWM is never calling the QID setEditorData…

            It calls it if the mapping does not have a custom property.

            I tried to investigate, but I didn't find from where this bug could come from, without spending hours trying to understand the metaobject internal mechanism :D

            However, the default delegate just set the corresponding widget's user property and QLineEdit has one.

            This maybe comes from QMetaProperty::write ?

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

              That's not really a bug:

              • QVariant can't convert a null QVariant::Int to a string (checked in QMetaPropery::write through QVariant::convert),
              • QObject::setProperty, called directly in QDWM or in setEditorData, won't assign a value from QVariant if it can't convert it from the original type to the real property type.
              • QDWM can't possibly take care automatically of cases where conversions doesn't work.

              So, if the "empty value" is a possibility, you should handle it yourself with a delegate as you did.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dangelog
                wrote on last edited by
                #7

                [quote author="alexisdm" date="1315313698"]That's not really a bug:

                • QVariant can't convert a null QVariant::Int to a string (checked in QMetaPropery::write through QVariant::convert),
                • QObject::setProperty, called directly in QDWM or in setEditorData, won't assign a value from QVariant if it can't convert it from the original type to the real property type.
                • QDWM can't possibly take care automatically of cases where conversions doesn't work.

                So, if the "empty value" is a possibility, you should handle it yourself with a delegate as you did.[/quote]

                Makes perfectly sense, but why if a non-null QVariant::Int converts successfully into a QString, why doesn't a null QVariant::Int convert into a null QString? (Note: I don't know QVariant internals).

                Software Engineer
                KDAB (UK) Ltd., a KDAB Group company

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

                  [quote author="peppe" date="1315319386"]but why if a non-null QVariant::Int converts successfully into a QString, why doesn't a null QVariant::Int convert into a null QString?[/quote]It is kind of converted: any null QVariant can be converted to a null QString, or to an invalid/null value of any type it could be converted to if it wasn't null, but QVariant::convert will return false, indicating an error.

                  QObject::setProperty just doesn't ignore that error.

                  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