I would like to write a simple text editor. I would like it to be GUI-based, and written using Qt.
Qt provides the class QTextEdit
, which is a pretty fully-featured text editor (it supports ctrl+c
, x
, and z
, text highlighting, and moving the cursor around left-right-top-down). I would like to write something similar, but I would like to implement all this functionality from scratch.
So my requirements are:
- A simple window.
- Be able to read text from a user, and output the prepared file to the screen.
- The user must be able to access the text for editing.
I have taken a look at the source code to qtextedit.cpp
from the official Qt site to get some idea of how this was implemented. Unfortunately I am not experienced enough to understand it (there are nearly 4000 lines of code, and many macros).
How might I make some progress with my project?
I think you are best off taking a look at some of the code here: http://kde-apps.org/index.php?xcontentmode=241
Take a look at the code, try coding something of your own, and realise that Qt requires one to know C++ fairly well. You will, at the very least, need to understand classes and pointers. If that feels manageable, then the best way is generally to try: you'll run into some problems, and then you can see how other people have solved that problem.
I think the most specific advice I can give is to write tests for everything that looks like it can reasonably be tested, that may save you some debugging time. Other than that, there's little more to say without example code and a more specific question.
This is a major job of work. If you want a decent editor, you are more or less forced to start with the Scintilla edit control, because it's simply the best (and it comes with a Lesser General Public Licence). But then you have to implement an editor on top of it. You don't really want to do this (trust me), so you have to use an existing open-source editor. The best of these is probably Notepad++, which has way more than 4000 lines of code. It comes with a General Public Licence, which is not compatible with the project I'm currently working on, so I ended up using the SciTE editor instead. I have implemented an interface for this which lets me embed it in a Qt application, and it works like a charm. In the next two or three months I hope to get this interface accepted by the SciTE community and incorporated into the official release. Meanwhile, if you are not bound by the licensing requirements, you might want to look at QScintilla.
In the Qt Demo application there is an MDI text editor that teaches a lot (http://doc.qt.nokia.com/latest/mainwindows-mdi.html).
精彩评论