Qt编写的我的记事本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fei86155/article/details/77592334
#include "mainwindow.h"
#include <QToolBar>
#include <QStatusBar>
#include <QMenuBar>
#include <QTextEdit>
#include <QFileDialog>
#include <QFile>
#include <QTextEdit>
#include <QTextStream>
#include <QMessageBox>
#include <QLabel>
#include <QFontDialog>
#include <QDebug>
#include <QColorDialog>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QSystemTrayIcon>
#include <QCloseEvent>

void MainWindow::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton button;
    button = QMessageBox::question(this, tr("退出程序?"),
        QString(tr("是否最小化到托盘?")),
        QMessageBox::Yes | QMessageBox::No);

    if (button == QMessageBox::Yes) {
        event ->ignore();
        this->hide();  //忽略退出信号,程序继续运行
    }
    else if (button == QMessageBox::No) {
        event->accept();  //接受退出信号,程序退出
    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    textEdit = new QTextEdit();
    this->setCentralWidget(textEdit);                      //设置文本为主窗体
    this->resize(400,400);
    this->setWindowTitle("我的记事本");
    this->setWindowIcon(QIcon(":/images/notePad.ico"));
//菜单栏
    QMenuBar * myMenuBar = this ->menuBar();
    QMenu * file = myMenuBar ->addMenu("文件");
    QMenu * edit = myMenuBar ->addMenu("编辑");
    QMenu * format = myMenuBar ->addMenu("格式");
//  QMenu * check = myMenuBar ->addMenu("查看");
    QMenu * help = myMenuBar ->addMenu("帮助");
//menu ->file
    file->addAction(QIcon(":/images/createNewFile.ico"),"新建",this,SLOT(doCreateNewFile()),QKeySequence::New);
    file->addAction(QIcon(":/images/openFile.ico"),"打开文件",this,SLOT(doOpenFile()),QKeySequence::Open);
    file->addSeparator();
    file->addAction(QIcon(":/images/saveFile.ico"),"保存文件",this,SLOT(doSaveFile()),QKeySequence::Save);
    file->addSeparator();
    file->addAction(QIcon(":/images/exit.ico"),"退出",this,SLOT(close()),QKeySequence::Quit);
//menu ->edit
    edit ->addAction(QIcon(":/images/search.ico"),"查找",this,SLOT(doSearchText()));
//menu ->format
    format->addAction(QIcon(":/images/font.ico"),"更换字体",this,SLOT(doChangeFont()));
    format->addAction(QIcon(":/images/color.ico"),"更换颜色",this,SLOT(doChangeColor()));
//menu ->help
    help ->addAction(QIcon(":/images/关于.ico"),"关于我的记事本",this,SLOT(doHelp()));

//工具栏
    QToolBar * myToolBar = this ->addToolBar("tool");
    myToolBar ->addAction(QIcon(":/images/createNewFile.ico"),"New File",this,SLOT(doCreateNewFile()));
    myToolBar ->addAction(QIcon(":/images/openFile.ico"),"Open File",this,SLOT(doOpenFile()));
    myToolBar ->addSeparator();
    myToolBar ->addAction(QIcon(":/images/saveFile.ico"),"Save File",this,SLOT(doSaveFile()));
//状态栏
    QStatusBar * myStatusBar = this -> statusBar();
    QLabel * label = new QLabel("Good Good Study ! Day Day Up !");
    myStatusBar ->addWidget(label);
//系统托盘
    QSystemTrayIcon * systemTray = new QSystemTrayIcon(QIcon(":/images/tray.ico"),this);
    systemTray ->setContextMenu(file);
    systemTray ->setToolTip("我的记事本");
    systemTray ->show();
//托盘信号
    connect(systemTray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(doShow(QSystemTrayIcon::ActivationReason)));
}

MainWindow::~MainWindow()
{   
}

void MainWindow::doCreateNewFile()
{
    if(!textEdit ->toPlainText().isEmpty())
    {
        int ret = QMessageBox::warning(this,"waring","是否保存当前编辑内容?",QMessageBox::Yes,QMessageBox::No);
        if(ret == QMessageBox::Yes)
        {
            doSaveFile();
        }
        else if(ret == QMessageBox::No)
        {
            textEdit ->clear();
        }
    }
    else
    {
        textEdit ->clear();
    }
}

void MainWindow::doOpenFile()
{
    if(!textEdit ->toPlainText().isEmpty())
    {
        int ret = QMessageBox::warning(this,"waring","是否保存当前编辑内容?",QMessageBox::Yes,QMessageBox::No);
        if(ret == QMessageBox::Yes)
        {
            doSaveFile();
        }
        else if(ret == QMessageBox::No)
        {
        }
    }
    {
       QString openPath = QFileDialog::getOpenFileName(this,"打开文件",".","*.txt;;*.doc");
       QFile file(openPath);
       QTextStream in(&file);
       if(file.open(QIODevice::ReadOnly|QIODevice::Text))
       {
    //       QByteArray ba = file.readAll();        //不支持中文显示
    //       textEdit->setText(QString(ba));
           while(!in.atEnd())
           {
               textEdit ->setText(QString(in.readLine()));
           }
           file.close();
       }
    }
}

void MainWindow::doSaveFile()
{
    QString savePath = QFileDialog::getSaveFileName(this,"打开文件",".","*.txt;;*.doc");
    QFile file(savePath);
    QTextStream out(&file);
    if(file.open(QIODevice::WriteOnly|QIODevice::Text))
    {
       out << textEdit ->toPlainText();
       file.close();
    }
}

void MainWindow::doChangeFont()
{
    bool ok;
    QFont font = QFontDialog::getFont(&ok,this);
    textEdit ->setFont(font);
}

void MainWindow::doChangeColor()
{
    QColor color = QColorDialog::getColor(Qt::blue,this,"调色器");
    textEdit ->setTextColor(color);
}

void MainWindow::doHelp()
{
    QLabel * label = new QLabel;
    label ->setWindowTitle("帮助");
    label ->resize(200,200);
    label ->setAlignment(Qt::AlignHCenter);
    label ->setText("我的记事本帮助文档\n\n\n哈哈哈哈");
    label ->show();
}

void MainWindow::doSearchText()
{
    QVBoxLayout * vLayout = new QVBoxLayout;
    QHBoxLayout * hLayout = new QHBoxLayout;
    findWidget = new QWidget;
    findWidget ->setWindowTitle("Find");
    QLabel * findLabel = new QLabel(findWidget);
    findLabel ->setText("请输入要查找的内容:");
    findEdit = new QLineEdit(findWidget);

    QPushButton * findBtn = new QPushButton("查找");                 //查找按钮布局
    hLayout ->addStretch(1);
    hLayout ->addWidget(findBtn);
    vLayout ->addWidget(findLabel);
    vLayout ->addWidget(findEdit);
    vLayout ->addLayout(hLayout);

    findWidget ->setLayout(vLayout);
    findWidget ->show();
    connect(findBtn,SIGNAL(clicked(bool)),this,SLOT(doSearch()));
}

void MainWindow::doSearch()
{
    findStr = findEdit ->text();
    if(textEdit ->find(findStr,QTextDocument::FindBackward))                //查找
        ;
    else
    {
        QMessageBox::warning(findWidget,"Waring","已经查找到头了!",QMessageBox::Ok);
    }

}

void MainWindow::doShow(QSystemTrayIcon::ActivationReason trayRet)
{
    if(trayRet == QSystemTrayIcon::DoubleClick)
        this ->show();
}

猜你喜欢

转载自blog.csdn.net/fei86155/article/details/77592334