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. QTextBlockFormat resets by pressing the return key for empty block
Forum Updated to NodeBB v4.3 + New Features

QTextBlockFormat resets by pressing the return key for empty block

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 265 Views 1 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.
  • K Offline
    K Offline
    Koru
    wrote on last edited by
    #1

    I am making a rich text editor where each paragraph has a different background depending on its type. I set the background using this code:

        QTextCursor cursor(ui->textEdit->textCursor());
        QTextBlockFormat block_format = cursor.blockFormat();
        block_format.setBackground(Qt::yellow);
        cursor.setBlockFormat(block_format);
        ui->textEdit->setTextCursor(cursor);
    

    This works well and sets the expected background for the paragraph. However, when I press enter on an empty block I noticed a weird behavior. Instead of adding a new paragraph after the existing one, the active block (the one where the cursor is set) resets its formatting. When the block is non-empty everything works as expected.
    I want to leave the block format for the empty paragraph and add a new block by pressing the return key. What am I missing?

    1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      If memory serves well, there was a report on the Qt bug tracker for something similar but when removing all blocks and starting to write again. You should check it.

      K Offline
      K Offline
      Koru
      wrote on last edited by
      #3

      @SGaist thanks, I tried to find something but no luck.
      However, I managed to find a workaround. I installed an event filter for the text widget that is listening for the return key and manually inserts a new block.
      If someone is interested in the code:

          if (event->type() == QEvent::KeyPress && object == ui->textEdit)
          {
              QKeyEvent* key = static_cast<QKeyEvent*>(event);
              if (key->key() == Qt::Key_Return)
              {
                  QTextCursor cursor = ui->textEdit->textCursor();
                  cursor.insertBlock();
                  ui->textEdit->setTextCursor(cursor);
                  return true;
              }
              return false;
          }
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        If memory serves well, there was a report on the Qt bug tracker for something similar but when removing all blocks and starting to write again. You should check it.

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

        K 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          If memory serves well, there was a report on the Qt bug tracker for something similar but when removing all blocks and starting to write again. You should check it.

          K Offline
          K Offline
          Koru
          wrote on last edited by
          #3

          @SGaist thanks, I tried to find something but no luck.
          However, I managed to find a workaround. I installed an event filter for the text widget that is listening for the return key and manually inserts a new block.
          If someone is interested in the code:

              if (event->type() == QEvent::KeyPress && object == ui->textEdit)
              {
                  QKeyEvent* key = static_cast<QKeyEvent*>(event);
                  if (key->key() == Qt::Key_Return)
                  {
                      QTextCursor cursor = ui->textEdit->textCursor();
                      cursor.insertBlock();
                      ui->textEdit->setTextCursor(cursor);
                      return true;
                  }
                  return false;
              }
          
          1 Reply Last reply
          1
          • K Koru has marked this topic as solved on

          • Login

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