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. Editiable QGraphicsTextItem problem when setting pageSize and documentMargin
QtWS25 Last Chance

Editiable QGraphicsTextItem problem when setting pageSize and documentMargin

Scheduled Pinned Locked Moved Solved General and Desktop
qgraphicsitempagesizedocumentmargin
16 Posts 2 Posters 5.2k Views
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #3

    Hi,

    Is there a specific size that triggers that or any size does it ?

    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
    • J Offline
      J Offline
      Jan-Willem
      wrote on last edited by
      #4

      Hi,

      I've checked it with different sizes and it seems be happening with all of them.

      Initial I used QSizeF(500, 500) and changed the height to 600 and 700. While the exact line from where it becomes uneditable changes, the basic problem remains. After that I've tried changing to totaly different sizes with more or less the same outcome.

      However, when setting the pageSize for example to QSizeF(540, 500) and the QGraphicsTextItem::textWidth to 500, the whole text from top to bottom becomes editable (I can click with the mouse and place the textCursor there). Problem is that the text on the right becomes visualy uneditable, probably because of the difference in textWidth and pageSize.width.

      When also giving a documentMargin of 20, the textWidth had to become 440 to achieve the same result.

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

        Which version of Qt are you using ? On which OS ?

        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
        • J Offline
          J Offline
          Jan-Willem
          wrote on last edited by
          #6

          Qt5.6 (from Qt.io) on Debian Jessie.

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

            Can you check against the latest version to see if it still happening ?

            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
            • J Offline
              J Offline
              Jan-Willem
              wrote on last edited by
              #8

              I just installed 5.7 and ran the program. Still the same problem.

              Perhaps something wrong with my implementation?
              I've adjusted the TextEdit example as in using a QGraphicsTextItem instead of a QTextEdit. In the Load function, after setting the contents of the QGraphicsTextItem, I set the pageSize and the documentMargin.

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

                Can you share your implementation ?

                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
                • J Offline
                  J Offline
                  Jan-Willem
                  wrote on last edited by
                  #10

                  from textedit.cpp

                  in the constructor:

                  scene = new QGraphicsScene;
                  view = new QGraphicsView(scene);
                  textEdit = new TextItem;
                  
                  scene->addItem(textEdit);
                  setCentralWidget(view);
                  

                  in the Load function:

                  bool TextEdit::load(const QString &f)
                  {
                      if (!QFile::exists(f))
                          return false;
                      QFile file(f);
                      if (!file.open(QFile::ReadOnly))
                          return false;
                  
                      QByteArray data = file.readAll();
                      QTextCodec *codec = Qt::codecForHtml(data);
                      QString str = codec->toUnicode(data);
                      if (Qt::mightBeRichText(str)) {
                          textEdit->setHtml(str);
                      } else {
                          str = QString::fromLocal8Bit(data);
                          textEdit->setPlainText(str);
                      }
                      setCurrentFileName(f);
                  
                      textEdit->setTextWidth(500);
                  
                      QTextDocument *doc = textEdit->document();
                      doc->setPageSize(QSizeF(500, 500));
                      doc->setDocumentMargin(20);
                  
                      return true;
                  }
                  

                  textitem.h:

                  class TextItem : public QGraphicsTextItem
                  {
                  public:
                      TextItem(QGraphicsItem *parent = 0);
                  
                      QRectF boundingRect() const;
                      void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
                  };
                  

                  textitem.cpp

                  TextItem::TextItem(QGraphicsItem *parent)
                      : QGraphicsTextItem(parent)
                  {
                      setCursor(QCursor(Qt::IBeamCursor));
                      setFlags(ItemSendsGeometryChanges);
                      setTextInteractionFlags(Qt::TextEditorInteraction);
                  }
                  
                  QRectF TextItem::boundingRect() const
                  {
                      return QRectF (this->pos(), QSizeF(540, 8000));
                  }
                  
                  void TextItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
                  {
                      QRectF r1(0, 0, 500, 500);
                      QRectF r2(0, 500, 500, 500);
                      QRectF r3(0, 1000, 500, 500);
                  
                      painter->fillRect(r1, QBrush(Qt::white));
                      painter->fillRect(r2, QBrush(Qt::red));
                      painter->fillRect(r3, QBrush(Qt::blue));
                  
                      QGraphicsTextItem::paint(painter, option, widget);
                  }
                  

                  As you can see I have kept the implementation quite simple for testing purposes.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    Jan-Willem
                    wrote on last edited by
                    #11

                    Some observations I made when going through the source code of QGraphicsTextItem, QTextDocument and QWidgetTextControl:

                    • setting the textWidth on a QGraphicsTextItem will in the end result in setting a pageSize QSizeF(textWidth, -1) on the QTextDocument, so it should not be necessary to setting a textWidth and a pageSize;
                    • in the function QGraphicsTextItem::paint the option->exposedRect is used for the drawing of the contents from the QWidgetTextControl. The controlOffset is used for setting the position of the exposedRect, which is equal to the pagenumber * pageheight;
                    • the size of the viewportRect in the documentLayout is set through setViewport. This is a setting in the private QTextDocumentlayout, so not for us to use. Since the result of the viewportRect seems to be only used if the textWidth/pageSize.width == 0, it is probably of no concern;
                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      Jan-Willem
                      wrote on last edited by
                      #12

                      I've added some blank lines to the end of the example html file with:

                      <br/>
                      

                      the whole text became editable, except of course for the lines at the end.

                      After that I added some blank lines in the text (with the enter key).
                      Lets say there are about 222 visible lines of text. From line 219 the text becomes visually uneditable. After entering five more line, the text below moves down accordingly.
                      But from line 219 the text remains visually uneditable.

                      Somehow the 'viewport' is not adjusted. While, when the documentMargin and the textWidth are set, everything behaves like it is expected to. But when setting the pageSize, the problems arise.

                      Since textWidth sets the pageSize.width while the pageSize.heigth remains -1, it seems that there is something with the pageSize.height which is screwing up things. Or something else I forgot to do when setting the pageSize, but what?

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        Jan-Willem
                        wrote on last edited by
                        #13

                        After a lot of tinkering I still haven't found the cause of the problem, let alone a solution.
                        Of course I can add a QTextEdit to a QGraphicsScene, but that still doesn't explain what's happening with the QGraphicsTextItem.

                        Someone an idea?

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

                          You may have found a bug. Did you check the bug report system ?

                          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
                          • J Offline
                            J Offline
                            Jan-Willem
                            wrote on last edited by
                            #15

                            I have checked it and created a new bug request: QTBUG-55527

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              Jan-Willem
                              wrote on last edited by
                              #16

                              I think I have found the problem. I went through the source code of the QGraphicsTextItem and came across the private _q_updateBoundingRect. In there there is a line:

                              if (size == qq->m_boundingRect.size() || pageSize.height() != -1)
                                  return;
                              

                              It looks like the pageSize.height() != -1 part is the problem. I have removed it and it seems to be working!

                              1 Reply Last reply
                              0

                              • Login

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