Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Deleting a table from QTextDocument deletes only the text (emty table remains)
Qt 6.11 is out! See what's new in the release blog

Deleting a table from QTextDocument deletes only the text (emty table remains)

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • michalosM Offline
    michalosM Offline
    michalos
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • michalosM Offline
        michalosM Offline
        michalos
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • michalosM Offline
          michalosM Offline
          michalos
          wrote on last edited by
          #4

          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>");
              }
          }
          
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • michalosM Offline
              michalosM Offline
              michalos
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • michalosM Offline
                  michalosM Offline
                  michalos
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved