应用介绍
QT C++ 项目 Notepad
如下特点
- Copy
- Paste
- Cut
- Paste
- Font
项目目录结构
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
©版权声明:本文内容由互联网用户自发贡献,版权归原创作者所有,本站不拥有所有权,也不承担相关法律责任。如果您发现本站中有涉嫌抄袭的内容,欢迎发送邮件至: www_apollocode_net@163.com 进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
转载请注明出处: apollocode » notepad
文件列表(部分)
名称 | 大小 | 修改日期 |
---|---|---|
MyNotepad | 0.00 KB | 2019-04-28 |
images | 0.00 KB | 2019-04-26 |
bold.png | 0.71 KB | 2019-04-40 |
copy.png | 1.59 KB | 2019-04-12 |
create.png | 0.45 KB | 2019-04-16 |
cut.png | 9.33 KB | 2019-04-22 |
edit_redo.png | 7.29 KB | 2019-04-26 |
edit_undo.png | 8.23 KB | 2019-04-32 |
exit.png | 0.37 KB | 2019-04-38 |
font.png | 6.82 KB | 2019-04-40 |
info.png | 0.54 KB | 2019-04-44 |
italic.png | 0.46 KB | 2019-04-50 |
new.png | 7.25 KB | 2019-04-54 |
open.png | 5.31 KB | 2019-04-58 |
paste.png | 3.51 KB | 2019-04-04 |
pencil.png | 3.69 KB | 2019-04-10 |
print.png | 0.32 KB | 2019-04-14 |
save.png | 2.64 KB | 2019-04-18 |
save_as.png | 8.02 KB | 2019-04-22 |
Thumbs.db | 83.00 KB | 2019-04-40 |
underline.png | 0.51 KB | 2019-04-26 |
main.cpp | 0.20 KB | 2019-04-10 |
mainwindow.ui | 0.62 KB | 2019-04-18 |
mynotepad.cpp | 5.36 KB | 2019-04-50 |
mynotepad.h | 0.73 KB | 2019-04-10 |
mynotepad.qrc | 0.75 KB | 2019-04-16 |
mynotepad.ui | 8.96 KB | 2019-04-28 |
notepad.pro | 0.36 KB | 2019-04-36 |
notepad.pro.user | 23.87 KB | 2019-04-14 |
发表评论 取消回复