Results 1 to 3 of 3

Thread: QTextEdit - Getting the current line/cursor position (Qt Jambi)

  1. #1
    Join Date
    Apr 2008
    Posts
    3
    Thanks
    2
    Platforms
    MacOS X

    Default QTextEdit - Getting the current line/cursor position (Qt Jambi)

    Hi, I'm writing a simple text editor and I'm using QTextEdit as a base
    I'm trying to implement a "indent" action.
    I basically just want to add four spaces at the beginning of the current line.
    By "current line" I mean the line that the cursor (or whatever it's called) is at. You know... Just like in any other editor.

    The problem is that I´m having trouble finding the position in the document I want. So I can get the current line.

    I've been playing around with QTextCursor and such, but with no luck.
    I've tried updating othe cursor on the cursorPositionChanged signal, but that only works when some content is changed. Not when i move the cursor around with the arrow keys or click somewhere else.

    The JavaDoc suggests that I should use cursorPositionChanged() instead for this, but that doesn't seem to exist in Jambi.

    Could anyone help me out? Point me in the right direction or suggest some kind of workaround?

    Thanks for your time,
    Thomas

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit - Getting the current line/cursor position (Qt Jambi)

    To find the line ...
    you need QTextLine it can show you rect line nr char on cursor and X vieport position mouse and other thing....



    Qt Code:
    1. QTextLine currentTextLine(const QTextCursor &cursor)
    2. {
    3. const QTextBlock block = cursor.block();
    4. if (!block.isValid())
    5. return QTextLine();
    6.  
    7. const QTextLayout *layout = block.layout();
    8. if (!layout)
    9. return QTextLine();
    10.  
    11. const int relativePos = cursor.position() - block.position();
    12. return layout->lineForTextPosition(relativePos);
    13. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTextEdit - Getting the current line/cursor position (Qt Jambi)

    This is how I did it: (Best way I found WITHOUT moving the cursor around)

    QString MainWindow::currentInputLine(){
    int pos=ui->INPUT->textCursor().position();
    QString temp=ui->INPUT->toPlainText();
    QString before=temp.left(pos);
    before=before.mid(temp.lastIndexOf("\n"),-1);
    QString after=temp.mid(pos,temp.indexOf("\n",pos));
    return before+after;
    }

    *sorry Im new I dont know how to paste code correctly.
    Note this does not work, but if you guys no why I think its a little more efficient if you can fix it.(only worked for the first line)
    QString MainWindow::currentInputLine(){
    int pos=ui->INPUT->textCursor().position();
    QString temp=ui->INPUT->toPlainText();
    return temp.mid(temp.lastIndexOf("\n",pos), temp.indexOf("\n",pos));
    }
    Last edited by elihall08; 20th February 2012 at 23:02. Reason: format

Similar Threads

  1. Qt designer plugin errors on OSX Leopard
    By mpotocnik in forum Qt Tools
    Replies: 10
    Last Post: 21st January 2008, 10:45
  2. Distributing QT application for Mac OS
    By mb0 in forum Qt Programming
    Replies: 1
    Last Post: 31st May 2007, 19:59
  3. Replies: 15
    Last Post: 21st April 2007, 18:46
  4. Current char in QTextEdit
    By jlbrd in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 10:30

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.