Text Editor with Qt
The task is to create a simple, plain text editor using Qt Creator. This will involve the creation of a simple GUI and a Document class for maintaining the data saved to file.
Note: the term ‘Document’ is used in two ways: as ‘document’ (no text styling) in a generic sense referring to the current content of the editor , and as Document (with styling ) indicating a reference to the class or an instance of the class.
The final program must meet the requirements down below:
Requirements Marks
Document class
10
1. A class called Document must be created that represents a document saved/loaded from file.
2. Document class must include the following data members:
a. file name – the absolute path of the file.
b. text – the complete text of the file.
3. The Document class must have a constructor that takes two QString arguments (const
references), the file name and text, respectively. It must not have any other constructor.
4. The Document class must have accessors for both data members.
5. The Document class must have a mutator member for the text member.
6. The Document class must not have a mutator for the file name member.
7. The text in a Document object must always match the text saved to file.
1 2
3
1 1 1 1
Editor Window
17
8. The window class must be called EditorWindow.
9. The window must have a QMenuBar (menuBar), with a ‘File’ menu containing ‘New’, ‘Open…’,
‘Save’, ‘Save As…’ and ‘Exit’ options.
10. The EditorWindow interface must consist of a single ‘Plain Text Edit’ widget
a. which must correctly resize with the window.
11. The EditorWindow must use automatically connected slots for actioning menu items. See: Qt
documentation for QMenuBar, QMenu, QAction, and the Auto-Connect documentation: http://doc.qt.io/qt-5/designer-using-a-ui-file.html#widgets-and-dialogs-with-auto-connect (Note: you do not need to call QMetaObject::connectSlotsByName(this);. If using Qt Designer, this should be easily done through the form editor UI.)
12. The EditorWindow must have one (and only one) document/file open at a time:
a. the relevant information must be loaded into a data member of type Document when a
file is opened by the user.
b. The Document member must be the only member variable (aside from the automatic ui
member).
13. If a document is not yet saved, the editor (title bar or status bar) must display the name
‘
1 1
1 1
1 1
1 1
14. When a file is open:
a. its text must be displayed in the Plain Text Edit widget, and
b. its file name must be displayed in either the title bar or the status bar.
Note: If the status bar is unused, it must be removed.
15. The Document member and the on-screen file name display must be updated every time the file
name changes.
16. The editor must visually inform the user when the file has been edited and not saved.
Hint: QPlainTextEdit has a textChanged signal.
17. The ‘Exit’ menu item, with any platform exit button (e.g., the red x in the corner of the window),
must operate the same and quit the application.
18. The editor must display an exit confirmation box (e.g., ‘The file is not saved. Are you sure you
want to exit?’) if the user attempts to exit without saving a modified file. Hint: see QMessageBox documentation.
The exit confirmation box:
a. must provide a ‘Save’ option that will save the file before quitting the application
b. must provide a ‘Don’t Save’ option that will quit the application without saving the file
c. must provide a ‘Cancel’ option that returns the user to the editor
1 1
1 1 1 1
1 1 1
Creating New Documents
5
19. The ‘New’ menu item must clear the display and set the file name to ‘
20. The ‘New’ menu item action must prompt the user for confirmation if the file has been modified.
The new file confirmation box:
a. must provide a ‘Save’ option that will save the file before resetting the document
b. must provide a ‘Don’t Save’ option that will reset the document without saving the file
c. must provide a ‘Cancel’ option that returns the user to the editor without changes
Opening and Saving Files
1 1
1 1 1
8
21. The ‘Open’, ‘Save’, and ‘Save As…’ menu action slots must use the static members of QFileDialog (getOpenFileName and getSaveFileName, respectively) with:
a. an appropriate caption (something about opening or saving a text document), and
b. appropriate filter (file extension ‘.txt’).
Note: The ‘Save’ action will only display a dialog if the Document is currently unsaved.
22. The ‘Open’ action must read the contents of a file into the application as a Document object and display it in the EditorWindow.
23. The ‘Open’ action will assume the file is UTF8 encoded.
24. The ‘Save’/‘Save As…’ actions must write the contents of the plain text editor to file:
a. The ‘Save As…’ action will always require the user to specify a file in which to write.
b. The ‘Save’ action will only require the user to specify a file if the document is currently
unsaved.
25. The ‘Save’/’Save As…’ action must ensure the file is saved in UTF8 encoding.
1
1 1
1
1 1 1
1
Compiles and Runs 10