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. Problem with QTextCursor selection
Forum Updated to NodeBB v4.3 + New Features

Problem with QTextCursor selection

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 4.0k 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.
  • J Offline
    J Offline
    jdowner
    wrote on last edited by
    #1

    Hi,

    I'm have a situation where I need to customize the input to a QGraphicsTextItem. In particular, I need to select characters in response to an up-arrow key press. So I have created a custom QGraphicsTextItem and over-ridden the keyPressEvent,

    @ class MyText : public QGraphicsTextItem
    {
    public:
    MyText(const char* str) : QGraphicsTextItem(str)
    {
    }

      virtual void keyPressEvent(QKeyEvent* event)
      {   
        QTextCursor cursor = textCursor();
        if (event->key() == Qt::Key_Down)
        {   
          cursor.movePosition(QTextCursor::Left,QTextCursor::KeepAnchor);
        }   
      }  
    

    };@

    Through the testing I've done, this is selecting text (e.g. if I called removeSelectedText() the text is correctly removed). The problem is that I need to have the visual indicator of the selection and I do not see it for some reason. Am I doing something wrong, or does the selection performed by QTextCursor not get displayed graphically?

    -Josh

    1 Reply Last reply
    0
    • S Offline
      S Offline
      steno
      wrote on last edited by
      #2

      You have to set the cursor back onto the text edit once you move its position.

      @
      cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor);
      setCursor(cursor);
      @

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jdowner
        wrote on last edited by
        #3

        I don't think that is quite right. setCursor() defines the mouse cursor via QCursor, I am modifying text with QTextCursor, which does not inherit from QCursor so I cannot pass it to setCursor().

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

          ok you got me, setTextCursor is the the call. textCursor returns a copy of the QTextCursor, thats why you have to set it back into the object.

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

            Arg, of course! Thank you for the help, that seems to have fixed the problem.

            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