Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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.


  • Lifetime Qt Champion

    Hi,

    Can you provide a more complete sample where you also setup the table ?



  • I set up the table using HTML (<table><tr><td>). I'll paste the code tomorrow.



  • 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>");
        }
    }
    
    

  • Lifetime Qt Champion

    Is it me or are you mixing the QTextDocument rich text classes with HTML ?



  • Is it bad practice, to use HTML in a QTextDocument?


  • Lifetime Qt Champion

    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.


Log in to reply