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. How to let a QStyledItemDelegate editor paint its background?
Forum Updated to NodeBB v4.3 + New Features

How to let a QStyledItemDelegate editor paint its background?

Scheduled Pinned Locked Moved General and Desktop
16 Posts 2 Posters 5.5k 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
    aknudsen
    wrote on last edited by
    #1

    It seems to me that when implementing [url=http://qt-project.org/doc/qt-5/qstyleditemdelegate.html#createEditor]QStyledItemDelegate.createEditor[/url], the returned editor is painted with a transparent background. That is to say, the editor's background is not applied at all, only the foreground (e.g., text) is visible. How can I make sure that the editor paints its background?

    For full reference, with code, please see my [url=http://stackoverflow.com/questions/23912799/how-do-i-paint-the-background-from-a-qstyleditemdelegate-editor]StackOverflow question[/url].

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

      Hi and welcome to devnet,

      Why are you overloading the paint function ? It doesn't seems that you need it

      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
      • A Offline
        A Offline
        aknudsen
        wrote on last edited by
        #3

        I think you're right in that the current incarnation could do without the custom paint method. However, it doesn't change that the editor widget needs to be able to paint the cell (including the background)?

        I tried not defining the paint method override, and the problem remains that any cell with an active editor has a white background even if it's selected.
        [quote author="SGaist" date="1401307779"]Hi and welcome to devnet,

        Why are you overloading the paint function ? It doesn't seems that you need it[/quote]

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

          Most of the time, the editor covers the entire cell and doesn't care about the cell state. Are you sure you're not over complicate things here ?

          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
          • A Offline
            A Offline
            aknudsen
            wrote on last edited by
            #5

            The problem is, the practical effect is that the last cell that gets hovered above loses its visual selection state (i.e., highlighting). My solution only launches an editor as a hack to receive e.g. double-clicks, it's not supposed to be an actual editor.

            !http://i.imgur.com/bH8r94M.png(Screenshot)!

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

              Then it sounds like you have a bug elsewhere, AFAIK hovering an element should not influence that

              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
              • A Offline
                A Offline
                aknudsen
                wrote on last edited by
                #7

                I'm not sure if you understand how it works. The point is that I need to launch an "editor" upon hovering above a cell, in order to be able to handle double-clicks etc. The problem is that, while the editor is open, the cell cannot be highlighted due to the editor being unable to paint its background. Even when removing the mouse pointer from the cell, the editor remains open, and the cell un-highlighted. The problem would go away AFAICT if the editor could paint its background.

                As to why the cell's highlighting disappears when the editor opens, I'm not sure. Seems like automatic behaviour on Qt's part.

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

                  The highlighting "disappearing" is completely normal. The editor is generally a widget that follows the system color scheme.

                  Closing an editor generally happens when pressing enter (e.g QLineEdit), an item is selected (e.g. QComboBox) or the widget loses focus (e.g. new cell selected) so just moving the mouse cursor away isn't enough.

                  However, opening an editor on the hover event is very surprising. Why do you need that ?

                  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
                  • A Offline
                    A Offline
                    aknudsen
                    wrote on last edited by
                    #9

                    Like I explained earlier, the "editor" is only a hack to receive events such as double-click and hover. I couldn't find any way for delegates to receive such events on their own. It's basically a way to implement sub-cells, so that the table can contain two values per cell, which each can be highlighted (when hovered above) and double-clicked separately.

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

                      Then you should rather use a proxy model that put these values together, then the job of your delegate would be simplified

                      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
                      • A Offline
                        A Offline
                        aknudsen
                        wrote on last edited by
                        #11

                        But how would a proxy model help me detect which of the two values in a certain cell is double-clicked/hovered above??

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

                          Do you absolutely want to edit only one of the two values ? Or a "double" editor would be fine ?

                          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
                          • A Offline
                            A Offline
                            aknudsen
                            wrote on last edited by
                            #13

                            There's no editing involved. Double-clicking one value should lead to an action (not editing) for that particular value.

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

                              That's one thing that would have been useful to know before.

                              Then I would e.g. filter the doubleClick event, get the index under the cursor, from that get the visual rect, then check wether the cursor is the in the left or right section of that rect, then you should be able to do that action.

                              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
                              • A Offline
                                A Offline
                                aknudsen
                                wrote on last edited by
                                #15

                                I'd already stated that it isn't a real editor in three different posts, I've certainly tried to get the point across. Where should I filter the doubleClick event though? AFAICT the delegate can't receive such events. Do you mean I should subclass QTableView, and catch double-click/hover events there?

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  aknudsen
                                  wrote on last edited by
                                  #16

                                  I implemented an editor-less solution that appears to work well. I chose to subclass QTableView in order to handle the required events, and pass them on to the delegate so it can do the real work. See this [url=http://stackoverflow.com/a/23995577/265261]StackOverflow answer[/url] for code.

                                  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