Scrolling 2 QPlainTextEdits at the same time



  • 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):


  • Lifetime Qt Champion

    Hi,

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.