make QPlainTextEdit ignore scrolling
-
I can't find a solution for this but is there a way to make a QPlainTextEdit ignore all scrolling action like mouse wheel, space bar, clicking on text and dragging down and so on?
-
I can't find a solution for this but is there a way to make a QPlainTextEdit ignore all scrolling action like mouse wheel, space bar, clicking on text and dragging down and so on?
Hi,
You can install an event filter on it and filter out all the unwanted operations. You might have to do that on the widget's scrollbars as well.
-
@SGaist Hello I installed an event filter but im not sure how to ignore it or what the event i want to ignore is called i tried a few but none seemed to get recognized
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->plainTextDisplay) {
//something else
}
}
if (obj == ui->plainTextLines){
if(event->type() == QEvent::Scroll){
qDebug() << "hi";
event->ignore();
}
}
return QMainWindow::eventFilter(obj, event);
}
Just in case i did something wrong while installing the event filter
ui->plainTextDisplay->setFocus();
ui->plainTextDisplay->installEventFilter(this);
ui->plainTextLines->setFocus();
ui->plainTextLines->installEventFilter(this);
mainwindow.h:
protected:
bool eventFilter(QObject *obj, QEvent *ev) override; -
@SGaist Hello I installed an event filter but im not sure how to ignore it or what the event i want to ignore is called i tried a few but none seemed to get recognized
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->plainTextDisplay) {
//something else
}
}
if (obj == ui->plainTextLines){
if(event->type() == QEvent::Scroll){
qDebug() << "hi";
event->ignore();
}
}
return QMainWindow::eventFilter(obj, event);
}
Just in case i did something wrong while installing the event filter
ui->plainTextDisplay->setFocus();
ui->plainTextDisplay->installEventFilter(this);
ui->plainTextLines->setFocus();
ui->plainTextLines->installEventFilter(this);
mainwindow.h:
protected:
bool eventFilter(QObject *obj, QEvent *ev) override;@Grand_Titan said in make QPlainTextEdit ignore scrolling:
if (obj == ui->plainTextLines){ if(event->type() == QEvent::Scroll){ qDebug() << "hi"; event->ignore(); }You need to return true to filter the event.
-
@mpergand Thanks. But still I can't figure out which event is the one that controlls scrolling and those things.
if(event->type() == QEvent::Scroll || event->type() == QEvent::ScrollPrepare){
qDebug() << "hi";
event->ignore();
return true;
} -
if (obj == ui->plainTextLines){ if(event->type() == QEvent::Wheel){ qDebug() << "hi"; return true; }else{ return false; } }I tried it with the Wheel event but that event only gets called if i scroll when the text cannot scroll any further. I need to prevent scrolling always..
-
if (obj == ui->plainTextLines){ if(event->type() == QEvent::Wheel){ qDebug() << "hi"; return true; }else{ return false; } }I tried it with the Wheel event but that event only gets called if i scroll when the text cannot scroll any further. I need to prevent scrolling always..
Try this:
class Text : public QPlainTextEdit { public: Text() : QPlainTextEdit(nullptr) { appendPlainText("A\nB\nC\nD\nE\nF\nG\nH\nI\nJ"); installEventFilter(this); setTextInteractionFlags(Qt::NoTextInteraction); } bool eventFilter(QObject *o, QEvent *e) { qDebug()<<o<<e->type(); if( (e->type()==QEvent::Wheel) || (e->type()==QEvent::MouseMove) || (e->type()==QEvent::MouseButtonPress) || (e->type()==QEvent::MouseButtonDblClick) ) return true; return QPlainTextEdit::eventFilter(o,e); } }; -
Try this:
class Text : public QPlainTextEdit { public: Text() : QPlainTextEdit(nullptr) { appendPlainText("A\nB\nC\nD\nE\nF\nG\nH\nI\nJ"); installEventFilter(this); setTextInteractionFlags(Qt::NoTextInteraction); } bool eventFilter(QObject *o, QEvent *e) { qDebug()<<o<<e->type(); if( (e->type()==QEvent::Wheel) || (e->type()==QEvent::MouseMove) || (e->type()==QEvent::MouseButtonPress) || (e->type()==QEvent::MouseButtonDblClick) ) return true; return QPlainTextEdit::eventFilter(o,e); } };@mpergand I cant filter mouseButtonPress and dbclick cuz i still use this elsewhere. but every time i include this in my code: "==QEvent::MouseMove" it crashes on run. Same goes for noTextInteraction i still need to be able to interact with the text that way i just want to prevent scrolling.
-
@mpergand I cant filter mouseButtonPress and dbclick cuz i still use this elsewhere. but every time i include this in my code: "==QEvent::MouseMove" it crashes on run. Same goes for noTextInteraction i still need to be able to interact with the text that way i just want to prevent scrolling.
@Grand_Titan try to catch viewport events and block scrolling.
-
I forgot to close this since I already fixed the issue even though it was a bit of a dirty fix. I just did:
"connect(ui->plainTextLines->verticalScrollBar(), &QScrollBar::valueChanged, this, &MainWindow::handleLinesScroll);"
and in the handleLinesScroll I just set the scroll bar to the value it was before. But it works flawlessly and I see no performance impact. -
G Grand_Titan has marked this topic as solved on