很早以前就有了寫一個相似Windows記事本的想法,加上最近也恰好在學編譯原理,因此就想把二者結合起來,因而就打算結合MarkDown,開發一款MarkDown編輯器。
不過因爲我以前一直使用的是Java語言居多,對c++並不熟悉,因此一些糟糕的代碼風格和規範還望各位大佬諒解!html
LightMD 即一款基於QT實現的markdown編輯器,固然也能夠做爲代碼編輯器,因爲時間與我的能力等緣由,目前實現的功能很是有限!c++
主要包括:git
支持語法高亮(目前支持C/C++)github
支持MarkDown預覽web
代碼行數、文本信息統計等windows
其它一些基本的文件處理相關功能markdown
好了直接上圖吧app
本項目主要目的在於學習qt相關的windows開發,其中主要有如下幾個模塊:編輯器
最外面固然是_QMainWindow_, 而後從上到下依次是函數
menubar的簡單示例以下:
QMenu *menuFile = menuBar()->addMenu(tr("&File")); QAction *itemNew = new QAction(tr("&New"), this); itemNew->setStatusTip(tr("Create a new file")); connect(itemNew, &QAction::triggered, this, &Home::newFile); menuFile->addAction(itemNew);
其中QMenu就是最外面顯示的menu,即鼠標不點擊就能夠看見的那個menu;QAction則是QMenu上衆多選項之一;而後是調用connect函數爲QAction設置點擊事件。
中間主體則是QSplitter,
QSplitter *centralSplitter = new QSplitter(this); setCentralWidget(centralSplitter); centralSplitter->addWidget(codeEditor); centralSplitter->addWidget(preview);
首先new一個QSplitter,而後將其設置爲中間組件,而後在QSplitter上再添加兩個組件,分別爲代碼編輯區域和markdown預覽區域的組件。
這兩個區域在文章下面將有具體講解。
底部則是statusBar:
label = new QLabel("LightMD is ready!"); textType = new QLabel("Plain Text"); codeLength = new QLabel("Length:652"); codeLines = new QLabel("Lines:54"); statusBar()->addWidget(label, 1); statusBar()->addPermanentWidget(textType); statusBar()->addPermanentWidget(codeLength); statusBar()->addPermanentWidget(codeLines);
label用來顯示正常的提示消息;textType用來顯示當前的文本類型,如markdown或c++等;codeLength和codeLines就不用過多解釋了吧。
其中代碼編輯框我糾結了半天,用QPlainTextEdit好呢,仍是QTextEdit好???其中StackOverflow上一高贊回答以下:
QPlainTextEdit is an advanced viewer/editor supporting plain text. It is optimized to handle large documents and to respond quickly to user input.
QPlainText uses very much the same technology and concepts as QTextEdit, but is optimized for plain text handling.
QPlainTextEdit works on paragraphs and characters. A paragraph is a formatted string which is word-wrapped to fit into the width of the widget. By default when reading plain text, > one newline signifies a paragraph. A document consists of zero or more paragraphs. Paragraphs are separated by hard line breaks. Each character within a paragraph has its
own attributes, for example, font and color.
簡單點說,就是QPlainTextEdit對普通文本的支持度特別高,也就是很方便,不過一些複雜功能卻不能實現;而QTextEdit是一個更加劇量級的組件,支持各類複雜功能,不過一些簡單的功能可能沒有QPlainTextEdit使用的那麼方便。
二者我都簡單試用後,發現仍是QPlainTextEdit用着比較方便,因而就決定採用QPlainTextEdit了。
而後關於代碼框和代碼行數的實現,QT的官方demo裏面好像有現成的(不得不說,qt的demo是真的多!)。
因此,這裏的實現我就不解釋啥了。
這裏官方也有個markdown的demo,下載就好了。不過要注意的是,這裏因爲用到了web引擎,因此這裏必須使用vs來編譯運行,安裝vs環境這裏不懂的仍是自行百度吧。
官方demo中好像實現的都挺全的,我只是作了個小修改,而後就轉移到LightMD來了。
其中markdown預覽流程是先將markdown內容轉換爲對應的html內容,而後web引擎來顯示HTML頁面。
其中轉換官方也所有爲咱們作好了。
因爲時間關係,固然是直接套用了。不過之後有空的話,本身再去實現一下吧。
最後,LightMd項目地址:LightMD