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. QItemDelegate for editing questions
Forum Updated to NodeBB v4.3 + New Features

QItemDelegate for editing questions

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.9k 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.
  • Christian EhrlicherC Online
    Christian EhrlicherC Online
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    First please derive from QStyledItemDelegate, QItemDelegate will hopefully go away with Qt6.

    • setEditorData()/setModelData() - you forgot QModelIndex::data() which can be used in setEditorData() as a shortcut.
    • createEdtior() - it's because you can create a different editor/widget for each index
    • QStyleOptionViewItem - just take a look at the parameters - it's used for drawing the item so you can use it too if you want/need it.

    The stuff with the hard focus I don't understand...

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    JonBJ 1 Reply Last reply
    4
    • Christian EhrlicherC Christian Ehrlicher

      First please derive from QStyledItemDelegate, QItemDelegate will hopefully go away with Qt6.

      • setEditorData()/setModelData() - you forgot QModelIndex::data() which can be used in setEditorData() as a shortcut.
      • createEdtior() - it's because you can create a different editor/widget for each index
      • QStyleOptionViewItem - just take a look at the parameters - it's used for drawing the item so you can use it too if you want/need it.

      The stuff with the hard focus I don't understand...

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #3

      @Christian-Ehrlicher
      Thanks, that's perfect. I will change my derivation.

      you forgot QModelIndex::data() which can be used in setEditorData() as a shortcut.

      Funny you should say that! Earlier I was musing to myself: "If QModelIndex contains a reference to the model, then a convenience method to access the data would be possible...". :) That's good, but doesn't to me answer why Qt passes the model explicitly to setModelData() but not to setEditorData(). It's as though they expect you to need it in the former but not in the latter, so I wondered if I was missing something.

      I still wonder what that Qt hard focus thing I saw is about.... Does a widget have some "hard focus" property which affects how it receives events?

      VRoninV 1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Maybe they mean StrongFocus?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        JonBJ 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          Maybe they mean StrongFocus?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #5

          @Christian-Ehrlicher
          You're a hero :)
          Here we go https://doc.qt.io/qt-5/qabstractitemdelegate.html:

          The returned editor widget should have Qt::StrongFocus; otherwise, QMouseEvents received by the widget will propagate to the view.

          What do you think of that? Do I need to take any action on the QWidget I am returning?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #6

            At least I did not yet had problems with the default behavior but it maybe depends on the type of the displayed widget - with QComboBoxes I don't have a problem here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              At least I did not yet had problems with the default behavior but it maybe depends on the type of the displayed widget - with QComboBoxes I don't have a problem here.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #7

              @Christian-Ehrlicher
              OK, mine too seems to be working without my doing anything about StrongFocus so I won't worry.

              1 Reply Last reply
              0
              • JonBJ JonB

                @Christian-Ehrlicher
                Thanks, that's perfect. I will change my derivation.

                you forgot QModelIndex::data() which can be used in setEditorData() as a shortcut.

                Funny you should say that! Earlier I was musing to myself: "If QModelIndex contains a reference to the model, then a convenience method to access the data would be possible...". :) That's good, but doesn't to me answer why Qt passes the model explicitly to setModelData() but not to setEditorData(). It's as though they expect you to need it in the former but not in the latter, so I wondered if I was missing something.

                I still wonder what that Qt hard focus thing I saw is about.... Does a widget have some "hard focus" property which affects how it receives events?

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #8

                @JonB said in QItemDelegate for editing questions:

                but doesn't to me answer why Qt passes the model explicitly to setModelData() but not to setEditorData()

                index.model() returns a const QAbstractItemModel* you can read the data from the model and set it inside the editor but you can't performs actions that alter the model from it, that's why setModelData() needs to have another, non-const, model passed as argument

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                JonBJ 1 Reply Last reply
                4
                • VRoninV VRonin

                  @JonB said in QItemDelegate for editing questions:

                  but doesn't to me answer why Qt passes the model explicitly to setModelData() but not to setEditorData()

                  index.model() returns a const QAbstractItemModel* you can read the data from the model and set it inside the editor but you can't performs actions that alter the model from it, that's why setModelData() needs to have another, non-const, model passed as argument

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #9

                  @VRonin
                  Ahhhhh!!!!! Well, that makes sense. I wouldn't know that, because I'm Python/PyQt where we don't have any notion of const and we can change whatever we like when we like, regardless! :)

                  J.HilkJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @VRonin
                    Ahhhhh!!!!! Well, that makes sense. I wouldn't know that, because I'm Python/PyQt where we don't have any notion of const and we can change whatever we like when we like, regardless! :)

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #10

                    @JonB said in QItemDelegate for editing questions:

                    Python/PyQt where we don't have any notion of const and we can change whatever we like when we like

                    A truly frightening thought....


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    JonBJ 1 Reply Last reply
                    0
                    • J.HilkJ J.Hilk

                      @JonB said in QItemDelegate for editing questions:

                      Python/PyQt where we don't have any notion of const and we can change whatever we like when we like

                      A truly frightening thought....

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #11

                      @J.Hilk Everything about Python is frightening.... :)

                      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