QT开发中文网

Qt C++使用QSettings类来存储当前打开文件的历史页面,通过按下键盘的“pageup”和“pagedown”来实现界面的返回和多次返回功能,关闭程序后清空记录

以下是一个示例代码,演示了如何使用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();
}

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=1132

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?