• 【QT】简单文本编辑器,open,save功能


    头文件内容

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 #include <QAction>
     6 #include <QMenu>
     7 #include <QToolBar>
     8 #include <QTextEdit>
     9 #include <QFileDialog>
    10 #include <QMessageBox>
    11 #include <QTextStream>
    12 #include <QMenuBar>
    13 
    14 class MainWindow : public QMainWindow
    15 {
    16     Q_OBJECT
    17 
    18 public:
    19     MainWindow(QWidget *parent = 0);
    20     ~MainWindow();
    21 
    22 private:
    23     QAction *openAction;
    24     QAction *saveAction;
    25     QTextEdit *textEdit;
    26     void saveFile();
    27     void openFile();
    28 
    29 };
    30 
    31 #endif // MAINWINDOW_H

    cpp文件内容

     1 #include "mainwindow.h"
     2 
     3 MainWindow::MainWindow(QWidget *parent)
     4     : QMainWindow(parent)
     5 {
     6     openAction = new QAction(QIcon(":/file"),tr("&Open..."),this);
     7     openAction->setShortcuts(QKeySequence::Open);
     8     openAction->setStatusTip(tr("Open an existing file"));
     9 
    10     saveAction = new QAction(QIcon(":/file"),tr("&Save.."),this);
    11     saveAction->setShortcuts(QKeySequence::Save);
    12     saveAction->setStatusTip(tr("Save a new file"));
    13 
    14     QMenu *file = menuBar()->addMenu(tr("&File"));
    15     file->addAction(openAction);
    16     file->addAction(saveAction);
    17 
    18     QToolBar *toolBar = addToolBar(tr("&File"));
    19     toolBar->addAction(openAction);
    20     toolBar->addAction(saveAction);
    21 
    22     textEdit = new QTextEdit(this);
    23     setCentralWidget(textEdit);
    24 
    25     connect(openAction,&QAction::triggered,this,&MainWindow::openFile);
    26     connect(saveAction,&QAction::triggered,this,&MainWindow::saveFile);
    27 }
    28 
    29 void MainWindow::openFile()
    30 {
    31     QString path = QFileDialog::getOpenFileName(this,
    32                                                 tr("Open File"),
    33                                                 ".",
    34                                                 tr("Text Files(*.txt)"));
    35     if(!path.isEmpty()){
    36         QFile file(path);
    37         if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
    38             QMessageBox::warning(this,tr("Read File"),
    39                                  tr("Cannot open file:
    %1").arg(path));
    40             return;
    41         }
    42         QTextStream in(&file);
    43         textEdit->setText(in.readAll());
    44         file.close();
    45     }else{
    46         QMessageBox::warning(this,tr("Path"),
    47                              tr("You did not select any file."));
    48     }
    49 }
    50 
    51 void MainWindow::saveFile()
    52 {
    53     QString path = QFileDialog::getSaveFileName(this,
    54                                                 tr("Open File"),
    55                                                 ".",
    56                                                 tr("Text Files(*.txt)"));
    57     if(!path.isEmpty()){
    58         QFile file(path);
    59         if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
    60             QMessageBox::warning(this,tr("Write File"),
    61                                  tr("cannot open file:
    %1").arg(path));
    62             return;
    63         }
    64         QTextStream out(&file);
    65         out << textEdit->toPlainText();
    66         file.close();
    67     }else{
    68         QMessageBox::warning(this,tr("Path"),
    69                              tr("You did not select any file."));
    70     }
    71 }
    72 
    73 MainWindow::~MainWindow()
    74 {
    75 
    76 }

    截图

  • 相关阅读:
    CF375D Tree and Queries
    进制转换
    贪心问题
    next_permutation函数
    C++ STL
    一些排序总结
    KMP算法
    围圈报数
    车辆调度—模拟栈的操作
    搜索题
  • 原文地址:https://www.cnblogs.com/powercool/p/14442071.html
Copyright © 2020-2023  润新知