(please post answer that actually run) (c++) You need to write a program similar
ID: 3886134 • Letter: #
Question
(please post answer that actually run)
(c++) You need to write a program similar to NOTEPAD. The program will allow the user to type anything he/she wants. Letters, digits, special characters. In as many rows as he/she wants.
The user then can save the text he/she wrote, then later on load it to continue working on. Your program should allow the user to do the following:
-Navigate the screen using the arrow keys (go up if there is text above the cursor and down if there is text below cursor, right if there is text on the right side of cursor and left if there is text on left side of cursor: linked list)
-Cursor can not go outside screen.
-User can save the document on disk in text format
-User can load item from a file into the editor. So if I have my own TEXT file, I can load it into your editor to read it and edit it
-User can manipulate text by adding, deleting, copy/pasting....etc.
-Display a menu with the options/commands the user can use to operate your editor
**Make sure you use your own code, do not use any reserved functions from external libraries.
Use this code to manipulate the interface:
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
void gotoxy(int x, int y) {
COORD pos = { x, y };
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(output, pos);
}
Explanation / Answer
main.cpp
#include "notepad.h"
#include<QApplication>
int main(int argc, char *argv)
{
QApplication nPad(argc, argv);
notepad *window = new notepad();
window->show();
return nPad.exec();
}
notepad.cpp
#include "notepad.h"
notepad::notepad()
{
tEditor = new QTextEdit;
quitButton = new QPushButton;
openAction = new QAction(tr("Open"), this);
saveAction = new QAction(tr("Save"), this);
quitAction = new QAction(tr("Quit"), this);
QObject::connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(Quit()));
fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(quitAction);
setWindowTitle(tr("Notepad"));
setCentralWidget(tEditor);
}
void notepad::open()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Select a file... "), "", tr("Text Files (*.txt);;c++ source files (*.cpp);;All Files (*.*)"));
if(fileName!="")
{
QFile fIn(fileName);
if(!fIn.open(QIODevice::ReadOnly))
{
QMessageBox::critical(this, tr("Error"), tr("File could not opened"));
return;
}
QTextStream in(&fIn);
tEditor->setText(in.readAll());
fIn.close();
}
else
{
QMessageBox confirm;
confirm.setText(tr("No file has bee selected"));
confirm.setStandardButtons(QMessageBox::Retry | QMessageBox::Ok);
confirm.setDefaultButton(QMessageBox::Ok);
if(confirm.exec()==QMessageBox::Retry)
{
open();
}
}
}
void notepad::save()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "", tr("Text Files (*.txt);;C++ Source Files (*.cpp);;All files(*.*)"));
QFile fOut(fileName);
if(fileName!="")
{
if(fOut.open(QIODevice::WriteOnly))
{
QTextStream out(&fOut);
out<<tEditor->toPlainText();
out.flush();
fOut.close();
}
else
{
QMessageBox::critical(this, tr("Error !!!"), tr("File could not be saved"));
return;
}
}
}
void notepad::Quit()
{
QMessageBox confirmQuit;
confirmQuit.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
confirmQuit.setDefaultButton(QMessageBox::No);
confirmQuit.setText(tr("Do you really want to quit ?"));
confirmQuit.setWindowTitle(tr("Close the Notepad"));
if(confirmQuit.exec() == QMessageBox::Yes)
{
qApp->quit();
}
}
notepad.h
#include<QtGui>
class notepad : public QMainWindow
{
Q_OBJECT;
public:
notepad();
private:
QTextEdit *tEditor;
QPushButton *quitButton;
QAction *openAction;
QAction *saveAction;
QAction *quitAction;
QMenu *fileMenu;
private slots:
void open();
void save();
void Quit();
};
Hope This Helps
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.