Ok, I see the problem. QWidget's geometry is calculated upon first show event. You should move the ensureCursorVisible() call in QTextEdit's showEvent() or into a slot connected to a single shot timer with timeout 0. This ensures that the geometry is constructed properly before trying to do operations which rely on widget's geometry.
Either subclass QTextEdit and override it's showEvent():
{
static bool initialized = false;
if (!initialized)
{
ensureCursorVisible();
initialized = true;
}
}
void MyTextEdit::showEvent(QShowEvent* event)
{
QTextEdit::showEvent(event);
static bool initialized = false;
if (!initialized)
{
ensureCursorVisible();
initialized = true;
}
}
To copy to clipboard, switch view to plain text mode
Or use the timer technique:
// add this in place where you had "editor->ensureCursorVisible();"
QTimer::singleShot(0,
this,
SLOT(initialize
()));
// declared as a slot
void BodyWidget::initialize()
{
// QTextEdit* editor moved as a member variable
editor->ensureCursorVisible();
}
editor->ensureCursorVisible();
// add this in place where you had "editor->ensureCursorVisible();"
QTimer::singleShot(0, this, SLOT(initialize()));
// declared as a slot
void BodyWidget::initialize()
{
// QTextEdit* editor moved as a member variable
editor->ensureCursorVisible();
}
editor->ensureCursorVisible();
To copy to clipboard, switch view to plain text mode
Bookmarks