How to reposition cursor to the end of the document without autoscroll
-
@lukutis222 Print out what event->type() returns in your eventFilter and see what you get when you press left mouse button. For left mouse button click you should get two events: QEvent::MouseButtonPress and QEvent::MouseButtonRelease.
-
@lukutis222
I don't know why you are not getting the left mouse button, especially as you say you do for the right button. Like @jsulm I would expect a mouse press and a mouse release. Start by removing yourobject == ui->Console_read &&
, let's see whether you get these anywhere regardless of object? Also do as @jsulm says: log every event arriving in youreventFilter()
, see if there is anything else there of interest. -
@jsulm said in How to reposition cursor to the end of the document without autoscroll:
Print out what event->type(
When I hover my mouse over console, events 10 and 110 are generated
When cursor leaves the console area, event 11 is generated
When I left click on my console, event = 207 is generated.
When I click left mouse button multiple times(double click), multiple 207 events are generated:
event->type() = 207 event->type() = 207 event->type() = 207 event->type() = 207
Right mouse button click generates 4 events:
event->type() = 2 mouse clicked event->type() = 9 event->type() = 12 event->type() = 11
bool Widget::eventFilter(QObject *object, QEvent *event) { qDebug("event->type() = %u \n",event->type()); if ((object == ui->Console_read) && (event->type() == QEvent::MouseButtonPress) ) { qDebug("mouse clicked \n"); } if ( object == ui->Console_read && ( event->type() == QEvent::MouseButtonDblClick ) ) { qDebug("mouse double click \n"); } return false; }
Additionally, I tried to remove object == ui->Console_read but does not have any different affect
-
@lukutis222
If you wait a few minutes I will knock up a tiny sample (I am Linux and Qt5,15, you never seem to have said what you are) with aQTextEdit
and see whether I get the left mouse button press/release.... Come back and look in a while :) -
@lukutis222
Please note that tells you your Creator was built with Qt 6.3.1, but not what version of Qt you downloaded and use for you application. Anyway at a guess it's some Qt 6. Whatever, my test will be Linux + Qt 5.15, you will have to see how that behaves for you. -
@JonB
Based on my directory folders I assume the project is also 6_3_1:
Anyways, in my application, apart from the Console I have a write box where user can write commands to the serial device:
Just for testing purpose, I have created eventfilter2 for QLineEdit events and I can see that it behaves differently than QTextEdit:
When I left click mouse button on the small rectangular text box at the bottom of my application, many events are generated(Just from one left mouse button click):event->type() = 213 event->type() = 71 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 110 event->type() = 207 event->type() = 8 event->type() = 207 event->type() = 207 event->type() = 207 event->type() = 2 mouse clicked event->type() = 207 event->type() = 207 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 170 event->type() = 3 event->type() = 207 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 12 event->type() = 170 event->type() = 170 event->type() = 12 event->type() = 170 event->type() = 170
As you can see from above, one of the events were triggered mouse clicked so that seem to work (Im not sure why it is creating so many 12 170 170 events over and over again )
-
@lukutis222
OK, it gets complicated. Full story probably in https://stackoverflow.com/questions/41631011/my-qt-eventfilter-doesnt-stop-events-as-it-should. I agreeQTextEdit
behaves differently fromQLineEdit
. I said you may needLook at installing an application event filter
and you do.
Here is my sample application:
#include <QCoreApplication> #include <QDebug> #include <QLineEdit> #include <QTextEdit> #include <QVBoxLayout> #include "widget.h" Widget::Widget(QWidget *parent) : QWidget(parent) { QVBoxLayout *vlayout = new QVBoxLayout; QLineEdit *lineEdit = new QLineEdit; lineEdit->setObjectName("lineEdit"); vlayout->addWidget(lineEdit); QTextEdit *textEdit = new QTextEdit; textEdit->setObjectName("textEdit"); vlayout->addWidget(textEdit); setLayout(vlayout); QCoreApplication::instance()->installEventFilter(this); lineEdit->installEventFilter(this); textEdit->installEventFilter(this); } Widget::~Widget() { } bool Widget::eventFilter(QObject *obj, QEvent *event) { // qDebug() << event->type(); if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) qDebug() << "Mouse button event" << obj->objectName() << event->type(); // pass the event on to the parent class return QWidget::eventFilter(obj, event); }
Note how I use
QCoreApplication::instance()->installEventFilter(this);
.Output (from left-clicking in each of the line edit and text edit) is:
11:17:36: Debugging starts Mouse button event "WidgetClassWindow" QEvent::MouseButtonPress Mouse button event "lineEdit" QEvent::MouseButtonPress Mouse button event "lineEdit" QEvent::MouseButtonPress Mouse button event "WidgetClassWindow" QEvent::MouseButtonRelease Mouse button event "lineEdit" QEvent::MouseButtonRelease Mouse button event "lineEdit" QEvent::MouseButtonRelease Mouse button event "WidgetClassWindow" QEvent::MouseButtonPress Mouse button event "qt_scrollarea_viewport" QEvent::MouseButtonPress Mouse button event "WidgetClassWindow" QEvent::MouseButtonRelease Mouse button event "qt_scrollarea_viewport" QEvent::MouseButtonRelease 11:17:41: Debugging has finished
See how for the
QTextEdit
the mouse events actually go toqt_scrollarea_viewport
.I leave you to play with this/adapt to your needs.
-
@lukutis222
"Yes I figured that but its not fully clear how to do it since I do not have a seperate class for QTextEdit. Can I do that in my Widget component?"slot function:
QObject* objTE1 = QObject::sender(); if (objTE1 == textEdit) { // you can catch the click or press event here. you should declare textEdit public to access. <do action on mouse press or click event here> }
you can use to catch the click or mouse press event here by connecting the signal (click or press) of the textEdit obj to any defined slot above. this way you can always catch the specific child Widget
-
@dan1973 I am now able to check in mouse click event the position of the cursor but I am not sure how is it indended to help me:
bool Widget::eventFilter(QObject *object, QEvent *event) { if ( event->type() == QEvent::MouseButtonPress) { qDebug("mouse click event \n"); QString str1 = "\n X:"+QString::number(QCursor::pos().x())+ "Y:" +QString::number(QCursor::pos().x()); qDebug("str1 = %s\n",str1.toStdString().c_str()); } if ( object == ui->Console_read && ( event->type() == QEvent::MouseMove ) ) { qDebug("mouse move event \n"); QString str1 = "\n X:"+QString::number(QCursor::pos().x())+ "Y:" +QString::number(QCursor::pos().x()); qDebug("str1 = %s\n",str1.toStdString().c_str()); } return false; }
-
The main issue I have right now is when autoscroll is disabled, the cursor moves up by 1 column everytime the new data is received (of course that makes sense as the new data is received, the column number shifts up by one). However, I am yet to discover how to avoid this.
When I click somewhere in the middle. I would expect my cursor to stay there until I re-enable autoscroll so I can easily read logs without them moving up( which makes it complicated to read logs).
I think my problem is:
qDebug("refresh cursor to the old position \n"); ui->Console_read->setTextCursor(cursor_backup); int column_number3 = ui->Console_read->textCursor().columnNumber(); qDebug("column number after append = %u \n",column_number3); refresh_scrollbar = 0;
Instead of setting cursor to cursor_backup, I must set it to cursor_backup + offset of how many lines has the cursor moved after append in order for the cursor to maintain the position and not shift up
-
@lukutis222
Absolutely no promises, but I might go have a play with this to see if I can spot anything. Some clarifications please:- In your last post you keep talking about it moving by 1 column. You do mean 1 row, don't you? EDIT: Oh, it seems you do mean column, why are columns rather than rows involved here? Aren't you taking about appending lines/rows to the end of the
QTextEdit
and vertical scrolling, why are columns/horizontal scrolling at issue? - You have a
QTextEdit
. Your situation arises once its visual rows are filled so that it needs to scroll to display the last line, right? - "Autoscroll" is just your own implemented behaviour, right? When autoscroll is enabled you are happy that it visually jumps to the end when new data is appended?
- What you want is essentially: with autoscroll disabled, user is looking at some line in the visible area. New data/line appears behind the scenes and is appended to the
QTextEdit
. Even if you save and restore cursor position or scroll position this causes a visual move of the preceding visible lines so that the line the user is looking it moves, and that is undesirable behaviour? - You would like a solution which allows data to be appended to the
QTextEdit
but leaves the visual area exactly as-was, so no move/scroll by one line or whatever?
It would be great if you simply answered "Yes to all of these"! :)
UPDATE: OK, I'm sorry but now having read through the length of this topic and all the code I am not offering to go start setting this up and investigate! But I am perplexed by your constant use of "columns" and
QTextCursor::columnNumber()
when you talking about appending to aQTextEdit
which will cause its rows/lines to increase and vertical scrolling to occur, which is what you want to prevent? But you don't have to explain if you don't want to, I haven't investigatedQTextEdit
in detail....I Googled
qtextedit maintain scroll position
, which I think is what you are trying to achieve? For vertical scrolling issue, what about say https://stackoverflow.com/a/9832270/489865 or https://stackoverflow.com/a/57975092/489865 or https://www.pythonguis.com/faq/how-to-get-set-the-position-of-the-scroll-area/, etc.? Or have you already implemented this area? - In your last post you keep talking about it moving by 1 column. You do mean 1 row, don't you? EDIT: Oh, it seems you do mean column, why are columns rather than rows involved here? Aren't you taking about appending lines/rows to the end of the
-
-
I talk about columns because I want to maintain vertical position of the cursor. I.E when new data is appended, I want the cursor to stay in the exact same vertical position and not be shifted up. Isn't that what column is used for? Perhaps my understanding about columns and rows is wrong here.
-
Again you mention rows and I am thinking about columns in my head. Every new ROW of data that is appended, adds one more column to my console.
-
Yes. Autoscroll is my own enabled feature and the logic is very simple:
if(ui->autoscroll->isChecked()){ QTextCursor cursor = ui->Console_read->textCursor(); cursor.clearSelection(); cursor.movePosition(QTextCursor::End); ui->Console_read->setTextCursor(cursor); }
I am totally happy about how my autoscroll works. I am mainly concerned when the "autoscroll" is disabled
-
Even if you save and restore cursor position or scroll position this causes a visual move of the preceding visible lines so that the line the user is looking it moves, and that is undesirable behaviour?
Yes exactly. When user disabled the autoscroll and clicks with mouse on a specific row of data, I want the QTextEdit window to "freeze" (while appending the new data at the end of course) and allow user to "inspect the data" without the console constantly moving the data upwards which makes it difficult for user to focus on that row and read it properly. -
Exactly
I am now looking into the link that you have posted (stackoverflow) about vertical scrolling
-
-
@lukutis222 said in How to reposition cursor to the end of the document without autoscroll:
Every new ROW of data that is appended, adds one more column to my console.
Umm, I just don't get this?! How does a row appended to the bottom of a
QTextEdit
have anything to do with "adding one more column"? "console constantly moving the data upwards" is to do with new rows/lines, not columns? "columns" is to do with moving cursor right/left, not up/down. -
@JonB Hm yes you are right. Now that I think about it more I cant think of any logic behind this.. I think I just confused myself with this.. But I only use
int column_number = ui->Console_read->textCursor().columnNumber();
to detect if the cursor is anywhere else other than the END and that part seems to work.
EDIT
I understand now, if I click at the very beginning of a row it will still not detect but that is another issue.EDIT
I am getting some results in the right direction (https://stackoverflow.com/questions/14550146/qtextedit-scroll-down-automatically-only-if-the-scrollbar-is-at-the-bottom/57975092#57975092)
void Widget::readData() { while(serial_local->is_data_available()){ //if autoscroll is checked, just move the cursor to the end of the file which will automatically cause the autoscroll to happen if(ui->autoscroll->isChecked()){ QTextCursor cursor = ui->Console_read->textCursor(); cursor.clearSelection(); cursor.movePosition(QTextCursor::End); ui->Console_read->setTextCursor(cursor); } QScrollBar *scrollbar = ui->Console_read->verticalScrollBar(); bool scrollbarAtBottom = (scrollbar->value() >= (scrollbar->maximum() - 4)); int scrollbarPrevValue = scrollbar->value(); ui->Console_read->moveCursor(QTextCursor::End); QByteArray line = serial_local->read_data(); QString DataAsString = QString(line); ui->Console_read->insertPlainText(DataAsString); //reposition the cursor once the write is complete if(!ui->autoscroll->isChecked()) { if (scrollbarAtBottom) { ui->Console_read->ensureCursorVisible(); } else { ui->Console_read->verticalScrollBar()->setValue(scrollbarPrevValue); } } } }
When I put my cursor in the middle if QTextEdit, the data will be appended at the end but the QTextEdit will freeze in place and will not move which is exactly what I want!
The only issue is:
One of the two will happen:
1.When I try to highlight some data(Assume I want to highlight and copy few rows of data), when the new data is appended (cursor is moved to the end of QTextEdit to append the data), all the data from the position I clicked my mouse till the end of the document will be automatically be higlighted
2.
When I try to highlight some row, my action will be canceled as the new data is appended due to the cursor moving to the end again.