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. A QItemDelegate that uses my own editor QWidget can fail to update the model
Forum Update on Monday, May 27th 2025

A QItemDelegate that uses my own editor QWidget can fail to update the model

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 7.2k 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
    Jonathan
    wrote on last edited by
    #1

    I'm using a QTableView and a QSqlRelationalTableModel. I have successfully implemented QItemDelegates that use a QDateTimeEdit and QLineEdit.

    In particular if I:

    modify the text in the QLineEdit I use for my QItemDelegate

    call submitAll() on the model

    then QItemDelegate::setModelData() is correctly called. ie the model is updated and the modified text saved to the database.

    However, if instead my QItemDelegate editor is a "compound" editor, say a QWidget to which I've added a QLineEdit (rather than just a QLineEdit by itself), then setModelData() isn't called in the above circumstances.

    On the other hand if I

    navigate away from my "compound" editor or if I press return

    call submitAll()

    then setModelData() is correctly called.

    Any ideas on why my "compound" editor isn't fully working? I presume that if I use a simple editor the model knows that the data in the editor has changed, but if I use a compound editor the necessary events are being blocked.

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

      Yes. You know the reason right yourself:)
      If you have the custom widget how the Qt will know when the data is changed? You must call commitData().
      See void QAbstractItemDelegate::commitData ( QWidget * editor ) function.

      Darryl D'Souza

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

        You need a "user property":http://doc.qt.nokia.com/4.7/properties.html in your compound widget, so that the delegate can set and retrieve the data automagically.

        See the docs of:

        • "QStyledItemDelegate::createEditor() ":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html#createEditor
        • "QStyledItemDelegate::setEditorData() ":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html#setEditorData
        • "QStyledItemDelegate::setModelData() ":http://doc.qt.nokia.com/4.7/qstyleditemdelegate.html#setModelData

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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jonathan
          wrote on last edited by
          #4

          Darryl: How do I obtain a reference to the editor in order to call ::commitData? The methods in the delegate are all "consts".

          Volker: Are you suggesting that I create a user property in my compound widget (ie the editor) to store a reference to the delegate? When and by whom is this property (i) set, (ii) retrieved?

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

            Define a property for your compound widget that holds the value the user has entered. Whatever it is - we don't know. Provide a read and a write method and mark it as user property, QStyledItemDelegate will recognize it and use it to set the models value as default for the editor and to retrieve the value the user has entered and store that one in the model. You only will have to return a new instance of your widget in the overwritten method createEditor() of your delegate. See the links provided, they tell you who calls the property.

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

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Darryl DSouza
              wrote on last edited by
              #6

              Another simple way could be catch the signal of your compound widget QLineEdit which is a child of QWidget on user update finished and in that slot, you can call commitData(). Qt now must call setModelData() function.

              Darryl D'Souza

              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