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. [Solved] QTableView + QAbstractItemDelegate: Custom editor doesnt close on enter or when "clicking away"?
Forum Updated to NodeBB v4.3 + New Features

[Solved] QTableView + QAbstractItemDelegate: Custom editor doesnt close on enter or when "clicking away"?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 10.6k 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.
  • W Offline
    W Offline
    wjlrkjjv
    wrote on last edited by
    #1

    Hi!

    Can someone tell me how to correctly use delegation? I actually want to use QValidator on QTableView "cells".
    Seems that there's no other way than to delegate.

    So I've implemented QAbstractItemDelegate::createEditor(), but when editing the cell, the custom created QLineEdit doesn't close again as it should normally by pressing enter or clicking into the QTableView-viewport (it closes when clicking anywhere on any row though).

    What am I missing?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Please show some code (especially the delegate) and tell a bit, which version, compiler and platform you are using.
      I'm heavily using custom delegates with line edits, combo boxes etc and it works perfect.
      The best would be a simple, working (or not :-) ) example.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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

        At least, make sure the example is self-contained and compilable. Preferably, post a link to a complete package we can just build here and post the critical code sections you want to discuss in a posting here.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wjlrkjjv
          wrote on last edited by
          #4

          Thanks for your responses.
          Before I go with an example, another question, because it seems I got it working now.

          closeEditor(...) signal must be emitted by the custom QLineEdit returned in QAbstractItemDelegate::createEditor().

          So I subclassed QLineEdit and let it emit closeEditor(...) on return, escape and lost focus.
          And then I connected MyLineEdit::closeEditor(...) with QTableView::closeEditor(...).

          Now the editor closes properly whenever I click away or press return or escape.

          Is this the (a) correct way?

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

            No. The delegates work with standard line edits without any additional signal, so it must be something different.

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

            1 Reply Last reply
            0
            • W Offline
              W Offline
              wjlrkjjv
              wrote on last edited by
              #6

              Hmm... I had to inherit my delegate from QItemDelegate, not from QAbstractItemDelegate.
              But I saw code using QAbstractItemDelegate I'm sure. I guess I still didn't get the basics...
              Now it works (behaves like default) by just returning a new widget (without frame) in createEditor(), and setting geometry in updateEditorGeometry().
              Sure I won't forget about setEditorData() and setModelData().

              1 Reply Last reply
              0
              • G Offline
                G Offline
                giesbert
                wrote on last edited by
                #7

                QItemDelegate implements all the needed stuff fopr you, QAbstractItemDelegate is only the interface with some basics, not all needed stuff.

                If you potentially want to use style sheets, I suggest using QStyledItemDelegate instead of QItemDelegate.

                Nokia Certified Qt Specialist.
                Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                1 Reply Last reply
                0
                • W Offline
                  W Offline
                  wjlrkjjv
                  wrote on last edited by
                  #8

                  Ok, thanks again!
                  I think this thread is done.

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

                    It's generally recommended to use QStyledItemDelegate instead of QItemDelegate.

                    See the "note in the docs":http://doc.qt.nokia.com/4.7/qitemdelegate.html#qstyleditemdelegate-vs-qitemdelegate:

                    bq. QStyledItemDelegate vs. QItemDelegate
                    Since Qt 4.4, there are two delegate classes: QItemDelegate and QStyledItemDelegate. However, the default delegate is QStyledItemDelegate. These two classes are independent alternatives to painting and providing editors for items in views. The difference between them is that QStyledItemDelegate uses the current style to paint its items. We therefore recommend using QStyledItemDelegate as the base class when implementing custom delegates or when working with Qt style sheets. The code required for either class should be equal unless the custom delegate needs to use the style for drawing.

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

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

                      Problem with that is, that the API of both delegates are completely different. Some things that were easy with QItemDelegate are neigh on inmpossible with QStyledItemDelegate. If you can manage what you need to do with QStyledItemDelegate, then that is obviously the way to go, but otherwise QItemDelegate is still an option, IMHO.

                      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