1、基于QT的文本编辑器程序源代码 文本编辑器代码 textedit.proTEMPLATE = appTARGET = texteditCONFIG += qt warn_onHEADERS = textedit.hSOURCES = textedit.cpp main.cppRESOURCES += textedit.qrcbuild_all:!build_pass CONFIG -= build_all CONFIG += release# installtarget.path = $QT_INSTALL_DEMOS/texteditsources.files = $SOURCES $HEA
2、DERS $RESOURCES *.pro *.html *.doc imagessources.path = $QT_INSTALL_DEMOS/texteditINSTALLS += target sources textedit.h#ifndef TEXTEDIT_H#define TEXTEDIT_H#include #include #include QT_FORWARD_DECLARE_CLASS(QAction)QT_FORWARD_DECLARE_CLASS(QComboBox)QT_FORWARD_DECLARE_CLASS(QFontComboBox)QT_FORWARD_
3、DECLARE_CLASS(QTextEdit)QT_FORWARD_DECLARE_CLASS(QTextCharFormat)QT_FORWARD_DECLARE_CLASS(QMenu)class TextEdit : public QMainWindow Q_OBJECTpublic: TextEdit(QWidget *parent = 0);protected: virtual void closeEvent(QCloseEvent *e);private: void setupFileActions(); void setupEditActions(); void setupTe
4、xtActions(); bool load(const QString &f); bool maybeSave(); void setCurrentFileName(const QString &fileName);private slots: void fileNew(); void fileOpen(); bool fileSave(); bool fileSaveAs(); void filePrint(); void filePrintPreview(); void filePrintPdf(); void textBold(); void textUnderline(); void
5、 textItalic(); void textFamily(const QString &f); void textSize(const QString &p); void textStyle(int styleIndex); void textColor(); void textAlign(QAction *a); void currentCharFormatChanged(const QTextCharFormat &format); void cursorPositionChanged(); void clipboardDataChanged(); void about(); void
6、 printPreview(QPrinter *);private: void mergeFormatOnWordOrSelection(const QTextCharFormat &format); void fontChanged(const QFont &f); void colorChanged(const QColor &c); void alignmentChanged(Qt:Alignment a); QAction *actionSave, *actionTextBold, *actionTextUnderline, *actionTextItalic, *actionText
7、Color, *actionAlignLeft, *actionAlignCenter, *actionAlignRight, *actionAlignJustify, *actionUndo, *actionRedo, *actionCut, *actionCopy, *actionPaste; QComboBox *comboStyle; QFontComboBox *comboFont; QComboBox *comboSize; QToolBar *tb; QString fileName; QTextEdit *textEdit;#endif main.cpp#include tex
8、tedit.h#include int main( int argc, char * argv ) Q_INIT_RESOURCE(textedit); QApplication a( argc, argv ); TextEdit mw; mw.resize( 700, 800 ); mw.show(); return a.exec(); textedit.cpp#include textedit.h#include #include #include #include #include #include #include #include #include #include #include
9、 #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef Q_WS_MACconst QString rsrcPath = :/images/mac;#elseconst QString rsrcPath = :/images/win;#endifTextEdit:TextEdit(QWidget *parent) : QMainWindow(parent) this-setWindowTitle(QSt
10、ring(文本编辑器); QTextCodec:setCodecForCStrings(QTextCodec:codecForName(UTF-8); setupFileActions(); setupEditActions(); setupTextActions(); QMenu *helpMenu = new QMenu(帮助, this); menuBar()-addMenu(helpMenu); helpMenu-addAction(关于, this, SLOT(about(); helpMenu-addAction(关于 &Qt, qApp, SLOT(aboutQt(); text
11、Edit = new QTextEdit(this); connect(textEdit, SIGNAL(currentCharFormatChanged(const QTextCharFormat &), this, SLOT(currentCharFormatChanged(const QTextCharFormat &); connect(textEdit, SIGNAL(cursorPositionChanged(), this, SLOT(cursorPositionChanged(); setCentralWidget(textEdit); textEdit-setFocus();
12、 setCurrentFileName(QString(); fontChanged(textEdit-font(); colorChanged(textEdit-textColor(); alignmentChanged(textEdit-alignment(); connect(textEdit-document(), SIGNAL(modificationChanged(bool), actionSave, SLOT(setEnabled(bool); connect(textEdit-document(), SIGNAL(modificationChanged(bool), this,
13、 SLOT(setWindowModified(bool); connect(textEdit-document(), SIGNAL(undoAvailable(bool), actionUndo, SLOT(setEnabled(bool); connect(textEdit-document(), SIGNAL(redoAvailable(bool), actionRedo, SLOT(setEnabled(bool); setWindowModified(textEdit-document()-isModified(); actionSave-setEnabled(textEdit-do
14、cument()-isModified(); actionUndo-setEnabled(textEdit-document()-isUndoAvailable(); actionRedo-setEnabled(textEdit-document()-isRedoAvailable(); connect(actionUndo, SIGNAL(triggered(), textEdit, SLOT(undo(); connect(actionRedo, SIGNAL(triggered(), textEdit, SLOT(redo(); actionCut-setEnabled(false);
15、actionCopy-setEnabled(false); connect(actionCut, SIGNAL(triggered(), textEdit, SLOT(cut(); connect(actionCopy, SIGNAL(triggered(), textEdit, SLOT(copy(); connect(actionPaste, SIGNAL(triggered(), textEdit, SLOT(paste(); connect(textEdit, SIGNAL(copyAvailable(bool), actionCut, SLOT(setEnabled(bool); c
16、onnect(textEdit, SIGNAL(copyAvailable(bool), actionCopy, SLOT(setEnabled(bool); connect(QApplication:clipboard(), SIGNAL(dataChanged(), this, SLOT(clipboardDataChanged(); QString initialFile = 文本编辑器; const QStringList args = QCoreApplication:arguments(); if (args.count() = 2) initialFile = args.at(1
17、); if (!load(initialFile) fileNew();void TextEdit:closeEvent(QCloseEvent *e) if (maybeSave() e-accept(); else e-ignore();void TextEdit:setupFileActions() QToolBar *tb = new QToolBar(this); tb-setWindowTitle(tr(File Actions); addToolBar(tb); QMenu *menu = new QMenu(&文件, this); menuBar()-addMenu(menu)
18、; QAction *a; a = new QAction(QIcon(rsrcPath + /filenew.png), &新建, this); a-setShortcut(QKeySequence:New); connect(a, SIGNAL(triggered(), this, SLOT(fileNew(); tb-addAction(a); menu-addAction(a); a = new QAction(QIcon(rsrcPath + /fileopen.png), &打开, this); a-setShortcut(QKeySequence:Open); connect(a
19、, SIGNAL(triggered(), this, SLOT(fileOpen(); tb-addAction(a); menu-addAction(a); menu-addSeparator(); actionSave = a = new QAction(QIcon(rsrcPath + /filesave.png), &保存, this); a-setShortcut(QKeySequence:Save); connect(a, SIGNAL(triggered(), this, SLOT(fileSave(); a-setEnabled(false); tb-addAction(a)
20、; menu-addAction(a); a = new QAction(&另存为., this); connect(a, SIGNAL(triggered(), this, SLOT(fileSaveAs(); menu-addAction(a); menu-addSeparator(); a = new QAction(QIcon(rsrcPath + /fileprint.png), &打印, this); a-setShortcut(QKeySequence:Print); connect(a, SIGNAL(triggered(), this, SLOT(filePrint(); t
21、b-addAction(a); menu-addAction(a); a = new QAction(QIcon(rsrcPath + /fileprint.png), &打印预览, this); connect(a, SIGNAL(triggered(), this, SLOT(filePrintPreview(); menu-addAction(a); a = new QAction(QIcon(rsrcPath + /exportpdf.png), &转换为 PDF., this); a-setShortcut(Qt:CTRL + Qt:Key_D); connect(a, SIGNAL
22、(triggered(), this, SLOT(filePrintPdf(); tb-addAction(a); menu-addAction(a); menu-addSeparator(); a = new QAction(&退出, this); a-setShortcut(Qt:CTRL + Qt:Key_Q); connect(a, SIGNAL(triggered(), this, SLOT(close(); menu-addAction(a);void TextEdit:setupEditActions() QToolBar *tb = new QToolBar(this); tb
23、-setWindowTitle(tr(Edit Actions); addToolBar(tb); QMenu *menu = new QMenu(&编辑, this); menuBar()-addMenu(menu); QAction *a; a = actionUndo = new QAction(QIcon(rsrcPath + /editundo.png),&撤销, this); a-setShortcut(QKeySequence:Undo); tb-addAction(a); menu-addAction(a); a = actionRedo = new QAction(QIcon
24、(rsrcPath + /editredo.png), &恢复, this); a-setShortcut(QKeySequence:Redo); tb-addAction(a); menu-addAction(a); menu-addSeparator(); a = actionCut = new QAction(QIcon(rsrcPath + /editcut.png), &剪切, this); a-setShortcut(QKeySequence:Cut); tb-addAction(a); menu-addAction(a); a = actionCopy = new QAction
25、(QIcon(rsrcPath + /editcopy.png), &复制, this); a-setShortcut(QKeySequence:Copy); tb-addAction(a); menu-addAction(a); a = actionPaste = new QAction(QIcon(rsrcPath + /editpaste.png), &粘贴, this); a-setShortcut(QKeySequence:Paste); tb-addAction(a); menu-addAction(a); actionPaste-setEnabled(!QApplication:
26、clipboard()-text().isEmpty();void TextEdit:setupTextActions() QToolBar *tb = new QToolBar(this); tb-setWindowTitle(tr(Format Actions); addToolBar(tb); QMenu *menu = new QMenu(&格式, this); menuBar()-addMenu(menu); actionTextBold = new QAction(QIcon(rsrcPath + /textbold.png), &加粗, this); actionTextBold
27、-setShortcut(Qt:CTRL + Qt:Key_B); QFont bold; bold.setBold(true); actionTextBold-setFont(bold); connect(actionTextBold, SIGNAL(triggered(), this, SLOT(textBold(); tb-addAction(actionTextBold); menu-addAction(actionTextBold); actionTextBold-setCheckable(true); actionTextItalic = new QAction(QIcon(rsr
28、cPath + /textitalic.png), &斜体, this); actionTextItalic-setShortcut(Qt:CTRL + Qt:Key_I); QFont italic; italic.setItalic(true); actionTextItalic-setFont(italic); connect(actionTextItalic, SIGNAL(triggered(), this, SLOT(textItalic(); tb-addAction(actionTextItalic); menu-addAction(actionTextItalic); act
29、ionTextItalic-setCheckable(true); actionTextUnderline = new QAction(QIcon(rsrcPath + /textunder.png), &下划线, this); actionTextUnderline-setShortcut(Qt:CTRL + Qt:Key_U); QFont underline; underline.setUnderline(true); actionTextUnderline-setFont(underline); connect(actionTextUnderline, SIGNAL(triggered(), this, SLOT(textUn
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1