Results 1 to 6 of 6

Thread: how to get the text from QTextEdit

  1. #1
    Join Date
    Jul 2010
    Posts
    50
    Thanks
    7
    Platforms
    Windows

    Default how to get the text from QTextEdit

    how to get the text from a QTextEdit widget?

  2. #2
    Join Date
    Jun 2010
    Posts
    86
    Thanks
    10
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to get the text from QTextEdit

    if you want the text in HTML format:
    QString toHTML();

    rather, if you want the plain text:
    QString toPlainText();

  3. The following 2 users say thank you to ahmdsd_ostora for this useful post:

    Hossein (23rd October 2015), selmi.asma (5th July 2013)

  4. #3
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from QTextEdit

    Is there a way to get the SELECTED text from a QTextEdit? (i.e. selected by the user in the GUI?).

    The QTextCursor seems unfruitful -- when creating one on the QTextEdit's QTextDocument -- either before or after the selection -- its anchor and position -- don't contain results from user selections.

    I'm currently "PUSHING" the QTextEdit through the system clipboard via QTextEdit::copy(), see below. I hate nailing the system clipboard for this purpose. There must be a better way.

    [This is with Qt 4.6.3 on Windows and Solaris].
    Qt Code:
    1. QTextEdit* msgBox = new QTextEdit (this);
    2. ... ... ...
    3.  
    4. // We have to "push" the user-selected text to the conventional
    5. // system clipboard (using the QClipboard::Clipboard clipboard mode).
    6.  
    7. // Clear first, because the QTextEdit::copy() command doesn't do this
    8. // if nothing is selected.
    9. QApplication::clipboard()->clear (QClipboard::Clipboard);
    10.  
    11. msgBox->copy();
    12. const QString clipSel2 =
    13. QApplication::clipboard()->text (QClipboard::Clipboard);
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Posts
    368
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get the text from QTextEdit

    Use QTextCursor::selectedText():

    Qt Code:
    1. QPlainTextEdit *d = ...;
    2. QString s = d->textCursor().selectedText();
    To copy to clipboard, switch view to plain text mode 

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

    zeFree (10th November 2012)

  7. #5
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: how to get the text from QTextEdit

    Maybe I misunderstand, but are you saying the following doesn't work?
    Qt Code:
    1. QTextCursor cursor(msgBox->textCursor());
    2. const QString clipSel2 = cursor.selectedText();
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to norobro for this useful post:

    philw (22nd August 2011)

  9. #6
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Is there a way to get the SELECTED text from a QTextEdit?

    Oh! Quite right. THANK YOU! (Sorry for taking so long to say so).

    I was going after the wrong instance of the QTextCursor. I was retrieving one from the QTextEdit's QDocument -- or creating one FROM that QDocument. Those QTextCursors aren't "connected" to the user selection within the actual QTextEdit widget. BUT the QTextCursor available directly from the QTextEdit DOES (reasonably enough) indicate the character selection made by the user in the user interface).

    • QTextCursor QTextEdit::textCursor () const


    Quite right. THANK YOU!

    Quote Originally Posted by norobro View Post
    Maybe I misunderstand, but are you saying the following doesn't work?
    Qt Code:
    1. QTextCursor cursor(msgBox->textCursor());
    2. const QString clipSel2 = cursor.selectedText();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTextEdit::text()
    By hazardpeter in forum Newbie
    Replies: 2
    Last Post: 5th August 2009, 22:13
  2. QTextEdit + paste rich text as plain text only
    By Yong in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2008, 17:45
  3. QTextEdit -> add Text
    By ape in forum Newbie
    Replies: 16
    Last Post: 19th December 2007, 15:59
  4. QTextEdit and mark Text
    By J-jayz-Z in forum Qt Programming
    Replies: 12
    Last Post: 28th September 2007, 01:14
  5. QTextEdit is possible collapse text?
    By angel75 in forum Qt Programming
    Replies: 2
    Last Post: 4th July 2006, 14:09

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.