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. Get key modifiers when editting QAbstractItemModel

Get key modifiers when editting QAbstractItemModel

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.3k 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
    Julieng
    wrote on last edited by
    #1

    Hello,
    I have subclassed a QAbstractItemModel to feed a QTableWidget. Re-implementing setData() works well. Now, I would like to be able to check if CTRL key was pressed while entering in setData() function so that I could have different behaviors if modifier key was pressed or not. Unfortunately, I have no idea about how to get a keyevent (or key modifier) check in the QAbstractItemModel. Could someone help me ?
    Thanks in advance !

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      A model is not the right place for that, they shouldn't have to know anything about key events.

      What exactly are you trying to achieve ?

      In any case, you should take a look at QStyledItemDelegate.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Why not simply use QGuiApplication::keyboardModifiers()?

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

        1 Reply Last reply
        1
        • S Offline
          S Offline
          Slawomir_Piernikowski
          wrote on last edited by
          #4

          QGuiApplication::keyboardModifiers()? is a good idea.
          I have just used it in my setData function of QAbstractItem model and it works see hereunder picture:

          0_1541449400604_Bez tytułu.png

          1 Reply Last reply
          0
          • J Offline
            J Offline
            Julieng
            wrote on last edited by Julieng
            #5

            Thanks for all your replies.

            SGaist - my use case is the following one : columns are index of a list of objects in my model and lines are different properties of these objects. When modifying a cell with CTRL key pressed, I would like to modify the same propery (corresponding to the editted line number) of all objects with the same value. If not pressed, I only modify the property of the object with current column index. I hope my explanation is clear enough. I am interested if there is a cleaner way to get this kind of mechanism.

            Christian Ehrlicher/Slawomir_Piernikowski - It seems to answer my issue, I did not know well QGuiApplication class. Thanks for your answer, I will try it tomorrow !

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I'd rather implement that at the view level. Otherwise you might have strange behaviours if your model is used through another view or if setData is called from somewhere else than your current view.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Julieng
                wrote on last edited by
                #7

                @SGaist said in Get key modifiers when editting QAbstractItemModel:

                I'd rather implement that at the view level

                Could you please detail what your are referring to ? I am not clear about how to do that this way. Thanks.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  Julieng
                  wrote on last edited by
                  #8

                  Christian Ehrlicher/Slawomir_Piernikowski - I tested your solution and it works well, thanks !

                  SGaist - I would appreciate if you have time to give me some more guidance to learn better practices

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    What kind of editor are you using ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Julieng
                      wrote on last edited by
                      #10

                      The default one, for text edition.

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Then one possibility would be to implement a custom QStyledItemDelegate and reimplement setModelData.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          Julieng
                          wrote on last edited by
                          #12

                          I understand, I need to check modifier keys and, if pressed, call multiple times following function :

                              model->setData(index, value, role);
                          

                          Thanks a lot for your replies SGaist !

                          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