我想用2个QPlainTextEdits为我的用法创建一个内部工具。我想创建一个功能,当我滚动一个文本编辑时,另一个也滚动。我试图从http://doc.qt.io/qt-4.8/qt-widgets-codeeditor-example.html找出一些东西,但它不能像我预期的那样工作
这是我的代码:
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构造函数
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)));
}
当我执行代码并滚动一个文本编辑时,第二个看起来像这样(文本编辑具有相同的内容):
答案 0 :(得分:0)
最简单的方法是让QPlainTextEdit的垂直滚动条与''sliderPosition属性''保持同步。