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. QTableWidgetItem doesn't have multiline edit mode?

QTableWidgetItem doesn't have multiline edit mode?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 1.8k 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.
  • SGaistS SGaist

    Hi,

    Create a custom QStyledItemDelegate and provide a QTextEdit as editor in place of QLineEdit.

    P Offline
    P Offline
    Publicnamer
    wrote on last edited by
    #4

    @SGaist OK that basically works. However the user can't tap Return to exit the multiline editing mode. What could I do to enable that? I prefer to have multiple lines that wrap, and maybe a Shift-Return for a line break, but the Return key still has to exit editing mode.

    JonBJ 1 Reply Last reply
    0
    • P Publicnamer

      @SGaist OK that basically works. However the user can't tap Return to exit the multiline editing mode. What could I do to enable that? I prefer to have multiple lines that wrap, and maybe a Shift-Return for a line break, but the Return key still has to exit editing mode.

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

      @Publicnamer
      Then subclass and override e.g. QTextEdit::keyPressEvent(QKeyEvent *e). Or write an eventFiilter().

      P 1 Reply Last reply
      2
      • JonBJ JonB

        @Publicnamer
        Then subclass and override e.g. QTextEdit::keyPressEvent(QKeyEvent *e). Or write an eventFiilter().

        P Offline
        P Offline
        Publicnamer
        wrote on last edited by
        #6

        @JonB said in QTableWidgetItem doesn't have multiline edit mode?:

        @Publicnamer
        Then subclass and override e.g. QTextEdit::keyPressEvent(QKeyEvent *e). Or write an eventFiilter().

        I tried the first option, but it appears subclassing QPlainTextEdit isn't a matter of just subclassing. It wants me to implement some things like createEditor etc.

        As for adding an event filter, isn't there a simple way to inject a closure into the text editor to intercept events? Why is subclassing always the solution? Seems like the wrong way.

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

          Because it's C++ not an interpreted language like Python.

          As @JonB suggested, you can implement an event filter in place of subclassing in this case.

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

          P 1 Reply Last reply
          2
          • SGaistS SGaist

            Because it's C++ not an interpreted language like Python.

            As @JonB suggested, you can implement an event filter in place of subclassing in this case.

            P Offline
            P Offline
            Publicnamer
            wrote on last edited by Publicnamer
            #8

            @SGaist Who's using Python? In many modern compiled languages like Objective-C and Swift, you can do some useful amount of injection of closures.

            Anyway, it seemed to me that the eventFilter required subclassing as well. Not so?

            JonBJ 1 Reply Last reply
            0
            • P Publicnamer

              @SGaist Who's using Python? In many modern compiled languages like Objective-C and Swift, you can do some useful amount of injection of closures.

              Anyway, it seemed to me that the eventFilter required subclassing as well. Not so?

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

              @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

              I tried the first option, but it appears subclassing QPlainTextEdit isn't a matter of just subclassing. It wants me to implement some things like createEditor etc.

              You do not have to supply createEditor() etc. You should be able to subclass and only override whatever you need to. However, you are going down the eventFilter() route anyway.

              Anyway, it seemed to me that the eventFilter required subclassing as well. Not so?

              No. Or at least it is only the container/parent which needs to subclassed, not the QTextEdit. Its eventFilter() is effectively monitoring events sent to its children.

              P 1 Reply Last reply
              0
              • JonBJ JonB

                @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

                I tried the first option, but it appears subclassing QPlainTextEdit isn't a matter of just subclassing. It wants me to implement some things like createEditor etc.

                You do not have to supply createEditor() etc. You should be able to subclass and only override whatever you need to. However, you are going down the eventFilter() route anyway.

                Anyway, it seemed to me that the eventFilter required subclassing as well. Not so?

                No. Or at least it is only the container/parent which needs to subclassed, not the QTextEdit. Its eventFilter() is effectively monitoring events sent to its children.

                P Offline
                P Offline
                Publicnamer
                wrote on last edited by Publicnamer
                #10

                @JonB Ah well the container is a table widget item, so that's no improvement. I'd still have to subclass. Or does the way that events are processed let me implement the filter in the container of the container of the container? That would be my window class.

                JonBJ 1 Reply Last reply
                0
                • P Publicnamer

                  @JonB Ah well the container is a table widget item, so that's no improvement. I'd still have to subclass. Or does the way that events are processed let me implement the filter in the container of the container of the container? That would be my window class.

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

                  @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

                  Ah well the container is a table widget item, so that's no improvement.

                  I don't think QTableWidgetItem counts for this anyway, since it's not a QWidget/QObject. And I think even if it were the top-level widget like QMainWindow or whatever still sees events before children do.

                  One way or another, try it on your "window class" and see for yourself before proceeding.

                  P 2 Replies Last reply
                  0
                  • JonBJ JonB

                    @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

                    Ah well the container is a table widget item, so that's no improvement.

                    I don't think QTableWidgetItem counts for this anyway, since it's not a QWidget/QObject. And I think even if it were the top-level widget like QMainWindow or whatever still sees events before children do.

                    One way or another, try it on your "window class" and see for yourself before proceeding.

                    P Offline
                    P Offline
                    Publicnamer
                    wrote on last edited by
                    #12
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

                      Ah well the container is a table widget item, so that's no improvement.

                      I don't think QTableWidgetItem counts for this anyway, since it's not a QWidget/QObject. And I think even if it were the top-level widget like QMainWindow or whatever still sees events before children do.

                      One way or another, try it on your "window class" and see for yourself before proceeding.

                      P Offline
                      P Offline
                      Publicnamer
                      wrote on last edited by Publicnamer
                      #13

                      @JonB
                      I find that the event filter is being called but there is something strange:
                      I am receiving QEvent::KeyRelease but not QEvent::KeyPress.
                      The example code says to use the latter.
                      Also, even though I'm intercepting Return (returning true from the filter), the key is still being received by the text editor.

                      P 1 Reply Last reply
                      0
                      • P Publicnamer

                        @JonB
                        I find that the event filter is being called but there is something strange:
                        I am receiving QEvent::KeyRelease but not QEvent::KeyPress.
                        The example code says to use the latter.
                        Also, even though I'm intercepting Return (returning true from the filter), the key is still being received by the text editor.

                        P Offline
                        P Offline
                        Publicnamer
                        wrote on last edited by
                        #14

                        @Publicnamer
                        In the end I overrode the keypress/keyrelease methods and now it works. The eventFilter was not a good solution.

                        1 Reply Last reply
                        0
                        • P Publicnamer

                          @JonB said in QTableWidgetItem doesn't have multiline edit mode?:

                          @Publicnamer
                          Then subclass and override e.g. QTextEdit::keyPressEvent(QKeyEvent *e). Or write an eventFiilter().

                          I tried the first option, but it appears subclassing QPlainTextEdit isn't a matter of just subclassing. It wants me to implement some things like createEditor etc.

                          As for adding an event filter, isn't there a simple way to inject a closure into the text editor to intercept events? Why is subclassing always the solution? Seems like the wrong way.

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

                          @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

                          Why is subclassing always the solution?

                          In C++ it always is ;)

                          The eventFilter was not a good solution.

                          That should be THE solution, it's also explained in the docs

                          Super easy:

                          bool eventFilter(QObject *object, QEvent *event) override{
                              QWidget *editor = qobject_cast<QWidget*>(object);
                              if (!editor)
                                  return false;
                              if (event->type() == QEvent::KeyPress) {
                                  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                                  if (keyEvent->matches(QKeySequence::Cancel)) {
                                      // don't commit data
                                      emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
                                      return true;
                                  }
                                  switch (keyEvent->key()) {
                                  case Qt::Key_Tab:
                                      emit commitData(editor);
                                      emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
                                      return true;
                                  case Qt::Key_Backtab:
                                      emit commitData(editor);
                                      emit closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
                                      return true;
                                  case Qt::Key_Enter:
                                  case Qt::Key_Return:
                                      QMetaObject::invokeMethod(this, [editor,this](){commitData(editor); closeEditor(editor, QAbstractItemDelegate::SubmitModelCache);},Qt::QueuedConnection);
                                      return false;
                                  default:
                                      return false;
                                  }
                              }
                              return QStyledItemDelegate::eventFilter(QObject *editor, QEvent *event)
                          }
                          

                          Note: most of this code is copy-pasted from QAbstractItemDelegatePrivate::editorEventFilter

                          "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

                            @Publicnamer said in QTableWidgetItem doesn't have multiline edit mode?:

                            Why is subclassing always the solution?

                            In C++ it always is ;)

                            The eventFilter was not a good solution.

                            That should be THE solution, it's also explained in the docs

                            Super easy:

                            bool eventFilter(QObject *object, QEvent *event) override{
                                QWidget *editor = qobject_cast<QWidget*>(object);
                                if (!editor)
                                    return false;
                                if (event->type() == QEvent::KeyPress) {
                                    QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                                    if (keyEvent->matches(QKeySequence::Cancel)) {
                                        // don't commit data
                                        emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
                                        return true;
                                    }
                                    switch (keyEvent->key()) {
                                    case Qt::Key_Tab:
                                        emit commitData(editor);
                                        emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
                                        return true;
                                    case Qt::Key_Backtab:
                                        emit commitData(editor);
                                        emit closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
                                        return true;
                                    case Qt::Key_Enter:
                                    case Qt::Key_Return:
                                        QMetaObject::invokeMethod(this, [editor,this](){commitData(editor); closeEditor(editor, QAbstractItemDelegate::SubmitModelCache);},Qt::QueuedConnection);
                                        return false;
                                    default:
                                        return false;
                                    }
                                }
                                return QStyledItemDelegate::eventFilter(QObject *editor, QEvent *event)
                            }
                            

                            Note: most of this code is copy-pasted from QAbstractItemDelegatePrivate::editorEventFilter

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

                            @VRonin said in QTableWidgetItem doesn't have multiline edit mode?:

                            That should be THE solution

                            if (event->type() == QEvent::KeyPress) {

                            I agree with you. The trouble is the OP wrote earlier

                            I am receiving QEvent::KeyRelease but not QEvent::KeyPress.

                            but I didn't pursue it with him....

                            VRoninV 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @VRonin said in QTableWidgetItem doesn't have multiline edit mode?:

                              That should be THE solution

                              if (event->type() == QEvent::KeyPress) {

                              I agree with you. The trouble is the OP wrote earlier

                              I am receiving QEvent::KeyRelease but not QEvent::KeyPress.

                              but I didn't pursue it with him....

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

                              @JonB said in QTableWidgetItem doesn't have multiline edit mode?:

                              I am receiving QEvent::KeyRelease but not QEvent::KeyPress.

                              Yes, that's because the eventFilter of the delegate intercepts KeyPress and doesn't pass it on

                              "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

                              1 Reply Last reply
                              2

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved