help with QScroller
-
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);}
-
Hi and welcome to devnet,
Why are you putting your QTextEdit in a QScrollArea ? QTextEdit already is a QScrollArea.
-
Hi and welcome to devnet,
Why are you putting your QTextEdit in a QScrollArea ? QTextEdit already is a QScrollArea.
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
-
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
@josemarcio15 What do you mean by doesn't support it ?
QPlainTextEdit is based on QAbstractScrollArea so, AFAIK, you can apply grabGesture directly on it.
-
@josemarcio15 What do you mean by doesn't support it ?
QPlainTextEdit is based on QAbstractScrollArea so, AFAIK, you can apply grabGesture directly on it.
@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); -
I think you are hitting this QTBUG-48574.
-
I think you are hitting this QTBUG-48574.
@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."
-
J josemarcio15 has marked this topic as solved on