一用Qt4实现的文本编辑器1. 新建,打开,保存文件2.基本的文本编辑功能(复制,粘贴,剪切等)3.不同风格4.改变文字的大小,颜色,字体等界面如下:二工程文件1. /* src.pro*/TEMPLATE = appCONFIG += warn_on \thread \qt \qtestlibTARGET = texteditorDESTDIR = ../binSOURCES += CMainWindow.cpp \MainWindow.cpp \CFindDialog.cppHEADERS += CFindDialog.h \CMainWindow.h \ui_finddialog.h \ui_MainWindow.hFORMS += FindDialog.ui \MainWindow.uiRESOURCES += MainWindow.qrcQT += qt3support2. /*CMainWindow.cpp*/#include "CMainWindow.h"#include "CFindDialog.h"#include <QDir>#include <QFileInfo>CMainWindow::CMainWindow(QWidget *parent): QMainWindow(parent){setupUi(this);iniStatusBar();iniConnect();fontColorAct = new QAction(QPixmap(32, 32), tr("改变字体颜色"), this);styleToolBar = new QToolBar(this);styleToolBar->setObjectName(QString::fromUtf8("styleToolBar"));styleToolBar->setOrientation(Qt::Horizontal);this->addToolBar(Qt::TopToolBarArea, styleToolBar);menu_2->addAction(fontColorAct);styleToolBar->addAction(fontColorAct);show();connect(fontColorAct, SIGNAL(triggered()), this, SLOT(changeColor())); }void CMainWindow::iniConnect(){connect(textEdit, SIGNAL(cursorPositionChanged()),this, SLOT(doCursorChanged()));connect(textEdit->document(), SIGNAL(contentsChanged()),this, SLOT(doModified()));connect(actionAbout, SIGNAL(triggered()), this, SLOT(doAbout()));connect(actionHelp, SIGNAL(triggered()), this, SLOT(doHelp()));connect(actionQuit, SIGNAL(triggered()), this, SLOT(doQuit()));connect(actionOpen, SIGNAL(triggered()), this, SLOT(doOpen()));connect(actionAll, SIGNAL(triggered()), this, SLOT(doAll()));connect(actionASave, SIGNAL(triggered()), this, SLOT(doASave()));connect(actionClose, SIGNAL(triggered()), this, SLOT(doClose()));connect(actionCopy, SIGNAL(triggered()), this, SLOT(doCopy()));connect(actionUndo, SIGNAL(triggered()), this, SLOT(doUndo()));connect(actionRedo, SIGNAL(triggered()), this, SLOT(doRedo()));connect(actionSave, SIGNAL(triggered()), this, SLOT(doSave()));connect(actionFind, SIGNAL(triggered()), this, SLOT(doFind()));connect(actionNew, SIGNAL(triggered()), this, SLOT(do New()));connect(actionPaste, SIGNAL(triggered()), this, SLOT(doPaste()));connect(actionCut, SIGNAL(triggered()), this, SLOT(doCut()));connect(actionWindows, SIGNAL(triggered()), this, SLOT(doWindows()));connect(actionWindowsXp, SIGNAL(triggered()), this,SLOT(doWindowsXp()));connect(actionCDE, SIGNAL(triggered()), this, SLOT(doCDE()));connect(actionMotif, SIGNAL(triggered()), this, SLOT(doMotif()));connect(actionPlastique, SIGNAL(triggered()), this, SLOT(doPlastique()));connect(actionFont, SIGNAL(triggered()), this, SLOT(doFont()));}void CMainWindow::changeColor(){QColor color =QColorDialog::getColor(fontColor, this);if(color.isValid()) {fontColor = color;updateColor();}textEdit->setTextColor(fontColor);}void CMainWindow::doModified(){setWindowModified(textEdit->document()->isModified());label2->setText("正在修改");}void CMainWindow::doAbout(){QMessageBox::about(this,"关于","TextEditor 1.0\nBy Roc\nJune-15th-2009"); }void CMainWindow::doHelp(){QString info;QFile file("help.txt");if (!file.open(QFile::ReadOnly | QFile::Text)){QMessageBox::warning(this,"打开帮助文档",QString("无法读取帮助文件:\n%1").arg(file.errorString()));return ;}else{QTextStream in(&file);QMessageBox box(this);box.setWindowTitle("帮助");box.setIcon(QMessageBox::Information);box.setText("TextEditor简介:");box.setStandardButtons(QMessageBox::Ok);box.setDetailedText(in.readAll());box.exec();return ;}}void CMainWindow::iniStatusBar()label1 = new QLabel;label1->setMinimumSize(200, 25);label1->setFrameShadow(QFrame::Sunken);label1->setFrameShape(QFrame::WinPanel);label2 = new QLabel;label2->setMinimumSize(200, 25);label2->setFrameShadow(QFrame::Sunken);label2->setFrameShape(QFrame::WinPanel);statusbar->addWidget(label1);statusbar->addWidget(label2);}void CMainWindow::doASave(){QString filename =QFileDialog::getSaveFileName(this,"另存为", currentFile, "text files *.txt");if (!filename.isEmpty()){saveFile(filename);}}void CMainWindow::doCut(){textEdit->cut();}void CMainWindow::doCopy(){textEdit->copy();}void CMainWindow::doClose(){maybeSave();textEdit->setVisible(false);void CMainWindow::doNew(){maybeSave();isUntited = true;currentFile = "Untited";setWindowTitle(currentFile + "[*]");textEdit->clear();textEdit->setVisible(true);setWindowModified(false);}void CMainWindow::doOpen(){QString fileName = QFileDialog::getOpenFileName(this, "打开文本", QDir::currentPath(), "text files *.txt");if (!fileName.isEmpty()){maybeSave();if (loadFile(fileName)){label2->setText("已读取");}}textEdit->setVisible(true);}void CMainWindow::doUndo(){textEdit->undo();}void CMainWindow::doRedo(){textEdit->redo();}void CMainWindow::doPaste(){textEdit->paste();}void CMainWindow::doAll(){textEdit->selectAll();}void CMainWindow::doSave(){if (isUntited){doASave();}else{saveFile(currentFile);}}void CMainWindow::doFind(){CFindDialog *finddlg = new CFindDialog(0);finddlg->bolding(textEdit);finddlg->show();}void CMainWindow::doQuit(){doClose();delete textEdit;textEdit = NULL;qApp->quit();}bool CMainWindow::loadFile(const QString &fileName){QFile file(fileName);if (!file.open(QFile::ReadOnly | QFile::Text)){QMessageBox::warning(this,"读取文件",QString("无法读取文件%1:\n%2").arg(fileName).arg(file.errorString()));return false;}else{QTextStream in(&file);QApplication::setOverrideCursor(Qt::WaitCursor);textEdit->setText(in.readAll());QApplication::restoreOverrideCursor();setCurrentFile(fileName);return true;}}void CMainWindow::setCurrentFile(const QString &fileName) {currentFile = QFileInfo(fileName).canonicalFilePath();isUntited = false;setWindowTitle(currentFile + "[*]");textEdit->document()->setModified(false);setWindowModified(false);}bool CMainWindow::saveFile(const QString fileName){QFile file(fileName);if (!file.open(QFile::WriteOnly | QFile::Text)){QMessageBox::warning(this,"保存文件",QString("无法保存文件%1:\n%2").arg(fileName).arg(file.errorString()));return false;}QTextStream out(&file);QApplication::setOverrideCursor(Qt::WaitCursor);out<<textEdit->toPlainText();QApplication::restoreOverrideCursor();setCurrentFile(fileName);label2->setText("已保存");return true;}void CMainWindow::doWindows(){qApp->setStyle("windows");}void CMainWindow::doWindowsXp(){qApp->setStyle("windowsxp");}void CMainWindow::doMotif(){qApp->setStyle("motif");}void CMainWindow::doCDE(){qApp->setStyle("cde");}void CMainWindow::doPlastique(){qApp->setStyle("plastique");}void CMainWindow::doFont(){bool ok;QFont font = QFontDialog::getFont(&ok,textEdit->font(),this,"字体对话框");if (ok){textEdit->setFont(font);}}void CMainWindow::maybeSave(){if (textEdit->document()->isModified()){QMessageBox box;box.setWindowTitle("警告");box.setIcon(QMessageBox::Warning);box.setText("文档未保存,是否保存?");box.setStandardButtons(QMessageBox::Yes | QMessageBox::No);if (box.exec() == QMessageBox::Yes){doSave();}}}void CMainWindow::doCursorChanged(){int pageNum = textEdit->document()->pageCount();const QTextCursor cursor = textEdit->textCursor();int colNum = cursor.columnNumber();int rowNum = textEdit->document()->blockCount();label1->setText(QString("%1页%2行%3列").arg(pageNum).arg(rowNum).arg(colNum));}void CMainWindow::updateColor(){QPixmap pixmap(32, 32);QPainter painter(&pixmap);painter.fillRect(0, 0, 32, 32, fontColor);QColor lighter = fontColor.light();painter.setPen(lighter);QPoint lightFrame[] = {QPoint(0, 31), QPoint(0, 0), QPoint(31, 0) }; painter.drawPolyline(lightFrame, 3);painter.setPen(fontColor.dark());QPoint darkFrame[] = {QPoint(1, 31), QPoint(31, 31), QPoint(31, 1) }; painter.drawPolyline(darkFrame, 3);painter.end();fontColorAct->setIcon(pixmap);}。