how to get the text from a QTextEdit widget?
how to get the text from a QTextEdit widget?
if you want the text in HTML format:
QString toHTML();
rather, if you want the plain text:
QString toPlainText();
Hossein (23rd October 2015), selmi.asma (5th July 2013)
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:
... ... ... // We have to "push" the user-selected text to the conventional // system clipboard (using the QClipboard::Clipboard clipboard mode). // Clear first, because the QTextEdit::copy() command doesn't do this // if nothing is selected. msgBox->copy();To copy to clipboard, switch view to plain text mode
Use QTextCursor::selectedText():
Qt Code:
QPlainTextEdit *d = ...;To copy to clipboard, switch view to plain text mode
zeFree (10th November 2012)
Maybe I misunderstand, but are you saying the following doesn't work?Qt Code:
To copy to clipboard, switch view to plain text mode
philw (22nd August 2011)
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!
Bookmarks