Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-79

QTextBlockUserData not moved with its QTextBlock if all of its QTextBlock is moved down a row

    XMLWordPrintable

Details

    Description

      QTextBlockUserData is not moved with its QTextBlock if all of its QTextBlocks are moved down a row

      To reproduce:

      #include <QtGui>

      struct Data : public QTextBlockUserData
      {
      int id;
      };

      class TextEdit : public QTextEdit
      {
      Q_OBJECT

      public:
      TextEdit(QWidget *parent = 0)
      : QTextEdit(parent)
      {
      }
      void paintEvent(QPaintEvent *e)

      { QPainter painter(viewport()); drawUserData(&painter); painter.end(); QTextEdit::paintEvent(e); }

      void drawUserData(QPainter *painter)
      {
      QTextDocument *doc = document();
      QTextBlock currentBlock = doc->begin();
      for (; currentBlock.isValid(); currentBlock = currentBlock.next())

      { Data *data = static_cast<Data *>(currentBlock.userData()); QPen pen; if (data != 0) pen.setColor(Qt::blue); else pen.setColor(Qt::red); painter->setPen(pen); QRectF rect = document()->documentLayout()->blockBoundingRect(currentBlock); rect.adjust(0., 2., 0., -2.); painter->drawRect(rect); }

      }
      public slots:
      void populateUserData()
      {
      QTextDocument *doc = document();
      QTextBlock currentBlock = doc->begin();
      for (; currentBlock.isValid(); currentBlock = currentBlock.next()) {
      if (currentBlock.userData() == 0)

      { Data *d = new Data; d->id = idPool++; currentBlock.setUserData(d); }

      }
      update();
      }
      private:
      static int idPool;
      };

      int TextEdit::idPool = 0;
      #include "main.moc"

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);
      QDialog dialog;
      QVBoxLayout *layout = new QVBoxLayout;
      QPushButton *populateButton = new QPushButton;
      populateButton->setText(QObject::tr("Populate with QTextBlockUserData"));
      TextEdit *edit = new TextEdit;
      layout->addWidget(populateButton);
      layout->addWidget(edit);
      dialog.setLayout(layout);
      QObject::connect(populateButton, SIGNAL(clicked()), edit, SLOT(populateUserData()));
      dialog.show();
      return app.exec();
      }

      What the attached example does is make a blue rectangle around any QTextBlocks that have QTextBlockUserData set on them through QTextBlock::setUserData(...). If they don't have any set then they're colored red. There's a button which gives all QTextBlocks some user data.
      The value isn't important in this example, since it shows the issue with the red and blue rectangles.

      The attached screen shots that show the issue using a QTextEdit with an overridden paint event to draw rectangles around the QTextBlocks.
      ==========================
      Note the red box means that the empty QTextBlock has no QTextBlockUserData set for it. I.e., userData() returns a
      null pointer for that QTextBlock.

      Type some text "ooooo........ooooo" in the first block.

      Press the button so all blocks get some QTextBlockUserData.
      Notice that the rectangle is now blue, which means userData() does not return null.

      Move to the beginning of the line containing "ooooo......oooooo".

      Press enter so that "oooo.....ooooo" is moved down a row.
      Note that the "oooo..ooooo" text has a red rectangle around it, so its userData() returns null. However the blue rectangle is still there above it for the newly inserted, empty block.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            shausman Simon Hausmann
            rve Anders Bakken
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes