This commit is contained in:
Piyush मिश्रः 2019-08-13 11:23:43 +05:30 committed by GitHub
parent d97797e86a
commit 463628ef55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 507 additions and 0 deletions

40
Notepad.pro Normal file
View File

@ -0,0 +1,40 @@
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-10T22:10:36
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets printsupport
TARGET = Notepad
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

11
main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

138
mainwindow.cpp Normal file
View File

@ -0,0 +1,138 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#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
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_triggered()
{
currentFile.clear();
ui->textEdit->setText(QString());
}
void MainWindow::on_actionOpen_triggered()
{
QString filename = QFileDialog::getOpenFileName(this, "Open File");
if(filename.isEmpty()) return;
QFile file(filename);
currentFile = filename;
if(!file.open(QIODevice::ReadOnly)){
ui->statusBar->showMessage("Can't open file: "+file.errorString(), 3000);
}
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
this->setWindowTitle(QFileInfo(filename).fileName()+" - Notepad");
}
bool MainWindow::on_actionSave_triggered()
{
QFile file(currentFile);
if(!file.exists())
{
this->setWindowTitle("Notepad");
QString filename = QFileDialog::getSaveFileName(this, "Save");
if(filename.isEmpty()) return false;
file.setFileName(filename);
currentFile = filename;
this->setWindowTitle(QFileInfo(filename).fileName()+" - Notepad");
}
if(!file.open(QIODevice::WriteOnly)){
ui->statusBar->showMessage("Can't save file: "+file.errorString(), 3000);
return false;
}
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
return true;
}
void MainWindow::on_actionSaveAs_triggered()
{
QString tmp = currentFile;
currentFile.clear();
if(!on_actionSave_triggered())
{
this->setWindowTitle(QFileInfo(tmp).fileName()+" - Notepad");
currentFile = tmp;
}
}
void MainWindow::on_actionPrint_triggered()
{
#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 MainWindow::on_actionExit_triggered()
{
qApp->exit();
}
void MainWindow::on_actionCopy_triggered()
{
ui->textEdit->copy();
}
void MainWindow::on_actionCut_triggered()
{
ui->textEdit->cut();
}
void MainWindow::on_actionPaste_triggered()
{
ui->textEdit->paste();
}
void MainWindow::on_actionUndo_triggered()
{
ui->textEdit->undo();
}
void MainWindow::on_actionRedo_triggered()
{
ui->textEdit->redo();
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox msgBox;
msgBox.setText("Developed by PiyushXCoder");
msgBox.exec();
}

53
mainwindow.h Normal file
View File

@ -0,0 +1,53 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextStream>
#include <QPrinter>
#include <QPrintDialog>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_actionNew_triggered();
void on_actionOpen_triggered();
bool on_actionSave_triggered();
void on_actionSaveAs_triggered();
void on_actionExit_triggered();
void on_actionCopy_triggered();
void on_actionCut_triggered();
void on_actionPaste_triggered();
void on_actionUndo_triggered();
void on_actionRedo_triggered();
void on_actionAbout_triggered();
void on_actionPrint_triggered();
private:
Ui::MainWindow *ui;
QString currentFile;
};
#endif // MAINWINDOW_H

265
mainwindow.ui Normal file
View File

@ -0,0 +1,265 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>500</height>
</rect>
</property>
<property name="windowTitle">
<string>Notepad</string>
</property>
<property name="windowIcon">
<iconset theme="document">
<normaloff>.</normaloff>.</iconset>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="separator"/>
<addaction name="actionSave"/>
<addaction name="actionSaveAs"/>
<addaction name="separator"/>
<addaction name="actionPrint"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuEdit">
<property name="title">
<string>Edit</string>
</property>
<addaction name="actionCopy"/>
<addaction name="actionCut"/>
<addaction name="actionPaste"/>
<addaction name="separator"/>
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
<addaction name="actionAbout"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actionNew"/>
<addaction name="actionOpen"/>
<addaction name="actionSave"/>
<addaction name="separator"/>
<addaction name="actionCopy"/>
<addaction name="actionCut"/>
<addaction name="actionPaste"/>
<addaction name="separator"/>
<addaction name="actionUndo"/>
<addaction name="actionRedo"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionNew">
<property name="icon">
<iconset theme="document-new">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>New</string>
</property>
<property name="toolTip">
<string>New Document</string>
</property>
<property name="shortcut">
<string>Ctrl+N</string>
</property>
</action>
<action name="actionOpen">
<property name="icon">
<iconset theme="document-open">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Open</string>
</property>
<property name="toolTip">
<string>Open Document</string>
</property>
<property name="shortcut">
<string>Ctrl+O</string>
</property>
</action>
<action name="actionSave">
<property name="icon">
<iconset theme="document-save">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Save</string>
</property>
<property name="toolTip">
<string>Save Document</string>
</property>
<property name="shortcut">
<string>Ctrl+S</string>
</property>
</action>
<action name="actionSaveAs">
<property name="icon">
<iconset theme="document-save-as">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>SaveAs</string>
</property>
<property name="toolTip">
<string>Save Document As</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+S</string>
</property>
</action>
<action name="actionExit">
<property name="icon">
<iconset theme="exit">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Exit</string>
</property>
<property name="toolTip">
<string>Exit </string>
</property>
<property name="shortcut">
<string>Ctrl+Q</string>
</property>
</action>
<action name="actionCopy">
<property name="icon">
<iconset theme="edit-copy">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Copy</string>
</property>
<property name="toolTip">
<string>Copy Text</string>
</property>
<property name="shortcut">
<string>Ctrl+C</string>
</property>
</action>
<action name="actionCut">
<property name="icon">
<iconset theme="edit-cut">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Cut</string>
</property>
<property name="toolTip">
<string>Cut Text</string>
</property>
<property name="shortcut">
<string>Ctrl+X</string>
</property>
</action>
<action name="actionPaste">
<property name="icon">
<iconset theme="edit-paste">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Paste</string>
</property>
<property name="toolTip">
<string>Paste Text</string>
</property>
<property name="shortcut">
<string>Ctrl+V</string>
</property>
</action>
<action name="actionUndo">
<property name="icon">
<iconset theme="edit-undo">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Undo</string>
</property>
<property name="toolTip">
<string>Undo Changes</string>
</property>
<property name="shortcut">
<string>Ctrl+Z</string>
</property>
</action>
<action name="actionRedo">
<property name="icon">
<iconset theme="edit-redo">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Redo</string>
</property>
<property name="toolTip">
<string>Redo Changes</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+Z</string>
</property>
</action>
<action name="actionAbout">
<property name="icon">
<iconset theme="help-about"/>
</property>
<property name="text">
<string>About</string>
</property>
</action>
<action name="actionPrint">
<property name="icon">
<iconset theme="document-print"/>
</property>
<property name="text">
<string>Print</string>
</property>
<property name="toolTip">
<string>Print Document</string>
</property>
<property name="shortcut">
<string>Ctrl+P</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>