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 2, now scroll bar jump up
input 2 again, now scroll bar jump down
keeps input, it just keeps jumping up and down, OMG!
-
Hi,
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. -
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 { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_actionInsert1_triggered(bool); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QTextDocumentFragment> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_actionInsert1_triggered(bool checked) { QTextDocumentFragment fragment; fragment = QTextDocumentFragment::fromHtml( QString("<img src='d:/ddd.png'>")); ui->textEdit->textCursor().insertFragment(fragment); }
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>120</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralWidget"> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QTextEdit" name="textEdit"/> </item> </layout> </widget> <widget class="QMenuBar" name="menuBar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>23</height> </rect> </property> <widget class="QMenu" name="menuTest"> <property name="title"> <string>test</string> </property> <addaction name="actionInsert1"/> </widget> <addaction name="menuTest"/> </widget> <action name="actionInsert1"> <property name="text"> <string>insert 1</string> </property> </action> <action name="actionInsert2"> <property name="text"> <string>insert 2</string> </property> </action> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui>
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.
-
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.