基于QT的简单文本编辑器

MainWindow.cpp

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QTextEdit>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void open();
    void close();
private:
    Ui::MainWindow *ui;
    QAction *openAction;
    QAction *saveAction;
    QTextEdit *textEdit;
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QMenuBar"
#include"QAction"
#include"QMessageBox"
#include"QMenuBar"
#include"QToolBar"
#include"QStatusBar"
#include"QDebug"
#include<QTextEdit>
#include<QFileDialog>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    openAction=new QAction(QIcon(":/images/file-open"),tr("&Open"),this);
    openAction->setShortcut(QKeySequence::Open);
    openAction->setStatusTip("open new file");
    connect(openAction,SIGNAL(triggered()),
            this,SLOT(open()));

    saveAction=new QAction(QIcon(":/images/doc-open"),tr("&Save"),this);
    saveAction->setShortcut(QKeySequence::Save);
    saveAction->setStatusTip("save the file");

    connect(saveAction,SIGNAL(triggered()),
            this,SLOT(close()));

    QMenu *fileOpen=menuBar()->addMenu(tr("&File"));
    fileOpen->addAction(openAction);

    QToolBar *toolBar= addToolBar(tr("&File"));
    toolBar->addAction(openAction);
    toolBar->addAction(saveAction);

    /*QToolBar *toolBar2= addToolBar(tr("&Save"));
    toolBar2->addAction(saveAction);
    */
     statusBar();

     textEdit=new QTextEdit;

     setCentralWidget(textEdit);

}



MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::open()
{
    /*
   QMessageBox msgBox;
   msgBox.setText(tr("The decument has been modified"));

   msgBox.setInformativeText(tr("Do you want to save your changes"));

   msgBox.setDetailedText(tr("Differences here......"));

   msgBox.setStandardButtons(QMessageBox::Yes
                             |QMessageBox::No
                             |QMessageBox::Cancel);
   msgBox.setDefaultButton(QMessageBox::Cancel);

   int ret=msgBox.exec();

   switch(ret)
   {
   case QMessageBox::Yes:
       break;
   case QMessageBox::No:
       break;
   case QMessageBox::Cancel:
       break;
   }*/
    QString path=QFileDialog::getOpenFileName(this,
                                              tr("Open File"),
                                              "/",
                                              tr("Text Files(*.txt)"));

    if(!path.isEmpty())
    {
        QFile file(path);
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {

            QMessageBox::information(this,tr("faile"),tr("open file faile"));
            return ;
        }



    QTextStream in(&file);
    textEdit->setText(in.readAll());
    file.close();
    }
    else
    {

    QMessageBox::information(this,tr("faile"),tr("you not open any file"));
    }

}
void MainWindow::close()
{
    /*
    QDialog *dialog=new QDialog(this);
    dialog->setWindowTitle("Do you sure close window?");
    dialog->exec();
    QMessageBox::information(this,tr("can use"),tr("can ues"));
    */
   // qDebug()<<dialog->result();

    QString path=QFileDialog::getOpenFileName(this,
                                              tr("Open File"),
                                              "/",
                                              tr("Text Files(*.txt)"));

    if(!path.isEmpty())
    {
    QFile file(path);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        QMessageBox::warning(this,tr("Write File"),tr("Cant open file \n%1").arg(path));
        return ;
    }

    QTextStream out(&file);
    out<<textEdit->toPlainText();

    file.close();


    }
    else
    {
    QMessageBox::information(this,tr("faile"),tr("save faile"));


    }


}

Main.cpp

#include <QtGui/QApplication>
#include "mainwindow.h"

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

    return a.exec();
}

基于QT的简单文本编辑器,只是实现了简单的打开和保存功能。

函数:

QString getOpenFileName(QWidget *parent=0,
                                             const QString &caption=QSting(),
                                             const QString &dir=QString(),
                                             const QString &filter=QString(),
                                             QString *selectedFilter=0,
                                             Options options=0);

第一个参数parent,用于指定父组件。注意,很多Qt组件的构造函数都会有这么一个parent参数,并提供一个默认值0;

第二个参数caption,是对话框的标题;

第三个参数dir,是对话框显示时默认打开的目录,”.” 代表程序运行目录,”/” 代表当前盘符的根目录(Windows,Linux下/就是根目录了),也可以是平台相关的,比如”C:\”等;例如我想打开程序运行目录下的Data文件夹作为默认打开路径,这里应该写成”./Data/”,若想有一个默认选中的文件,则在目录后添加文件名即可:”./Data/teaser.graph”

第四个参数filter,是对话框的后缀名过滤器,比如我们使用”Image Files(.jpg .png)”就让它只能显示后缀名是jpg或者png的文件。如果需要使用多个过滤器,使用”;;”分割,比如”JPEG Files(.jpg);;PNG Files(.png)”;

第五个参数selectedFilter,是默认选择的过滤器;

第六个参数options,是对话框的一些参数设定,比如只显示文件夹等等,它的取值是enum QFileDialog::Option,每个选项可以使用 | 运算组合起来。

如果我要想选择多个文件怎么办呢?Qt提供了getOpenFileNames()函数,其返回值是一个QStringList。你可以把它理解成一个只能存放QString的List,也就是STL中的list。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值