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. Scrolling 2 QPlainTextEdits at the same time
Forum Updated to NodeBB v4.3 + New Features

Scrolling 2 QPlainTextEdits at the same time

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 884 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.
  • JerwinprabuJ Offline
    JerwinprabuJ Offline
    Jerwinprabu
    wrote on last edited by
    #1

    I want to create an internal tool for my usage with 2 QPlainTextEdits. I want to make a functionality that when i scroll one text edit the other is scrolled also. I tried to figure something out from http://doc.qt.io/qt-4.8/qt-widgets-codeeditor-example.html but it does not work as i expected

    Here is my code:

    CustomEdit.h

    #ifndef CUSTOMEDIT_H
    #define CUSTOMEDIT_H
    
    #include <QPlainTextEdit>
    
    class CustomEdit : public QPlainTextEdit
    {
        Q_OBJECT
    public:
        CustomEdit(QWidget *parent = 0);
    
    public slots:
        void updatePosition(const QRect &, int);
    };
    
    #endif // CUSTOMEDIT_H
    

    CustomEdit.cpp

    #include "customedit.h"
    
    CustomEdit::CustomEdit(QWidget *parent) : QPlainTextEdit(parent)
    {
    
    }
    
    void CustomEdit::updatePosition(const QRect &rect, int position) {
    
        if (position) {
            this->scroll(0, position);
            this->update(0, rect.y(), this->width(), rect.height());
        }
        else {
            this->update(0, rect.y(), this->width(), rect.height());
        }
    
    }
    

    MainWindow constructor

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        printoutAnalyzer = new PrintoutAnalyzer();
    
        ui->Log->setReadOnly(true);
        ui->Progress->reset();
    
        resultEdit = new CustomEdit(this);
        printoutEdit = new CustomEdit(this);
    
        resultEdit->setReadOnly(true);
        printoutEdit->setReadOnly(true);
    
        ui->horizontalLayout->addWidget(printoutEdit);
        ui->horizontalLayout->addWidget(resultEdit);
    
    
        connect(printoutAnalyzer, SIGNAL(writeToLog(QString)), this, SLOT(putLogText(QString)));
        connect(printoutAnalyzer, SIGNAL(writeToResult(QString)), this, SLOT(putResultText(QString)));
        connect(printoutAnalyzer, SIGNAL(setProgress(int)), this, SLOT(setProgress(int)));
        connect(ui->LoadPrintout, SIGNAL(triggered()), this, SLOT(choosePrintoutFile()));
    
        connect(printoutEdit, SIGNAL(updateRequest(QRect, int)), resultEdit, SLOT(updatePosition(QRect, int)));
        connect(resultEdit, SIGNAL(updateRequest(QRect, int)), printoutEdit, SLOT(updatePosition(QRect, int)));
    }
    

    When I execute the code and scroll one text edit the second one looks like this (text edits have the same content):

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

      Hi,

      The most simple would be to keep the vertical scroll bars of your QPlainTextEdit in sync with the help of the sliderPosition property.

      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
      1

      • Login

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