Results 1 to 5 of 5

Thread: Make label follow qtextedit cursor

  1. #1
    Join Date
    Jun 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Make label follow qtextedit cursor

    Hi all, I'm working on a shared editor and I need to make a label to follow a cursors position in the text.


    - i tried with "ui->textEdit->viewport()->mapToGlobal(<textEdit cursor>);" -> it return me always the center of the window

    - i tried to get the textEdit position and sum it to the position of the cursor as an offset -> but return the top left corner of the window

    - i tried with "ui->textEdit->cursorRect() to get the actual cursor position -> but return the top left corner of the window

    but nothing seems to work

    Annotazione 2020-07-22 172840.png

    i want the label to align to that orange line I draw

    right now I ended up with apply an offset I set manually but aparently it vary with the computer the application is running on
    Qt Code:
    1. void MainWindow::UpdateCursorPosition(std::string user, int x, int y) {
    2.  
    3. QLabel *label;
    4. if(usersCursors.find(user) != usersCursors.end()){
    5. label = usersCursors.at(user);
    6. // set label geometry to move it
    7. label->setGeometry(x + 21, y + 40,/*label->sizeHint().width() +*/ 1, label->sizeHint().height() + 10);
    8. }
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Make label follow qtextedit cursor

    It sounds to me like you are confusing the mouse cursor with the QTextCursor. The QTextCursor position is defined as a character location within the QTextDocument, not the pixel location on screen in the QTextEdit widget.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Make label follow qtextedit cursor

    I know they are different, but I was assuming there was also a way to know the pixel location of the text cursor.
    So if there is no way to get the pixel position of the cursor how can I possibly achieve my goal?

  4. #4
    Join Date
    Jun 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Make label follow qtextedit cursor

    https://doc.qt.io/qt-5/qtextedit.html#cursorRect-1 I supposed this was the function I was looking for but probably I misunderstood the "that includes the cursor of the text edit." part

  5. #5
    Join Date
    Jun 2020
    Posts
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Make label follow qtextedit cursor

    I solved the problem

    I used

    "label->move(<point>)" instead of "setGeometry"

    and I passed

    "editor->mapTo(window(), editor->cursorRect(<cursor>).topLeft()" as point

    so basically the point where the cursor is at the moment, remapped in the window coordinates

  6. The following user says thank you to FabioG for this useful post:

    d_stranz (23rd July 2020)

Similar Threads

  1. Replies: 0
    Last Post: 24th April 2014, 09:54
  2. Replies: 3
    Last Post: 18th October 2011, 12:54
  3. Replies: 2
    Last Post: 25th November 2010, 10:53
  4. "Follow symbol under cursor" does not work...?
    By liormessinger in forum Qt Tools
    Replies: 0
    Last Post: 16th September 2009, 18:22
  5. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 10:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.