一 用 Qt4实现 的 文本 编辑器
1. 新建 ,打开 ,保存 文件
2.基本 的 文本 编辑功能 (复制 , 粘贴 , 剪切 等 )
3.不同 风格
4.改变 文字 的 大小, 颜色 ,字体 等
界面 如下 :
二 工程 文件
1. /* src.pro*/
TEMPLATE = app
CONFIG += warn_on \
thread \
qt \
qtestlib
TARGET = texteditor
DESTDIR = ../bin
SOURCES += CMainWindow.cpp \
MainWindow.cpp \
CFindDialog.cpp
HEADERS += CFindDialog.h \
CMainWindow.h \
ui_finddialog.h \
ui_MainWindow.h
FORMS += FindDialog.ui \
MainWindow.ui
RESOURCES += MainWindow.qrc
QT += qt3support
2. /*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(doNew()));
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);
}