以下是一个示例代码,演示了如何使用QSettings类来实现历史页面功能,并且在程序关闭时清空记录。
#include <QtCore>
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSettings settings("MyApp", "MySettings");
QStringList history = settings.value("History").toStringList();
QTabWidget tabWidget;
// 添加各个tab页
// ...
int currentTabIndex = 0;
if (!history.isEmpty()) {
// 如果历史记录不为空,则打开最近一次访问的页面
QString lastVisitedPage = history.last();
for (int i = 0; i < tabWidget.count(); i++) {
if (tabWidget.tabText(i) == lastVisitedPage) {
currentTabIndex = i;
break;
}
}
}
tabWidget.setCurrentIndex(currentTabIndex);
tabWidget.show();
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&] {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(tabWidget.currentIndex());
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
return app.exec();
}
接下来可以添加按下键盘“pageup”和“pagedown”来实现界面返回功能。当按下“pageup”键时,将当前页面的索引减1,并设置为当前页面。当按下“pagedown”键时,将当前页面的索引加1,并设置为当前页面。需要注意的是,当页面索引小于0或大于最大索引时,需要进行边界检查。
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&] {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(tabWidget.currentIndex());
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&app, &QApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(&tabWidget, &QTabWidget::currentChanged, [&](int index) {
// 当前页面发生变化时,更新历史记录
QString currentPage = tabWidget.tabText(index);
history.removeAll(currentPage); // 先删除已存在的记录
history.append(currentPage);
settings.setValue("History", history);
});
QObject::connect(qApp, &QCoreApplication::aboutToQuit, [&] {
// 程序关闭前清空历史记录
settings.setValue("History", QStringList());
});
// 处理键盘事件
QObject::connect(&tabWidget, &QWidget::keyPressEvent, [&](QKeyEvent *event) {
if (event->key() == Qt::Key_PageUp) {
int newIndex = tabWidget.currentIndex() - 1;
if (newIndex < 0) {
newIndex = 0;
}
tabWidget.setCurrentIndex(newIndex);
} else if (event->key() == Qt::Key_PageDown) {
int newIndex = tabWidget.currentIndex() + 1;
if (newIndex >= tabWidget.count()) {
newIndex = tabWidget.count() - 1;
}
tabWidget.setCurrentIndex(newIndex);
}
});
return app.exec();
}