Stop QTextCursor::insertText() from modifying QTextDocument scrollbar range










1















I have a QTextEdit that contains a QTextDocument, which is being programatically edited using the QTextCursor interface. The document is being edited with QTextCursor::insertText().



I load the text file being edited in chunks, so the initial size of the QTextDocument might only be 20 lines even though the document is 100,000 lines. However, I want the QTextEdit scrollbar to reflect the full size of the document instead of just the 20 line document it's currently displaying.



The QTextEdit's scrollbar range is set with QScrollBar::setMaximum() which adjusts the scrollbar to the proper size on the initial opening of the file, but when QTextCursor::insertText() is called the QScrollBar's range is recalculated.



I've already tried calling QScrollBar::setMaximum() after each QTextCursor::insertText() event, but it just makes the whole UI jerky and sloppy.



Is there any way to keep the range of the QScrollBar while the QTextDocument is being modified?










share|improve this question


























    1















    I have a QTextEdit that contains a QTextDocument, which is being programatically edited using the QTextCursor interface. The document is being edited with QTextCursor::insertText().



    I load the text file being edited in chunks, so the initial size of the QTextDocument might only be 20 lines even though the document is 100,000 lines. However, I want the QTextEdit scrollbar to reflect the full size of the document instead of just the 20 line document it's currently displaying.



    The QTextEdit's scrollbar range is set with QScrollBar::setMaximum() which adjusts the scrollbar to the proper size on the initial opening of the file, but when QTextCursor::insertText() is called the QScrollBar's range is recalculated.



    I've already tried calling QScrollBar::setMaximum() after each QTextCursor::insertText() event, but it just makes the whole UI jerky and sloppy.



    Is there any way to keep the range of the QScrollBar while the QTextDocument is being modified?










    share|improve this question
























      1












      1








      1








      I have a QTextEdit that contains a QTextDocument, which is being programatically edited using the QTextCursor interface. The document is being edited with QTextCursor::insertText().



      I load the text file being edited in chunks, so the initial size of the QTextDocument might only be 20 lines even though the document is 100,000 lines. However, I want the QTextEdit scrollbar to reflect the full size of the document instead of just the 20 line document it's currently displaying.



      The QTextEdit's scrollbar range is set with QScrollBar::setMaximum() which adjusts the scrollbar to the proper size on the initial opening of the file, but when QTextCursor::insertText() is called the QScrollBar's range is recalculated.



      I've already tried calling QScrollBar::setMaximum() after each QTextCursor::insertText() event, but it just makes the whole UI jerky and sloppy.



      Is there any way to keep the range of the QScrollBar while the QTextDocument is being modified?










      share|improve this question














      I have a QTextEdit that contains a QTextDocument, which is being programatically edited using the QTextCursor interface. The document is being edited with QTextCursor::insertText().



      I load the text file being edited in chunks, so the initial size of the QTextDocument might only be 20 lines even though the document is 100,000 lines. However, I want the QTextEdit scrollbar to reflect the full size of the document instead of just the 20 line document it's currently displaying.



      The QTextEdit's scrollbar range is set with QScrollBar::setMaximum() which adjusts the scrollbar to the proper size on the initial opening of the file, but when QTextCursor::insertText() is called the QScrollBar's range is recalculated.



      I've already tried calling QScrollBar::setMaximum() after each QTextCursor::insertText() event, but it just makes the whole UI jerky and sloppy.



      Is there any way to keep the range of the QScrollBar while the QTextDocument is being modified?







      c++ qt qtextedit qtextdocument qtextcursor






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jul 24 '18 at 13:36









      George V.George V.

      61




      61






















          2 Answers
          2






          active

          oldest

          votes


















          0














          Yes. You'd depend on the implementation detail. In QTextEditPrivate::init(), the following connection is made:



          Q_Q(QTextEdit);
          control = new QTextEditControl(q);
          ...
          QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars()))


          Here, q is of the type QTextEdit* and is the Q-pointer to the API object. Thus, you'd need to disconnect this connection, and manage the scroll bars on your own:



          bool isBaseOf(const QByteArray &className, const QMetaObject *mo) 
          while (mo)
          if (mo->className() == className)
          return true;
          mo = mo->superClass();

          return false;


          bool setScrollbarAdjustmentsEnabled(QTextEdit *ed, bool enable)
          QObject *control = ;
          for (auto *ctl : ed->children())
          if (isBaseOf("QWidgetTextControl", ctl->metaObject())
          Q_ASSERT(!control);
          control = ctl;


          if (!control)
          return false;
          if (enable)
          return QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()), Qt::UniqueConnection);
          else
          return QObject::disconnect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()));



          Hopefully, this should be enough to prevent QTextEdit from interfering with you.






          share|improve this answer
































            0














            I'm using qt for python but I Think the underground is same.



            1. you decide the page size valiable.(A4 = width=797,height=1023)

            2. you insert Text.

            3. if you insert Text,the textcursor is the bottom line on the document.

            4. you get the rect.

            5. you devide the rect by the pagesize.

            6. If the text length 20lines are equal to 1 page, the line of 20 is the last of one page.probably,this page bottom height is 1023 if you set the height 1023. current_bottomline + cursor.height > 1023*pageheight = current_page last_bottomline + cursor.height>1023*lastpageheiht = max_page

            7. you resize the textedit.(797,1023*page)

            8. If you delete the texts by backspace key or delete key,you must recalculate the last botom line.(I recommend that this check is done in the KeyEvent.)QTextCursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor,1)


            9. And,you get the cursorRect()


            10. you divide the height by A4 Size unit-height.

            11. if the height 102300, the page will be 100.

            12. to set pagesize to 100.

            13. you resize textedit resize(797,1023*page)

            14. you can get textedit.verticalScrollBar()

            15. you set the verticalScrollBar() setMaximum(the last page height)

            16. You set SliderPosition by setSliderPosition(the same value or a Little difference of current textcursor y position)

            I couldn't know if the scrollBar was taken from textedit.verticalScrollBar() pointer from your question.This is a Little verbose but this can be a way of solution.






            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51500059%2fstop-qtextcursorinserttext-from-modifying-qtextdocument-scrollbar-range%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Yes. You'd depend on the implementation detail. In QTextEditPrivate::init(), the following connection is made:



              Q_Q(QTextEdit);
              control = new QTextEditControl(q);
              ...
              QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars()))


              Here, q is of the type QTextEdit* and is the Q-pointer to the API object. Thus, you'd need to disconnect this connection, and manage the scroll bars on your own:



              bool isBaseOf(const QByteArray &className, const QMetaObject *mo) 
              while (mo)
              if (mo->className() == className)
              return true;
              mo = mo->superClass();

              return false;


              bool setScrollbarAdjustmentsEnabled(QTextEdit *ed, bool enable)
              QObject *control = ;
              for (auto *ctl : ed->children())
              if (isBaseOf("QWidgetTextControl", ctl->metaObject())
              Q_ASSERT(!control);
              control = ctl;


              if (!control)
              return false;
              if (enable)
              return QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()), Qt::UniqueConnection);
              else
              return QObject::disconnect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()));



              Hopefully, this should be enough to prevent QTextEdit from interfering with you.






              share|improve this answer





























                0














                Yes. You'd depend on the implementation detail. In QTextEditPrivate::init(), the following connection is made:



                Q_Q(QTextEdit);
                control = new QTextEditControl(q);
                ...
                QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars()))


                Here, q is of the type QTextEdit* and is the Q-pointer to the API object. Thus, you'd need to disconnect this connection, and manage the scroll bars on your own:



                bool isBaseOf(const QByteArray &className, const QMetaObject *mo) 
                while (mo)
                if (mo->className() == className)
                return true;
                mo = mo->superClass();

                return false;


                bool setScrollbarAdjustmentsEnabled(QTextEdit *ed, bool enable)
                QObject *control = ;
                for (auto *ctl : ed->children())
                if (isBaseOf("QWidgetTextControl", ctl->metaObject())
                Q_ASSERT(!control);
                control = ctl;


                if (!control)
                return false;
                if (enable)
                return QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()), Qt::UniqueConnection);
                else
                return QObject::disconnect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()));



                Hopefully, this should be enough to prevent QTextEdit from interfering with you.






                share|improve this answer



























                  0












                  0








                  0







                  Yes. You'd depend on the implementation detail. In QTextEditPrivate::init(), the following connection is made:



                  Q_Q(QTextEdit);
                  control = new QTextEditControl(q);
                  ...
                  QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars()))


                  Here, q is of the type QTextEdit* and is the Q-pointer to the API object. Thus, you'd need to disconnect this connection, and manage the scroll bars on your own:



                  bool isBaseOf(const QByteArray &className, const QMetaObject *mo) 
                  while (mo)
                  if (mo->className() == className)
                  return true;
                  mo = mo->superClass();

                  return false;


                  bool setScrollbarAdjustmentsEnabled(QTextEdit *ed, bool enable)
                  QObject *control = ;
                  for (auto *ctl : ed->children())
                  if (isBaseOf("QWidgetTextControl", ctl->metaObject())
                  Q_ASSERT(!control);
                  control = ctl;


                  if (!control)
                  return false;
                  if (enable)
                  return QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()), Qt::UniqueConnection);
                  else
                  return QObject::disconnect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()));



                  Hopefully, this should be enough to prevent QTextEdit from interfering with you.






                  share|improve this answer















                  Yes. You'd depend on the implementation detail. In QTextEditPrivate::init(), the following connection is made:



                  Q_Q(QTextEdit);
                  control = new QTextEditControl(q);
                  ...
                  QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars()))


                  Here, q is of the type QTextEdit* and is the Q-pointer to the API object. Thus, you'd need to disconnect this connection, and manage the scroll bars on your own:



                  bool isBaseOf(const QByteArray &className, const QMetaObject *mo) 
                  while (mo)
                  if (mo->className() == className)
                  return true;
                  mo = mo->superClass();

                  return false;


                  bool setScrollbarAdjustmentsEnabled(QTextEdit *ed, bool enable)
                  QObject *control = ;
                  for (auto *ctl : ed->children())
                  if (isBaseOf("QWidgetTextControl", ctl->metaObject())
                  Q_ASSERT(!control);
                  control = ctl;


                  if (!control)
                  return false;
                  if (enable)
                  return QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()), Qt::UniqueConnection);
                  else
                  return QObject::disconnect(control, SIGNAL(documentSizeChanged(QSizeF)), ed, SLOT(_q_adjustScrollbars()));



                  Hopefully, this should be enough to prevent QTextEdit from interfering with you.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 24 '18 at 16:05

























                  answered Jul 24 '18 at 15:56









                  Kuba OberKuba Ober

                  70.3k982191




                  70.3k982191























                      0














                      I'm using qt for python but I Think the underground is same.



                      1. you decide the page size valiable.(A4 = width=797,height=1023)

                      2. you insert Text.

                      3. if you insert Text,the textcursor is the bottom line on the document.

                      4. you get the rect.

                      5. you devide the rect by the pagesize.

                      6. If the text length 20lines are equal to 1 page, the line of 20 is the last of one page.probably,this page bottom height is 1023 if you set the height 1023. current_bottomline + cursor.height > 1023*pageheight = current_page last_bottomline + cursor.height>1023*lastpageheiht = max_page

                      7. you resize the textedit.(797,1023*page)

                      8. If you delete the texts by backspace key or delete key,you must recalculate the last botom line.(I recommend that this check is done in the KeyEvent.)QTextCursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor,1)


                      9. And,you get the cursorRect()


                      10. you divide the height by A4 Size unit-height.

                      11. if the height 102300, the page will be 100.

                      12. to set pagesize to 100.

                      13. you resize textedit resize(797,1023*page)

                      14. you can get textedit.verticalScrollBar()

                      15. you set the verticalScrollBar() setMaximum(the last page height)

                      16. You set SliderPosition by setSliderPosition(the same value or a Little difference of current textcursor y position)

                      I couldn't know if the scrollBar was taken from textedit.verticalScrollBar() pointer from your question.This is a Little verbose but this can be a way of solution.






                      share|improve this answer



























                        0














                        I'm using qt for python but I Think the underground is same.



                        1. you decide the page size valiable.(A4 = width=797,height=1023)

                        2. you insert Text.

                        3. if you insert Text,the textcursor is the bottom line on the document.

                        4. you get the rect.

                        5. you devide the rect by the pagesize.

                        6. If the text length 20lines are equal to 1 page, the line of 20 is the last of one page.probably,this page bottom height is 1023 if you set the height 1023. current_bottomline + cursor.height > 1023*pageheight = current_page last_bottomline + cursor.height>1023*lastpageheiht = max_page

                        7. you resize the textedit.(797,1023*page)

                        8. If you delete the texts by backspace key or delete key,you must recalculate the last botom line.(I recommend that this check is done in the KeyEvent.)QTextCursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor,1)


                        9. And,you get the cursorRect()


                        10. you divide the height by A4 Size unit-height.

                        11. if the height 102300, the page will be 100.

                        12. to set pagesize to 100.

                        13. you resize textedit resize(797,1023*page)

                        14. you can get textedit.verticalScrollBar()

                        15. you set the verticalScrollBar() setMaximum(the last page height)

                        16. You set SliderPosition by setSliderPosition(the same value or a Little difference of current textcursor y position)

                        I couldn't know if the scrollBar was taken from textedit.verticalScrollBar() pointer from your question.This is a Little verbose but this can be a way of solution.






                        share|improve this answer

























                          0












                          0








                          0







                          I'm using qt for python but I Think the underground is same.



                          1. you decide the page size valiable.(A4 = width=797,height=1023)

                          2. you insert Text.

                          3. if you insert Text,the textcursor is the bottom line on the document.

                          4. you get the rect.

                          5. you devide the rect by the pagesize.

                          6. If the text length 20lines are equal to 1 page, the line of 20 is the last of one page.probably,this page bottom height is 1023 if you set the height 1023. current_bottomline + cursor.height > 1023*pageheight = current_page last_bottomline + cursor.height>1023*lastpageheiht = max_page

                          7. you resize the textedit.(797,1023*page)

                          8. If you delete the texts by backspace key or delete key,you must recalculate the last botom line.(I recommend that this check is done in the KeyEvent.)QTextCursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor,1)


                          9. And,you get the cursorRect()


                          10. you divide the height by A4 Size unit-height.

                          11. if the height 102300, the page will be 100.

                          12. to set pagesize to 100.

                          13. you resize textedit resize(797,1023*page)

                          14. you can get textedit.verticalScrollBar()

                          15. you set the verticalScrollBar() setMaximum(the last page height)

                          16. You set SliderPosition by setSliderPosition(the same value or a Little difference of current textcursor y position)

                          I couldn't know if the scrollBar was taken from textedit.verticalScrollBar() pointer from your question.This is a Little verbose but this can be a way of solution.






                          share|improve this answer













                          I'm using qt for python but I Think the underground is same.



                          1. you decide the page size valiable.(A4 = width=797,height=1023)

                          2. you insert Text.

                          3. if you insert Text,the textcursor is the bottom line on the document.

                          4. you get the rect.

                          5. you devide the rect by the pagesize.

                          6. If the text length 20lines are equal to 1 page, the line of 20 is the last of one page.probably,this page bottom height is 1023 if you set the height 1023. current_bottomline + cursor.height > 1023*pageheight = current_page last_bottomline + cursor.height>1023*lastpageheiht = max_page

                          7. you resize the textedit.(797,1023*page)

                          8. If you delete the texts by backspace key or delete key,you must recalculate the last botom line.(I recommend that this check is done in the KeyEvent.)QTextCursor.movePosition(QtGui.QTextCursor.End,QtGui.QTextCursor.MoveAnchor,1)


                          9. And,you get the cursorRect()


                          10. you divide the height by A4 Size unit-height.

                          11. if the height 102300, the page will be 100.

                          12. to set pagesize to 100.

                          13. you resize textedit resize(797,1023*page)

                          14. you can get textedit.verticalScrollBar()

                          15. you set the verticalScrollBar() setMaximum(the last page height)

                          16. You set SliderPosition by setSliderPosition(the same value or a Little difference of current textcursor y position)

                          I couldn't know if the scrollBar was taken from textedit.verticalScrollBar() pointer from your question.This is a Little verbose but this can be a way of solution.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 14 '18 at 1:00









                          user9402680user9402680

                          342210




                          342210



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51500059%2fstop-qtextcursorinserttext-from-modifying-qtextdocument-scrollbar-range%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Darth Vader #20

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Ondo