QT开发中文网

Qt 只存储当前打开文件的历史页面,最多存20页,通过按下键盘的“前进”和“后退”按钮来实现界面的前进和返回、多次前进和多次返回功能,关闭文件后清空所有记录C++

代码如下:

#include <QtWidgets>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void openFile();
    void goBack();
    void goForward();

private:
    QList<QUrl> history;
    int currentIndex;

    QAction *openAction;
    QAction *backAction;
    QAction *forwardAction;

    QTextEdit *textEdit;
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), currentIndex(-1)
{
    openAction = new QAction(tr("&Open"), this);
    openAction->setShortcut(QKeySequence::Open);
    connect(openAction, &QAction::triggered, this, &MainWindow::openFile);

    backAction = new QAction(tr("&Back"), this);
    backAction->setShortcut(Qt::ALT + Qt::Key_Left);
    backAction->setEnabled(false);
    connect(backAction, &QAction::triggered, this, &MainWindow::goBack);

    forwardAction = new QAction(tr("&Forward"), this);
    forwardAction->setShortcut(Qt::ALT + Qt::Key_Right);
    forwardAction->setEnabled(false);
    connect(forwardAction, &QAction::triggered, this, &MainWindow::goForward);

    QToolBar *toolbar = addToolBar(tr("Toolbar"));
    toolbar->addAction(openAction);
    toolbar->addAction(backAction);
    toolbar->addAction(forwardAction);

    textEdit = new QTextEdit(this);
    setCentralWidget(textEdit);
}

MainWindow::~MainWindow()
{
}

void MainWindow::openFile()
{
    QFileDialog dialog(this);
    dialog.setFileMode(QFileDialog::ExistingFile);
    if (dialog.exec() == QDialog::Accepted) {
        QUrl url = dialog.selectedUrls().first();
        if (!history.isEmpty() && url == history.last()) {
            // Do nothing if the selected file is the current file.
            return;
        }
        if (currentIndex < history.count() - 1) {
            // Remove all items after current index if new item is added.
            for (int i = currentIndex + 1; i < history.count(); ++i) {
                history.removeAt(i);
            }
        }
        history.append(url);
        currentIndex = history.count() - 1;

        // Enable/disable back and forward actions depending on current index.
        backAction->setEnabled(currentIndex > 0);
        forwardAction->setEnabled(currentIndex < history.count() - 1);

        // Set text of text edit to contents of selected file.
        QFile file(url.toLocalFile());
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream stream(&file);
            textEdit->setPlainText(stream.readAll());
        }

        // Update window title with name of selected file.
        setWindowTitle(url.fileName());
    }
}

void MainWindow::goBack()
{
    if (currentIndex > 0) {
        currentIndex--;
        QUrl url = history.at(currentIndex);

        // Enable/disable back and forward actions depending on current index.
        backAction->setEnabled(currentIndex > 0);
        forwardAction->setEnabled(currentIndex < history.count() - 1);

        // Set text of text edit to contents of selected file.
        QFile file(url.toLocalFile());
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream stream(&file);
            textEdit->setPlainText(stream.readAll());
        }

        // Update window title with name of selected file.
        setWindowTitle(url.fileName());
    }
}

void MainWindow::goForward()
{
    if (currentIndex < history.count() - 1) {
        currentIndex++;
        QUrl url = history.at(currentIndex);

        // Enable/disable back and forward actions depending on current index.
        backAction->setEnabled(currentIndex > 0);
        forwardAction->setEnabled(currentIndex < history.count() - 1);

        // Set text of text edit to contents of selected file.
        QFile file(url.toLocalFile());
        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream stream(&file);
            textEdit->setPlainText(stream.readAll());
        }

        // Update window title with name of selected file.
        setWindowTitle(url.fileName());
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

#include "main.moc"

注:以上代码仅为示例,可能存在不足之处,请以实际情况为准。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?