Important: Please read the Qt Code of Conduct -

Input in QTextEdit cause vertical scroll bar jumping

  • I have developed a small GUI program based on QT 5.4, there
    is a QTextEdit widget. The widget can paste and display
    pictures. When the displayed picture high than widget's
    height(vertical scroll bar appears), input text(and
    picture is in the same row of the cursor) will cause the
    thumb of vertical scroll bar jumping up and down, the
    experience very unfriendly! how to do?

    input 1

    input 2, now scroll bar jump up

    input 2 again, now scroll bar jump down

    keeps input, it just keeps jumping up and down, OMG!

  • Lifetime Qt Champion


    Just to be sure I understand you correctly, when you paste an image that is higher than the widget height and then start to type some text, the scroll bar jumps down to where the text should be ? And then if you paste a new image there, it stays at the top of the newly inserted image ?

  • After paste an image on a clean QTextEdit, it displays scroll bar and jump down to maximized value,
    so we can see the bottom part of image. Then type in one character, the scroll bar jump up to 0 value, so we can see the top part of image. Because the text is align to bottom of image, so the character just typed in is out of display area of widget.

  • Lifetime Qt Champion

    Can you share a minimal compilable example that produce this behavior ?

  • mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    namespace Ui {
    class MainWindow;
    class MainWindow : public QMainWindow
        explicit MainWindow(QWidget *parent = 0);
    private slots:
        void on_actionInsert1_triggered(bool);
        Ui::MainWindow *ui;
    #endif // MAINWINDOW_H


    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTextDocumentFragment>
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
        delete ui;
    void MainWindow::on_actionInsert1_triggered(bool checked)
        QTextDocumentFragment fragment;
        fragment = QTextDocumentFragment::fromHtml(
                    QString("<img src='d:/ddd.png'>"));


    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
      <property name="windowTitle">
      <widget class="QWidget" name="centralWidget">
       <layout class="QHBoxLayout" name="horizontalLayout">
         <widget class="QTextEdit" name="textEdit"/>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
       <widget class="QMenu" name="menuTest">
        <property name="title">
        <addaction name="actionInsert1"/>
       <addaction name="menuTest"/>
      <action name="actionInsert1">
       <property name="text">
        <string>insert 1</string>
      <action name="actionInsert2">
       <property name="text">
        <string>insert 2</string>
     <layoutdefault spacing="6" margin="11"/>

    Before run this example, please prepare a picture on your disk, and make sure that the height of picture more than 120 pixel, the width of picture less than 600 pixel. The picture location is hard code in MainWindow::on_actionInsert1_triggered method, please change it to your picture location and recompile.

    Run this example, first click menu item 'insert 1' to insert picture into QTextEdit widget, after pictrue displayed, you will found that the vertical scroll bar now is available. Then type in slowly one bye one, notice the scroll bar, it will jump up/down with typing.

  • Lifetime Qt Champion

    Looks like it might be a limitation/bug. The behavior is the same with 5.6

    You should go to the bug report system to see if it's something known. If not please consider opening a new report providing a minimal compilable example (not just the code files) to reproduce the behavior.

Log in to reply