Elliot Howard
Elliot Howard

Reputation: 67

How to get QTextEdit's html tags without getting all of the html?

I've got a QTextEdit which I'm storing some text in from a variable called m_text. The text is stored in there like:

<ui><center>Info Plot</center><ui> <br> <table> <tr> <td> Name: </td> <td> 50A40 </td> </tr> etc...

This is what's stored in m_text anyway, it's essentially the text that I need plus some html to make it display the way I want, using a table, some underline etc.

I set the text on the QTextEdit by using setText(m_text), storing exactly what's in the variable into the edit text. My question is, how can I get that exact text back out, maybe with a few user additions (say they want to change it a bit)? toPlainText() returns everything except the html tags and toHtml() returns a massive string with loads of stuff I don't need or want (and I would like to not have to search through if possible).

I've written my entire code using these basic structure tags (like table) as a template for getting certain information out and changing other bits of information, therefore I'd like to keep the way I've done it if I can.

Is there anyway I can get all the data + the tags I have put in back out, the same way it was set from m_text?

Edit:

#include <QDialog>


class plotInfoDialog : public QDialog
{
public:
    plotInfoDialog();
    void setQTextEdit();
    void getFreeTextEditText();

 public slots:
    void editChange();

 private:
    QString m_text;

 };

 plotInfoDialog::plotInfoDialog() {

    m_ui = new Ui::plotInfoDialog();
    m_ui->setupUi(this);
    this->show();
    m_text = "<u> <center>Plot Info</center> </u> <br><table><tr><td>Name: </td><td> #BASENAME#</td></tr><tr><td>Date: </td><td>#DATE# </td></tr><tr><td>Time: </td><td>#TIME# </td></tr><tr><td>Radar: </td><td>#RADAR#</td></tr><tr><td>Polarization: </td><td> #P#</td></tr><tr><td>Range: </td><td>#RANGE# </td></tr><tr><td>Elevation: </td><td> #ELEV# </td></tr></table>";
    setQTextEdit();
    connect(m_ui->FreeTextEdit->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(editChange(int,int,int)));

}

void plotInfoDialog::setQTextEdit() {

    m_ui->FreeTextEdit->setText(m_text);
}

void plotInfoDialog::editChange(int pos, int del, int add)
{
    //update m_text with changes in FreeTextEdit here
}

void plotInfoDialog::getFreeTextEditText() {
    m_text = //get text from FreeTextEdit here
}

Upvotes: 4

Views: 1579

Answers (1)

Pavan Chandaka
Pavan Chandaka

Reputation: 12731

Actually when you setText() and when you ask your text with toPlainText(), the returned string maintains your HTML tags.

But in your case some where the string in QTextEdit is converted as HTML. So the returned string from toPlainText() is missing all your HTML tags. Look for all the references of QTextEdit and see anywhere HTML conversion is happening.

The alternate is:

Use QPlainTextEdit, if you do not want HTML at all, I mean your tags will be well preserved.

Try as said below.

to set the text

QPlainTextEdit *textEdit = new QPlainTextEdit();
QString m_text = "<ui><center>Info Plot</center><ui> <br> <table> <tr> <td> Name: </td> <td> 50A40 </td> </tr>";
textEdit->document()->setPlainText(m_text);

To get the text back

m_text = textEdit->toPlainText();

Upvotes: 4

Related Questions