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. Detect which QTextEdit currently has the cursor/caret, for paste
Forum Updated to NodeBB v4.3 + New Features

Detect which QTextEdit currently has the cursor/caret, for paste

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 1.8k Views 2 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.
  • JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by
    #1

    Page has a QListWidget with items which can double-clicked, and several QTextEdits. One of the QTextEdits currently has the "flashing text insert cursor/caret" somewhere inside it. Double-clicking an item in the list should insert its text at the cursor/caret in the text edit, via QTextEdit::insertPlainText(QListWidgetItem::text()).

    • The problem is: which QTextEdit instance has the cursor/caret? It is not QApplication.focusWidget(), that turns out to be the QListWidget I am clicking in.

    • Or, alternatively, should this be done via some global "paste" Qt function which knows where the cursor/caret is?

    1 Reply Last reply
    0
    • JonBJ JonB

      @mrjj
      Even if I did that, there may be other widgets on the page which accept focus. I don't think I should be changing the list widget's behaviour, I just need to know which text edit the insert cursor is currently sitting inside, regardless.

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #8

      @VRonin , @mrjj
      I had not realized/noticed that as soon as I (single- or double-) click an item in the list then the edit widget loses the flashing insert caret. I had thought that remained as-was. My bad.

      In that light, although @VRonin's proposal of tracking "the last focussed widget" is probably what I originally had in mind, I now think it is clearer & simpler to accept @mrjj's suggestion of setting QListWidget::setFocusPolicy(Qt.NoFocus) instead. This leaves the caret visible in the QTextEdit which is as it should be, and means that I can just use QApplication.focusWidget() to identify the edit widget, which is all neater. I do lose the automatic ability for the user to use the arrow keys to scroll through the list items, but the stakeholder is happy with this.

      Thank you both.

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        What I would do is reimplement the focus event of the QTextEdits to emit a signal, connect that signal to a slot that stores the "last used textedit" and use this stored value to call insertPlainText

        "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
        2
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #3

          hi
          there is also
          QWidget * fw = qApp->focusWidget();

          JonBJ 1 Reply Last reply
          0
          • VRoninV VRonin

            What I would do is reimplement the focus event of the QTextEdits to emit a signal, connect that signal to a slot that stores the "last used textedit" and use this stored value to call insertPlainText

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #4

            @VRonin
            Thanks for prompt reply. Yes, I realized I can do that if I have to, but I'm lazy and don't like writing more than about one line per each problem I encounter :) I will do that if I have to, but you're saying that there is no call I can make to just ask which widget/text/line edit currently has the caret? Because obviously the windowing system must know.

            For the record, the code used to work when it had tabs/stacked widgets (not sure which, each QTextEdit was on its own tab, now they're all on one page) and just used currentWidget() (e.g. http://doc.qt.io/qt-5/qtabwidget.html#currentWidget).

            1 Reply Last reply
            0
            • mrjjM mrjj

              hi
              there is also
              QWidget * fw = qApp->focusWidget();

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

              @mrjj
              No cigar! That's precisely why I wrote:

              It is not QApplication.focusWidget(), that turns out to be the QListWidget I am clicking in.

              mrjjM 1 Reply Last reply
              0
              • JonBJ JonB

                @mrjj
                No cigar! That's precisely why I wrote:

                It is not QApplication.focusWidget(), that turns out to be the QListWidget I am clicking in.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @JonB
                oh sorry. missed that. its not option to flag QListWidget with noFocus if u dont need keyboard navigation?

                JonBJ 1 Reply Last reply
                1
                • mrjjM mrjj

                  @JonB
                  oh sorry. missed that. its not option to flag QListWidget with noFocus if u dont need keyboard navigation?

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

                  @mrjj
                  Even if I did that, there may be other widgets on the page which accept focus. I don't think I should be changing the list widget's behaviour, I just need to know which text edit the insert cursor is currently sitting inside, regardless.

                  JonBJ 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @mrjj
                    Even if I did that, there may be other widgets on the page which accept focus. I don't think I should be changing the list widget's behaviour, I just need to know which text edit the insert cursor is currently sitting inside, regardless.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #8

                    @VRonin , @mrjj
                    I had not realized/noticed that as soon as I (single- or double-) click an item in the list then the edit widget loses the flashing insert caret. I had thought that remained as-was. My bad.

                    In that light, although @VRonin's proposal of tracking "the last focussed widget" is probably what I originally had in mind, I now think it is clearer & simpler to accept @mrjj's suggestion of setting QListWidget::setFocusPolicy(Qt.NoFocus) instead. This leaves the caret visible in the QTextEdit which is as it should be, and means that I can just use QApplication.focusWidget() to identify the edit widget, which is all neater. I do lose the automatic ability for the user to use the arrow keys to scroll through the list items, but the stakeholder is happy with this.

                    Thank you both.

                    1 Reply Last reply
                    2
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      Just as a note.
                      You can use
                      QApplication::focusChanged(QWidget *old, QWidget *now)
                      to disable ListWidget in case user can focus to other widget where
                      the feature ListWidget provides cannot be used.

                      JonBJ 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        Just as a note.
                        You can use
                        QApplication::focusChanged(QWidget *old, QWidget *now)
                        to disable ListWidget in case user can focus to other widget where
                        the feature ListWidget provides cannot be used.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #10

                        @mrjj
                        Thank you for that too. For now on list double-click I simply look at QApplication.focusWidget() and only do the copy-paste if that is one of my QTextEdits which does accept items from that list, else ignore, which will do me.

                        mrjjM 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @mrjj
                          Thank you for that too. For now on list double-click I simply look at QApplication.focusWidget() and only do the copy-paste if that is one of my QTextEdits which does accept items from that list, else ignore, which will do me.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @JonB
                          Yep i did assume in your use case it would be fine to simply ignore but
                          in other cases, it might been needed to show visual clue of feature being unavailable
                          as not to be confusing for end user.

                          JonBJ 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @JonB
                            Yep i did assume in your use case it would be fine to simply ignore but
                            in other cases, it might been needed to show visual clue of feature being unavailable
                            as not to be confusing for end user.

                            JonBJ Online
                            JonBJ Online
                            JonB
                            wrote on last edited by
                            #12

                            @mrjj
                            Unfortunately, the 32K's-worth of existing code lines inherited by me make no attempt anywhere to enable/disable widgets according to availability context. Whether this is by oversight, design, or inability of end-users to understand why something is disabled and preferring click-causes-error-message-or-ignore instead I cannot say, but now is not the time to tamper with it! Were I doing it from scratch, I would have followed your suggestion!

                            mrjjM 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @mrjj
                              Unfortunately, the 32K's-worth of existing code lines inherited by me make no attempt anywhere to enable/disable widgets according to availability context. Whether this is by oversight, design, or inability of end-users to understand why something is disabled and preferring click-causes-error-message-or-ignore instead I cannot say, but now is not the time to tamper with it! Were I doing it from scratch, I would have followed your suggestion!

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @JonB
                              well if rest of code never use such design clues then i would also consider it waste of time to
                              implement it in one form :)

                              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