- 声明QList
存储tab的索引
在窗口类中声明一个QList
class MyWindow : public QWidget {
Q_OBJECT
public:
explicit MyWindow(QWidget *parent = nullptr);
private:
QList<int> m_history;
};
- 添加历史记录
在切换tab时,将当前tab的索引加入历史记录中。为了限制历史记录最多只能存储20页,可以在添加前先判断历史记录是否已经达到最大值,如果是,则删除最早的一项。
void MyWindow::onTabChanged(int index) {
// 添加历史记录
m_history.append(index);
if (m_history.size() > 20) {
m_history.removeFirst();
}
}
- 响应“前进”和“后退”按钮
将“前进”和“后退”按钮的clicked信号与对应的槽函数连接,用当前tab索引更新历史记录中的索引,然后再根据所选方向(前进或后退)获取下一个或上一个索引,并将其设置为当前tab的索引。
void MyWindow::onBackwardClicked() {
if (!m_history.isEmpty()) {
int currentIndex = ui->tableWidget->currentIndex().column();
m_history.append(currentIndex);
currentIndex = m_history.takeLast();
ui->tableWidget->setCurrentCell(0, currentIndex);
}
}
void MyWindow::onForwardClicked() {
if (!m_history.isEmpty()) {
int currentIndex = ui->tableWidget->currentIndex().column();
m_history.append(currentIndex);
currentIndex = m_history.takeFirst();
ui->tableWidget->setCurrentCell(0, currentIndex);
}
}
- 响应tab切换
将ui_QTableWidget的currentChanged信号连接到一个槽函数,用于更新历史记录。
connect(ui->tableWidget, SIGNAL(currentChanged(int,int,int,int)), this, SLOT(onTabChanged(int)));
- 完整示例代码
class MyWindow : public QWidget {
Q_OBJECT
public:
explicit MyWindow(QWidget *parent = nullptr);
private slots:
void onBackwardClicked();
void onForwardClicked();
void onTabChanged(int index);
private:
QList<int> m_history;
};
MyWindow::MyWindow(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout(this);
// 创建ui_QTableWidget并添加表格数据
QTableWidget *tableWidget = new QTableWidget(this);
tableWidget->setRowCount(3);
tableWidget->setColumnCount(3);
tableWidget->setItem(0, 0, new QTableWidgetItem("1"));
tableWidget->setItem(0, 1, new QTableWidgetItem("2"));
tableWidget->setItem(0, 2, new QTableWidgetItem("3"));
tableWidget->setItem(1, 0, new QTableWidgetItem("4"));
tableWidget->setItem(1, 1, new QTableWidgetItem("5"));
tableWidget->setItem(1, 2, new QTableWidgetItem("6"));
tableWidget->setItem(2, 0, new QTableWidgetItem("7"));
tableWidget->setItem(2, 1, new QTableWidgetItem("8"));
tableWidget->setItem(2, 2, new QTableWidgetItem("9"));
// 将ui_QTableWidget添加到窗口布局中
layout->addWidget(tableWidget);
// 添加“前进”和“后退”按钮,并连接到槽函数
QHBoxLayout *buttonLayout = new QHBoxLayout();
QPushButton *backwardButton = new QPushButton("后退", this);
QPushButton *forwardButton = new QPushButton("前进", this);
buttonLayout->addWidget(backwardButton);
buttonLayout->addWidget(forwardButton);
layout->addLayout(buttonLayout);
connect(backwardButton, SIGNAL(clicked()), this, SLOT(onBackwardClicked()));
connect(forwardButton, SIGNAL(clicked()), this, SLOT(onForwardClicked()));
// 将ui_QTableWidget的currentChanged信号连接到onTabChanged槽函数,用于更新历史记录
connect(tableWidget, SIGNAL(currentChanged(int,int,int,int)), this, SLOT(onTabChanged(int)));
}
void MyWindow::onBackwardClicked() {
if (!m_history.isEmpty()) {
int currentIndex = ui->tableWidget->currentIndex().column();
m_history.append(currentIndex);
currentIndex = m_history.takeLast();
ui->tableWidget->setCurrentCell(0, currentIndex);
}
}
void MyWindow::onForwardClicked() {
if (!m_history.isEmpty()) {
int currentIndex = ui->tableWidget->currentIndex().column();
m_history.append(currentIndex);
currentIndex = m_history.takeFirst();
ui->tableWidget->setCurrentCell(0, currentIndex);
}
}
void MyWindow::onTabChanged(int index) {
// 添加历史记录
m_history.append(index);
if (m_history.size() > 20) {
m_history.removeFirst();
}
}