TextEdit::TextEdit(QWidget *parent)
: QMainWindow(parent)
{
setToolButtonStyle(Qt::ToolButtonFollowStyle);
setupFileActions();
setupEditActions();
setupTextActions();

{
QMenu *helpMenu = new QMenu(tr("Help"), this);
menuBar()->addMenu(helpMenu);
helpMenu->addAction(tr("About"), this, SLOT(about()));
helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt()));
}

textEdit = new QTextEdit(this);
connect(textEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)),
this, SLOT(currentCharFormatChanged(QTextCharFormat)));
connect(textEdit, SIGNAL(cursorPositionChanged()),
this, SLOT(cursorPositionChanged()));

setCentralWidget(textEdit);
textEdit->setFocus();
setCurrentFileName(QString());

QFont textFont("Helvetica");
textFont.setStyleHint(QFont::SansSerif);
textEdit->setFont(textFont);
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, 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->document()->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);
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)));
connect(textEdit, SIGNAL(copyAvailable(bool)), actionCopy, SLOT(setEnabled(bool)));

#ifndef QT_NO_CLIPBOARD
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(clipboardDataChanged()));
#endif

QString initialFile = ":/example.html";
const QStringList args = QCoreApplication::arguments();
if (args.count() == 2)
initialFile = args.at(1);

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(tr("&File"), this);
menuBar()->addMenu(menu);

QAction *a;

QIcon newIcon = QIcon::fromTheme("document-new", QIcon(rsrcPath + "/filenew.png"));
a = new QAction( newIcon, tr("&New"), this);
a->setPriority(QAction::LowPriority);
a->setShortcut(QKeySequence::New);
connect(a, SIGNAL(triggered()), this, SLOT(fileNew()));
tb->addAction(a);
menu->addAction(a);

a = new QAction(QIcon::fromTheme("document-open", QIcon(rsrcPath + "/fileopen.png")),
tr("&Open..."), this);
a->setShortcut(QKeySequence::Open);
connect(a, SIGNAL(triggered()), this, SLOT(fileOpen()));
tb->addAction(a);
menu->addAction(a);

menu->addSeparator();

actionSave = a = new QAction(QIcon::fromTheme("document-save", QIcon(rsrcPath + "/filesave.png")),
tr("&Save"), this);
a->setShortcut(QKeySequence::Save);
connect(a, SIGNAL(triggered()), this, SLOT(fileSave()));
a->setEnabled(false);
tb->addAction(a);
menu->addAction(a);

a = new QAction(tr("Save &As..."), this);
a->setPriority(QAction::LowPriority);
connect(a, SIGNAL(triggered()), this, SLOT(fileSaveAs()));
menu->addAction(a);
menu->addSeparator();
}

Qt实现编辑器_Qt

Qt实现编辑器_工具栏_02