Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Editiable QGraphicsTextItem problem when setting pageSize and documentMargin

    General and Desktop
    qgraphicsitem pagesize documentmargin
    2
    16
    4142
    Loading More Posts
    • 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.
    • J
      Jan-Willem last edited by

      I'm creating an editor using a QGraphicsTextItem. Everything works, but I ran into a problem when setting the pageSize and the documentMarging. When the pageSize and the documentMargin are set, the contents of the QGraphicsTextItem is shown as in a documentview. However, for some reason the last lines in the document become visualy uneditable. In other words, I cannot place the cursor or select that text with the mouse.

      Although I've tried to narrow the problem down to the pageSize or the documentMargin, so far I've not found a solution. Also, extending the height of the boundingRect did not solved the problem.

      Someone any idea how to solve this?

      1 Reply Last reply Reply Quote 0
      • J
        Jan-Willem last edited by

        UPDATE!

        When setting only the documentMargin, all is well. However, the problem occurs when setting the pageSize. For some reason the last lines become visual uneditable. My guess is that is has something to do with the viewport settings in the private QTextDocumentLayout.

        Someone any idea?

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          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 Reply Quote 1
          • J
            Jan-Willem last edited by

            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 Reply Quote 0
            • SGaist
              SGaist Lifetime Qt Champion last edited by

              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 Reply Quote 1
              • J
                Jan-Willem last edited by

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

                1 Reply Last reply Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 1
                  • J
                    Jan-Willem last edited by

                    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 Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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 Reply Quote 1
                      • J
                        Jan-Willem last edited by

                        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 Reply Quote 0
                        • J
                          Jan-Willem last edited by

                          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 Reply Quote 0
                          • J
                            Jan-Willem last edited by

                            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 Reply Quote 0
                            • J
                              Jan-Willem last edited by

                              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 Reply Quote 0
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                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 Reply Quote 1
                                • J
                                  Jan-Willem last edited by

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

                                  1 Reply Last reply Reply Quote 0
                                  • J
                                    Jan-Willem last edited by

                                    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 Reply Quote 0
                                    • First post
                                      Last post