Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. help with QScroller
Qt 6.11 is out! See what's new in the release blog

help with QScroller

Scheduled Pinned Locked Moved Solved Mobile and Embedded
7 Posts 2 Posters 1.9k Views 1 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.
  • J Offline
    J Offline
    josemarcio15
    wrote on last edited by
    #1

    Hello, I am developing an app and I encountered this issue. I am unable to scroll the text on touch devices with just one finger; I need to use two fingers to scroll the text. I tried to implement it as shown above but I am not succeeding. Could you please help me?

    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    #include <QFile>
    #include <QPlainTextEdit>
    #include <QScrollArea>
    #include <QScroller>

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {

    QPlainTextEdit *textEdit = new QPlainTextEdit(this);
    
    
    textEdit->setReadOnly(true);
    textEdit->setTextInteractionFlags(Qt::NoTextInteraction);
    
    
    QFile file(":/conteudo/Gn_1.txt");
    
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        QString text = in.readAll(); // Lê todo o conteúdo do arquivo de texto
        file.close();
    
      
        textEdit->setPlainText(text);
    } else {
        
        qDebug() << "Error";
    }
    
    
    QScrollArea *scrollArea = new QScrollArea(this);
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(textEdit);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QScroller::grabGesture(scrollArea->viewport(), QScroller::LeftMouseButtonGesture);
    
    
    
    
    setCentralWidget(scrollArea);
    

    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why are you putting your QTextEdit in a QScrollArea ? QTextEdit already is a QScrollArea.

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

      J 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Why are you putting your QTextEdit in a QScrollArea ? QTextEdit already is a QScrollArea.

        J Offline
        J Offline
        josemarcio15
        wrote on last edited by josemarcio15
        #3

        thanks @SGaist. I need to scroll the text with a mouse click, or on touch devices with 1 finger, and qplaintextedit doesn't support it, so I searched on some forums and saw that qscrollarea enables this scrolling

        SGaistS 1 Reply Last reply
        0
        • J josemarcio15

          thanks @SGaist. I need to scroll the text with a mouse click, or on touch devices with 1 finger, and qplaintextedit doesn't support it, so I searched on some forums and saw that qscrollarea enables this scrolling

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

          @josemarcio15 What do you mean by doesn't support it ?

          QPlainTextEdit is based on QAbstractScrollArea so, AFAIK, you can apply grabGesture directly on it.

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

          J 1 Reply Last reply
          0
          • SGaistS SGaist

            @josemarcio15 What do you mean by doesn't support it ?

            QPlainTextEdit is based on QAbstractScrollArea so, AFAIK, you can apply grabGesture directly on it.

            J Offline
            J Offline
            josemarcio15
            wrote on last edited by
            #5

            @SGaist like this "QScroller::grabGesture(ui->scrollArea->viewport(), QScroller::LeftMouseButtonGesture);"
            I managed to find a way, actually I was referring to the QPlainTextEdit incorrectly, so now I'm able to scroll the text with the left mouse button, but another issue has arisen, which I couldn't find a solution for. The text is scrolled too sensitively, and I couldn't reduce this speed. I'm trying to develop an Android app with Qt Widgets and at the end of development, I encountered this problem. I want to scroll the text with just one finger, and by default, the QPlainTextEdit requires using two fingers to scroll the text.

            QPlainTextEdit *textEdit = new QPlainTextEdit;
            textEdit->setReadOnly(true);
            textEdit->setTextInteractionFlags(Qt::NoTextInteraction);
            
            QFile file(":/conteudo/Gn_1.txt");
            if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QTextStream in(&file);
                QString text = in.readAll();
                file.close();
                textEdit->setPlainText(text);
            } else {
                qDebug() << "Error!";
            }
            QScroller::grabGesture(textEdit->viewport(), QScroller::LeftMouseButtonGesture);
            
            ui->scrollArea->setWidget(textEdit);
            ui->scrollArea->setWidgetResizable(true);
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I think you are hitting this QTBUG-48574.

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

              J 1 Reply Last reply
              0
              • SGaistS SGaist

                I think you are hitting this QTBUG-48574.

                J Offline
                J Offline
                josemarcio15
                wrote on last edited by
                #7

                @SGaist Thank you for your help, I found the solution. In my case, I needed to use QTextEdit instead of QPlainTextEdit. I didn't know there was a difference between the two, but indeed, QTextEdit worked perfectly for my project. Still, thank you for your attention, and this post will help others who have the same question! Goodbye."

                1 Reply Last reply
                1
                • J josemarcio15 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