我想同步滚动两个小部件(textEdit1和textEdit2)。
#include <QDialog>
#include <QTabWidget>
#include <QLayout>
#include <QTextEdit>
#include <QScrollBar>
class MainWindow : public QDialog{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0) :
QDialog(parent)
{
setLayout(new QVBoxLayout(this));
QTabWidget *tabWidget = new QTabWidget();
layout()->addWidget(tabWidget);
QString text = "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n";
QTextEdit *textEdit1 = new QTextEdit(tabWidget);
textEdit1->setText(text);
tabWidget->addTab(textEdit1, "1");
QTextEdit *textEdit2 = new QTextEdit(tabWidget);
textEdit2->setText(text);
tabWidget->addTab(textEdit2, "2");
//Scrollbar synchronization
connect(textEdit1->verticalScrollBar(), SIGNAL(valueChanged(int)),
textEdit2->verticalScrollBar(), SLOT(setValue(int)));
connect(textEdit2->verticalScrollBar(), SIGNAL(valueChanged(int)),
textEdit1->verticalScrollBar(), SLOT(setValue(int)));
setMaximumHeight(100);
}
};
我做错了什么?