PDA

View Full Version : how to get the text from QTextEdit



qt_user
16th August 2010, 13:41
how to get the text from a QTextEdit widget?

ahmdsd_ostora
16th August 2010, 13:47
if you want the text in HTML format:
QString toHTML();

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

philw
2nd August 2011, 19:30
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].


QTextEdit* msgBox = new QTextEdit (this);
... ... ...

// 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.
QApplication::clipboard()->clear (QClipboard::Clipboard);

msgBox->copy();
const QString clipSel2 =
QApplication::clipboard()->text (QClipboard::Clipboard);

elcuco
2nd August 2011, 20:22
Use QTextCursor::selectedText():



QPlainTextEdit *d = ...;
QString s = d->textCursor().selectedText();

norobro
2nd August 2011, 20:23
Maybe I misunderstand, but are you saying the following doesn't work?
QTextCursor cursor(msgBox->textCursor());
const QString clipSel2 = cursor.selectedText();

philw
22nd August 2011, 00:22
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!


Maybe I misunderstand, but are you saying the following doesn't work?
QTextCursor cursor(msgBox->textCursor());
const QString clipSel2 = cursor.selectedText();