界面如下所示,一个窗口中包含两个QTableWidget,通过右边的滚动条控制两个QTableWidget的滚动
代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QScrollBar>
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QWidget *centralWidget;
QWidget *widget;
QHBoxLayout *horizontalLayout;
QTableWidget *tableWidget;
QTableWidget *tableWidget_2;
public slots:
void onSliderMoved(int pos);
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
if (this->objectName().isEmpty())
this->setObjectName(QStringLiteral("MainWindow"));
this->resize(668, 309);
centralWidget = new QWidget(this);
centralWidget->setObjectName(QStringLiteral("centralWidget"));
widget = new QWidget(centralWidget);
widget->setObjectName(QStringLiteral("widget"));
widget->setGeometry(QRect(10, 10, 631, 281));
horizontalLayout = new QHBoxLayout(widget);
horizontalLayout->setSpacing(6);
horizontalLayout->setContentsMargins(11, 11, 11, 11);
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
horizontalLayout->setContentsMargins(0, 0, 0, 0);
tableWidget = new QTableWidget(widget);
if (tableWidget->columnCount() < 2)
tableWidget->setColumnCount(2);
QTableWidgetItem *__qtablewidgetitem = new QTableWidgetItem();
tableWidget->setHorizontalHeaderItem(0, __qtablewidgetitem);
QTableWidgetItem *__qtablewidgetitem1 = new QTableWidgetItem();
tableWidget->setHorizontalHeaderItem(1, __qtablewidgetitem1);
if (tableWidget->rowCount() < 18)
tableWidget->setRowCount(18);
QTableWidgetItem *__qtablewidgetitem2 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(0, __qtablewidgetitem2);
QTableWidgetItem *__qtablewidgetitem3 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(1, __qtablewidgetitem3);
QTableWidgetItem *__qtablewidgetitem4 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(2, __qtablewidgetitem4);
QTableWidgetItem *__qtablewidgetitem5 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(3, __qtablewidgetitem5);
QTableWidgetItem *__qtablewidgetitem6 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(4, __qtablewidgetitem6);
QTableWidgetItem *__qtablewidgetitem7 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(5, __qtablewidgetitem7);
QTableWidgetItem *__qtablewidgetitem8 = new QTableWidgetItem();
tableWidget->setVerticalHeaderItem(6, __qtablewidgetitem8);
QTableWidgetItem *__qtablewidgetitem9