Stop QTextCursor::insertText() from modifying QTextDocument scrollbar range
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
add a comment |
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
add a comment |
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
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
c++ qt qtextedit qtextdocument qtextcursor
asked Jul 24 '18 at 13:36
George V.George V.
61
61
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
I'm using qt for python but I Think the underground is same.
- you decide the page size valiable.(A4 = width=797,height=1023)
- you insert Text.
- if you insert Text,the textcursor is the bottom line on the document.
- you get the rect.
- you devide the rect by the pagesize.
- 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
- you resize the textedit.(797,1023*page)
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)
And,you get the cursorRect()
- you divide the height by A4 Size unit-height.
- if the height 102300, the page will be 100.
- to set pagesize to 100.
- you resize textedit resize(797,1023*page)
- you can get
textedit.verticalScrollBar()
- you set the verticalScrollBar() setMaximum(the last page height)
- 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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
edited Jul 24 '18 at 16:05
answered Jul 24 '18 at 15:56
Kuba OberKuba Ober
70.3k982191
70.3k982191
add a comment |
add a comment |
I'm using qt for python but I Think the underground is same.
- you decide the page size valiable.(A4 = width=797,height=1023)
- you insert Text.
- if you insert Text,the textcursor is the bottom line on the document.
- you get the rect.
- you devide the rect by the pagesize.
- 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
- you resize the textedit.(797,1023*page)
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)
And,you get the cursorRect()
- you divide the height by A4 Size unit-height.
- if the height 102300, the page will be 100.
- to set pagesize to 100.
- you resize textedit resize(797,1023*page)
- you can get
textedit.verticalScrollBar()
- you set the verticalScrollBar() setMaximum(the last page height)
- 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.
add a comment |
I'm using qt for python but I Think the underground is same.
- you decide the page size valiable.(A4 = width=797,height=1023)
- you insert Text.
- if you insert Text,the textcursor is the bottom line on the document.
- you get the rect.
- you devide the rect by the pagesize.
- 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
- you resize the textedit.(797,1023*page)
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)
And,you get the cursorRect()
- you divide the height by A4 Size unit-height.
- if the height 102300, the page will be 100.
- to set pagesize to 100.
- you resize textedit resize(797,1023*page)
- you can get
textedit.verticalScrollBar()
- you set the verticalScrollBar() setMaximum(the last page height)
- 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.
add a comment |
I'm using qt for python but I Think the underground is same.
- you decide the page size valiable.(A4 = width=797,height=1023)
- you insert Text.
- if you insert Text,the textcursor is the bottom line on the document.
- you get the rect.
- you devide the rect by the pagesize.
- 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
- you resize the textedit.(797,1023*page)
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)
And,you get the cursorRect()
- you divide the height by A4 Size unit-height.
- if the height 102300, the page will be 100.
- to set pagesize to 100.
- you resize textedit resize(797,1023*page)
- you can get
textedit.verticalScrollBar()
- you set the verticalScrollBar() setMaximum(the last page height)
- 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.
I'm using qt for python but I Think the underground is same.
- you decide the page size valiable.(A4 = width=797,height=1023)
- you insert Text.
- if you insert Text,the textcursor is the bottom line on the document.
- you get the rect.
- you devide the rect by the pagesize.
- 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
- you resize the textedit.(797,1023*page)
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)
And,you get the cursorRect()
- you divide the height by A4 Size unit-height.
- if the height 102300, the page will be 100.
- to set pagesize to 100.
- you resize textedit resize(797,1023*page)
- you can get
textedit.verticalScrollBar()
- you set the verticalScrollBar() setMaximum(the last page height)
- 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.
answered Nov 14 '18 at 1:00
user9402680user9402680
342210
342210
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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