代码如下:
#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"
注:以上代码仅为示例,可能存在不足之处,请以实际情况为准。