Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. make QPlainTextEdit ignore scrolling
Forum Updated to NodeBB v4.3 + New Features

make QPlainTextEdit ignore scrolling

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Grand_Titan
    wrote on last edited by
    #1

    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?

    SGaistS 1 Reply Last reply
    0
    • G Grand_Titan

      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?

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Grand_Titan
        wrote on last edited by
        #3

        @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;

        M 1 Reply Last reply
        0
        • G Grand_Titan

          @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;

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @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.

          1 Reply Last reply
          1
          • G Offline
            G Offline
            Grand_Titan
            wrote on last edited by
            #5

            @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;
            }

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Grand_Titan
              wrote on last edited by Grand_Titan
              #6
              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..

              M 1 Reply Last reply
              0
              • G Grand_Titan
                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..

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                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);
                	}
                };
                
                G 1 Reply Last reply
                0
                • M mpergand

                  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);
                  	}
                  };
                  
                  G Offline
                  G Offline
                  Grand_Titan
                  wrote on last edited by
                  #8

                  @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.

                  Z 1 Reply Last reply
                  0
                  • G Grand_Titan

                    @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.

                    Z Offline
                    Z Offline
                    zeljko
                    wrote on last edited by
                    #9

                    @Grand_Titan try to catch viewport events and block scrolling.

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      Grand_Titan
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      1
                      • G Grand_Titan has marked this topic as solved on

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved