notepad

此项目用QT,C++实现了记事本 notepad功能,具有复制,粘贴,剪切等功能

应用介绍

QT C++ 项目 Notepad

如下特点

  • Copy
  • Paste
  • Cut
  • Paste
  • Print
  • Font

项目目录结构

Source code structure of notepad in QT c++


Project Explorer in QT Creator 

main.cpp

#include "mynotepad.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication EditorApp(argc, argv);
    MyNotepad Editor;
    Editor.show();

    return EditorApp.exec();
}

mynotepad.h

#ifndef MYNOTEPAD_H
#define MYNOTEPAD_H

#include <QMainWindow>

namespace Ui {
class MyNotepad;
}


class MyNotepad : public QMainWindow
{
    Q_OBJECT

public:
    explicit MyNotepad(QWidget *parent = 0);
    ~MyNotepad();

private slots:
    void newDocument();

    void copy();

    void cut();

    void paste();
    
    void open();

    void save();

    void saveAs();

    void print();

    void exit();

    void undo();

    void redo();

    void selectFont();

    void setFontBold(bool bold);

    void setFontUnderline(bool underline);

    void setFontItalic(bool italic);

    void about();

private:
    Ui::MyNotepad *ui;
    QString currentFile;
};

#endif // MYNOTEPAD_H

mynotepad.cpp

#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>
#if defined(QT_PRINTSUPPORT_LIB)
#include <QtPrintSupport/qtprintsupportglobal.h>
#if QT_CONFIG(printer)
#if QT_CONFIG(printdialog)
#include <QPrintDialog>
#endif // QT_CONFIG(printdialog)
#include <QPrinter>
#endif // QT_CONFIG(printer)
#endif // QT_PRINTSUPPORT_LIB
#include <QFont>
#include <QFontDialog>

#include "mynotepad.h"
#include "ui_mynotepad.h"

MyNotepad::MyNotepad(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MyNotepad)
{
    ui->setupUi(this);
    this->setCentralWidget(ui->textEdit);

    connect(ui->actionNew, &QAction::triggered, this, &MyNotepad::newDocument);
    connect(ui->actionOpen, &QAction::triggered, this, &MyNotepad::open);
    connect(ui->actionSave, &QAction::triggered, this, &MyNotepad::save);
    connect(ui->actionSave_as, &QAction::triggered, this, &MyNotepad::saveAs);
    connect(ui->actionPrint, &QAction::triggered, this, &MyNotepad::print);
    connect(ui->actionExit, &QAction::triggered, this, &MyNotepad::exit);
    connect(ui->actionCopy, &QAction::triggered, this, &MyNotepad::copy);
    connect(ui->actionCut, &QAction::triggered, this, &MyNotepad::cut);
    connect(ui->actionPaste, &QAction::triggered, this, &MyNotepad::paste);
    connect(ui->actionUndo, &QAction::triggered, this, &MyNotepad::undo);
    connect(ui->actionRedo, &QAction::triggered, this, &MyNotepad::redo);
    connect(ui->actionFont, &QAction::triggered, this, &MyNotepad::selectFont);
    connect(ui->actionBold, &QAction::triggered, this, &MyNotepad::setFontBold);
    connect(ui->actionUnderline, &QAction::triggered, this, &MyNotepad::setFontUnderline);
    connect(ui->actionItalic, &QAction::triggered, this, &MyNotepad::setFontItalic);
    connect(ui->actionAbout, &QAction::triggered, this, &MyNotepad::about);

// Disable menu actions for unavailable features
#if !QT_CONFIG(printer)
    ui->actionPrint->setEnabled(false);
#endif

#if !QT_CONFIG(clipboard)
    ui->actionCut->setEnabled(false);
    ui->actionCopy->setEnabled(false);
    ui->actionPaste->setEnabled(false);
#endif
}

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

void MyNotepad::newDocument()
{
    currentFile.clear();
    ui->textEdit->setText(QString());
}

void MyNotepad::open()
{
    QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
    QFile file(fileName);
    currentFile = fileName;
    if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
        QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
        return;
    }
    setWindowTitle(fileName);
    QTextStream in(&file);
    QString text = in.readAll();
    ui->textEdit->setText(text);
    file.close();
}

void MyNotepad::save()
{
    QString fileName;
    // If we don't have a filename from before, get one.
    if (currentFile.isEmpty()) {
        fileName = QFileDialog::getSaveFileName(this, "Save");
        currentFile = fileName;
    } else {
        fileName = currentFile;
    }
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
        return;
    }
    setWindowTitle(fileName);
    QTextStream out(&file);
    QString text = ui->textEdit->toPlainText();
    out << text;
    file.close();
}

void MyNotepad::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this, "Save as");
    QFile file(fileName);

    if (!file.open(QFile::WriteOnly | QFile::Text)) {
        QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
        return;
    }
    currentFile = fileName;
    setWindowTitle(fileName);
    QTextStream out(&file);
    QString text = ui->textEdit->toPlainText();
    out << text;
    file.close();
}

void MyNotepad::print()
{
#if QT_CONFIG(printer)
    QPrinter printDev;
#if QT_CONFIG(printdialog)
    QPrintDialog dialog(&printDev, this);
    if (dialog.exec() == QDialog::Rejected)
        return;
#endif // QT_CONFIG(printdialog)
    ui->textEdit->print(&printDev);
#endif // QT_CONFIG(printer)
}

void MyNotepad::exit()
{
    QCoreApplication::quit();
}

void MyNotepad::copy()
{
#if QT_CONFIG(clipboard)
    ui->textEdit->copy();
#endif
}

void MyNotepad::cut()
{
#if QT_CONFIG(clipboard)
    ui->textEdit->cut();
#endif
}

void MyNotepad::paste()
{
#if QT_CONFIG(clipboard)
    ui->textEdit->paste();
#endif
}

void MyNotepad::undo()
{
     ui->textEdit->undo();
}

void MyNotepad::redo()
{
    ui->textEdit->redo();
}

void MyNotepad::selectFont()
{
    bool fontSelected;
    QFont font = QFontDialog::getFont(&fontSelected, this);
    if (fontSelected)
        ui->textEdit->setFont(font);
}

void MyNotepad::setFontUnderline(bool underline)
{
    ui->textEdit->setFontUnderline(underline);
}

void MyNotepad::setFontItalic(bool italic)
{
    ui->textEdit->setFontItalic(italic);
}

void MyNotepad::setFontBold(bool bold)
{
    bold ? ui->textEdit->setFontWeight(QFont::Bold) :
           ui->textEdit->setFontWeight(QFont::Normal);
}

void MyNotepad::about()
{
   QMessageBox::about(this, tr("About MDI"),
                tr("Notepad in QT C++ By CppBuzz.com. Users are allowed to download & modify it."
                   "text editor using QtWidgets"));

}

Output of Project

Notepad code in QT C++

文件列表(部分)

名称 大小 修改日期
MyNotepad0.00 KB2019-04-28
images0.00 KB2019-04-26
bold.png0.71 KB2019-04-40
copy.png1.59 KB2019-04-12
create.png0.45 KB2019-04-16
cut.png9.33 KB2019-04-22
edit_redo.png7.29 KB2019-04-26
edit_undo.png8.23 KB2019-04-32
exit.png0.37 KB2019-04-38
font.png6.82 KB2019-04-40
info.png0.54 KB2019-04-44
italic.png0.46 KB2019-04-50
new.png7.25 KB2019-04-54
open.png5.31 KB2019-04-58
paste.png3.51 KB2019-04-04
pencil.png3.69 KB2019-04-10
print.png0.32 KB2019-04-14
save.png2.64 KB2019-04-18
save_as.png8.02 KB2019-04-22
Thumbs.db83.00 KB2019-04-40
underline.png0.51 KB2019-04-26
main.cpp0.20 KB2019-04-10
mainwindow.ui0.62 KB2019-04-18
mynotepad.cpp5.36 KB2019-04-50
mynotepad.h0.73 KB2019-04-10
mynotepad.qrc0.75 KB2019-04-16
mynotepad.ui8.96 KB2019-04-28
notepad.pro0.36 KB2019-04-36
notepad.pro.user23.87 KB2019-04-14

立即下载

相关下载

[notepad] 此项目用QT,C++实现了记事本 notepad功能,具有复制,粘贴,剪切等功能

评论列表 共有 0 条评论

暂无评论

微信捐赠

微信扫一扫体验

立即
上传
发表
评论
返回
顶部