Deleting a table from QTextDocument deletes only the text (emty table remains)
-
Hi,
I'm trying to delete a table from a QTextDocument (in a QTextBrowser).
I delete the text, but when I look at the html of the document the table remains.
So when I try to Add some a new table, its aded a line lower, than the previous one.
I have multiple tables before the one I want to delete. The table that needs to be deleted is the last table in the document (there is nothing more in the document after the table I want to delete).My code is:
QTextDocument *document(textBrowserReadConversation->document()); QTextCursor cursor(document); cursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::End); cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); cursor.select(QTextCursor::BlockUnderCursor); cursor.removeSelectedText(); cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::PreviousBlock, QTextCursor::MoveAnchor); cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor); cursor.select(QTextCursor::BlockUnderCursor); cursor.removeSelectedText(); cursor.deletePreviousChar();
I found this:
http://stackoverflow.com/questions/16996679/remove-block-from-qtextdocument
http://stackoverflow.com/questions/10417795/remove-a-line-block-from-qtextedit
and some other, that I don't remember. -
Hi,
Can you provide a more complete sample where you also setup the table ?
-
My methods for adding the tables:
void SingleMessagingWidget::appendTextToMessageWindowRead(const QString &d, const QString &t) { resetReadConfirmationToMessageWindow(); QString html; QString date(d); QString hreftText(t); hreftText.replace(regExp, "<a href='\\1'>\\1</a>" ); hreftTextSent = hreftText; dateSent = date; if(messageSender != HIM){ messageSender = HIM; writeHeader(html, date, contactName); } writeMessage(html, hreftText, date); textBrowserReadConversation->append(html); QScrollBar *sb = textBrowserReadConversation->verticalScrollBar(); sb->setValue(sb->maximum()); qDebug() << textBrowserReadConversation->toPlainText(); qDebug() << contactExtNo; } void SingleMessagingWidget::writeHeader(QString &html, QString &date, QString &who) { if(messageSender == ME){ html.append("<p></p><table width=100% cellpadding=2 cellspacing=0 border=0 style=\" white-space: pre-wrap; font-family:MS Shell Dlg 2; " "font-size:8pt; font-weight:400; font-style:normal; text-decoration:none;\"><tr bgcolor = lightgray><td width=70% text-align=left><b> " + who +"</b></td><td width=30% align=right><font size = 3 color = gray>" + date +"</font></td></tr></table>"); } else { html.append("<p></p><table width=100% cellpadding=2 cellspacing=0 border=0 style=\" white-space: pre-wrap; font-family:MS Shell Dlg 2; " "font-size:8pt; font-weight:400; font-style:normal; text-decoration:none;\"><tr bgcolor = lightblue><td width=70% text-align=left><b> " + who +"</b></td><td width=30% align=right><font size = 3 color = gray>" + date +"</font></td></tr></table>"); } } void SingleMessagingWidget::writeMessage(QString &html, QString &text, QString &date) { writeMessageText(html, text); writeMessageDate(html, date); } void SingleMessagingWidget::writeMessageText(QString &html, QString &text) { html.append("<table width=100% cellpadding=4 cellspacing=0 border=0 ><tr style=\" white-space: pre-wrap; font-family:MS Shell Dlg 2; " "font-size:10pt; font-weight:400; font-style:normal; text-decoration:none;\"><td width=80% text-align=left valign=bottom>"); html.append(text); html.append("</td>"); } void SingleMessagingWidget::writeMessageDate(QString &html, QString &date) { date.remove(0,11); date.chop(3); if(messageSender == ME){ html.append("</td><td width=20% align=right valign=bottom><font size = 2 color = red>" + date +"</font></td><myTag></myTag></tr></table>"); } else { html.append("</td><td width=20% align=right valign=bottom><font size = 2 color = blue>" + date +"</font></td><myTag></myTag></tr></table>"); } }
-
Is it me or are you mixing the QTextDocument rich text classes with HTML ?
-
No it's not, however it's the kind of details that are important. QTextCursor provides API to insert tables and I thought you were using that.
-
That's what I thought. (after some trial and error)
So to use QTextCursor to select and erase a Table, I need to insert it using the QTextCursor and QTextTable?
As for the project that I'm working on, I figured that I do not need to delete the whole table. Fortunetly I can use cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor) to move from one cell to another. Then I just erase the content of a selected row and fill the cells, one by one, with new text.