Results 1 to 7 of 7

Thread: How to select a line of text

  1. #1
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default How to select a line of text

    Hi all

    I'm working on my first app using pyqt and i need some help

    I want to be able to click on a line of text in a QtextEdit box, and be able to load that line into a string.

    How would i do this...?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to select a line of text

    Hi,

    if you click on a line in a text edit, then also the cursor jumps to that line. Get that cursor via QTextEdit::textCursor() and use QTextCursor::movePosition() with QTextCursor::StartOfBlock and QTextCursor::EndOfBlock to select the line. Then grab the text via QTextCursor::selectedText().

    That's all!

  3. #3
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: How to select a line of text

    Thanks for the reply

    maybe you could help out with a demo code.

    I've had a play, but no luck. I had thought that to move to a function after a click would be the first step, I was thinking something like this but it didn't work.

    Qt Code:
    1. self.connect(self.QTextEdit,SIGNAL("clicked()"),self.test)
    To copy to clipboard, switch view to plain text mode 
    what am i doing wrong?
    Last edited by benlyboy; 25th April 2010 at 19:15.

  4. #4
    Join Date
    Apr 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to select a line of text

    Hi, I am very beginner myself but I tested out what Lykurg said and it works.



    I created QTextEdit and QLabel with QTCreator UI editor (based on QDialog).

    Added this to dialog class in header
    Qt Code:
    1. private slots:
    2. void on_textEdit_clicked();
    To copy to clipboard, switch view to plain text mode 

    Then I added this into constructor in dialog.cpp.
    Qt Code:
    1. connect(ui->textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(on_textEdit_clicked()));
    To copy to clipboard, switch view to plain text mode 

    and into dialog.cpp:
    Qt Code:
    1. void Dialog::on_textEdit_clicked()
    2. {
    3. QTextCursor cursor;
    4. QString text;
    5.  
    6. cursor = ui->textEdit->textCursor();
    7. cursor.movePosition(QTextCursor::StartOfBlock);
    8. cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
    9. text = cursor.selectedText();
    10.  
    11. ui->label->setText(text);
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

    Sorry, I dont use python. So you've got to translate it yourself.

    Works great. I however expected the line to turn blue to indicate selection but this did not happen.

  5. #5
    Join Date
    Feb 2010
    Posts
    12
    Qt products
    Qt4 PyQt3 PyQt4
    Platforms
    Unix/X11

    Default Re: How to select a line of text

    yes thanks that works great

    thanks all for the help

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to select a line of text

    Quote Originally Posted by qratman View Post
    Works great.
    Of course, because I said it
    I however expected the line to turn blue to indicate selection but this did not happen.
    That is because textCursor() gives a copy of the original (="displayed") text cursor. All things you do with it don't show up in the editor. If you wish so, you have to set it back via setTextCursor(). But be aware that this will in your case end up in a never ending loop because of your connect statement.

    In the example, I wouldn't use the clicked() signal. Only use cursorPositionChanged() and check if the line was changed (with a local member variable). Only if it changed, do all the cursor stuff.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to select a line of text

    Quote Originally Posted by Lykurg View Post
    Of course, because I said it
    Lol... I have nothing more to say
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Remove a line in a text file
    By jaca in forum Newbie
    Replies: 1
    Last Post: 18th March 2010, 22:13
  2. Auto text select in QTableWidgetItem
    By tstankey in forum Newbie
    Replies: 2
    Last Post: 5th October 2006, 20:40
  3. count line number in text
    By Alina in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2006, 08:29
  4. How to get text of last line only in QTextEdit?
    By rajesh in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2006, 13:37
  5. a Text Editor with line numbers...
    By fullmetalcoder in forum Qt Programming
    Replies: 47
    Last Post: 5th April 2006, 11:10

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
  •  
Qt is a trademark of The Qt Company.