在前面几节,从学习主窗口QMainWindow开始都是为了简易的文本编辑器的开发,在QMainWindow中学习了菜单栏、状态栏以及工具栏,这正是构成一个简易文本编辑器所需的界面功能。
- 文本编辑器的主体界面如上,在之前学习QMainWindow时候也完成过部分组件的添加,在私底下我又将其余部分补充到代码里面,最终就形成这样的一个界面,包含菜单栏每个菜单及其快捷方式、工具栏里面的快捷操作,并通过资源文件对每个按钮设置了图标,然后就是正中间的编辑组件,选用三种常用编辑组件中的QPlainTextEdit,因为我们需要多行编辑的同时不需要富文本编辑;最下面的部分就是QMainWindow的状态栏,可以通过两个QStatusBar的成员函数分别往组件的左边部分和右边部分添加任意组件。
- 文本编辑器的基本界面已经完成,接下来要处理的就是数据交互,这就用到文件操作类QFile、文本流类和数据流类、缓冲区以及目录操作了,也为今天的实现文件的打开编辑和保存提供基础。
- 在之前的QMainWindow里面知道具体的某个功能都是通过添加QAction对象实现的,在我们选择某个功能时实际上就是点击了这个QAction对象,它将产生一个triggered()信号,通过这个信号可以关联一个槽函数,这样就可以在槽函数中实现该QAction对象对应需要提供的功能,由于Qt里面可以将多个信号映射到同一个槽,那么快捷工具栏就可以通过这种映射方式实现同一个功能的快捷操作。
connect(&ActionObj, SIGNAL(triggered()), this, OnActionSlots());
在打开文件之前需要做的就是获取所需文件的路径,意味着我们点击open按钮时需要弹出一个对话框供我们选择所要打开的文件,那么此时QFileDialog类就派上用场了,因为QFileDialog类的本质就是为了返回用户所选择的文件的路径,那么根据这个类我们要封装一个函数,能够让用户选择文件并将其路径返回。
//该函数存在于自定义主窗口类里
QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title)
{
QString ret = "";
QFileDialog fd(this);
QStringList filters;
filters.append("Text Files (*.txt)");
filters.append("All Files (*)");
fd.setWindowTitle(title);//对话框标题
fd.setAcceptMode(mode);
fd.setFilters(filters);
if( mode == QFileDialog::AcceptOpen )//是打开文件还是保存文件
{
fd.setFileMode(QFileDialog::ExistingFile);//设置可选单个已存在的文件
}
if( fd.exec() == QFileDialog::Accepted )
{
ret = fd.selectedFiles()[0];//函数返回装有选中的文件的绝对路径的链表
}
return ret;
}
函数最终返回了选择文件的路径。
得到了文件路径之后就可以打开文件并将文件内容导入到主窗口的编辑组件中,所以就可以实现打开文件的操作了:
void MainWindow::onFileOpen()
{
QString path = showFileDialog(QFileDialog::AcceptOpen, "Open");
if( path != "" )
{
QFile file(path);
if( file.open(QIODevice::ReadOnly | QIODevice::Text) )
{
mainEditor.setPlainText(QString(file.readAll()));//读取文件的所有数据并导入到编辑组件
file.close();
setWindowTitle("NotePad - [ " + path + " ]");
}
else
{
showErrorMessage(QString("Open file error! \n\n") + "\"" + path + "\"");//自定义的一个错误提示对话框
}
}
}
在这里有一个比较人性化的小功能,就是通过setWindowTitle()函数将当前编辑的文件路径显示在了主窗口的标题栏上。
QString MainWindow::saveCurrentData(QString path)
{
QString ret = path;
if( ret == "" )
ret = showFileDialog(QFileDialog::AcceptSave, "Save");
if( ret != "" )
{
QFile file(ret);
if( file.open(QIODevice::WriteOnly | QIODevice::Text) )
{
QTextStream out(&file);
out << mainEditor.toPlainText();
file.close();
setWindowTitle("NotePad - [ " + ret + " ]");
}
else
{
showErrorMessage(QString("Save file error! \n\n") + "\"" + ret + "\"");
ret = "";
}
}
return ret;
}
当有需要保存到文件时并且此时并没有对应文件时点击保存按钮时就会弹出如下对话框:
文件定义主要用到的成员变量、封装整个界面的函数声明以及用到的槽函数
#ifndef _MAINWINDOW_H_
#define _MAINWINDOW_H_
#include
#include
#include
#include
#include
#include
#include
#include
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QPlainTextEdit mainEditor;
QLabel statusLbl;
QString m_filePath;
bool m_isTextChanged;
MainWindow();
MainWindow(const MainWindow&);
MainWindow& operator= (const MainWindow&);
bool construct();
bool initMenuBar();
bool initToolBar();
bool initStatusBar();
bool initMainEditor();
bool initFileMenu(QMenuBar* mb);
bool initEditMenu(QMenuBar* mb);
bool initFormatMenu(QMenuBar* mb);
bool initViewMenu(QMenuBar* mb);
bool initHelpMenu(QMenuBar* mb);
bool initFileToolItem(QToolBar* tb);
bool initEditToolItem(QToolBar* tb);
bool initFormatToolItem(QToolBar* tb);
bool initViewToolItem(QToolBar* tb);
bool makeAction(QAction*& action, QWidget* parent, QString text, int key);
bool makeAction(QAction*& action, QWidget* parent, QString tip, QString icon);
QString showFileDialog(QFileDialog::AcceptMode mode, QString title);
int showQueryMessage(QString message);
void showErrorMessage(QString message);
QString saveCurrentData(QString path = "");
void preEditorChange();
private slots:
void onFileNew();
void onFileOpen();
void onFileSave();
void onFileSaveAs();
void onTextChanged();
public:
static MainWindow* NewInstance();
~MainWindow();
};
#endif // _MAINWINDOW_H_
文件只包含界面的构建,包含菜单栏、工具栏和状态栏。
#include "MainWindow.h"
#include
#include
#include
#include
#include
#include
MainWindow::MainWindow()
{
setWindowTitle("NotePad - [ New ]");
m_filePath = "";
m_isTextChanged = false;
}
MainWindow* MainWindow::NewInstance()
{
MainWindow* ret = new MainWindow();
if( (ret == NULL) || !ret->construct() )
{
delete ret;
ret = NULL;
}
return ret;
}
bool MainWindow::construct()
{
bool ret = true;
ret = ret && initMenuBar();
ret = ret && initToolBar();
ret = ret && initStatusBar();
ret = ret && initMainEditor();
return ret;
}
bool MainWindow::initMenuBar()
{
bool ret = true;
QMenuBar* mb = menuBar();
ret = ret && initFileMenu(mb);
ret = ret && initEditMenu(mb);
ret = ret && initFormatMenu(mb);
ret = ret && initViewMenu(mb);
ret = ret && initHelpMenu(mb);
return ret;
}
bool MainWindow::initToolBar()
{
bool ret = true;
QToolBar* tb = addToolBar("Tool Bar");
tb->setIconSize(QSize(16, 16));
ret = ret && initFileToolItem(tb);
tb->addSeparator();
ret = ret && initEditToolItem(tb);
tb->addSeparator();
ret = ret && initFormatToolItem(tb);
tb->addSeparator();
ret = ret && initViewToolItem(tb);
return ret;
}
bool MainWindow::initStatusBar()
{
bool ret = true;
QStatusBar* sb = statusBar();
QLabel* label = new QLabel("TQSilence");
if( label != NULL )
{
statusLbl.setMinimumWidth(200);
statusLbl.setAlignment(Qt::AlignCenter);
statusLbl.setText("Ln: 1 Col: 1");
label->setMinimumWidth(200);
label->setAlignment(Qt::AlignCenter);
sb->addPermanentWidget(new QLabel());
sb->addPermanentWidget(&statusLbl);
sb->addPermanentWidget(label);
}
else
{
ret = false;
}
return ret;
}
bool MainWindow::initMainEditor()
{
bool ret = true;
mainEditor.setParent(this);
connect(&mainEditor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
setCentralWidget(&mainEditor);
return ret;
}
bool MainWindow::initFileMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("File(&F)", mb);
bool ret = (menu != NULL);
if( ret )
{
QAction* action = NULL;
ret = ret && makeAction(action, menu, "New(&N)", Qt::CTRL + Qt::Key_N);
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileNew()));
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Open(&O)...", Qt::CTRL + Qt::Key_O);
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen()));
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Save(&S)", Qt::CTRL + Qt::Key_S);
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileSave()));
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Save As(&A)...", 0);
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs()));
menu->addAction(action);
}
menu->addSeparator();
ret = ret && makeAction(action, menu, "Print(&P)...", Qt::CTRL + Qt::Key_P);
if( ret )
{
menu->addAction(action);
}
menu->addSeparator();
ret = ret && makeAction(action, menu, "Exit(&X)", 0);
if( ret )
{
menu->addAction(action);
}
}
if( ret )
{
mb->addMenu(menu);
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initEditMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("Edit(&E)", mb);
bool ret = (menu != NULL);
if( ret )
{
QAction* action = NULL;
ret = ret && makeAction(action, menu, "Undo(&U)", Qt::CTRL + Qt::Key_Z);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Redo(&R)...", Qt::CTRL + Qt::Key_Y);
if( ret )
{
menu->addAction(action);
}
menu->addSeparator();
ret = ret && makeAction(action, menu, "Cut(&T)", Qt::CTRL + Qt::Key_X);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Copy(&C)...", Qt::CTRL + Qt::Key_C);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Paste(&P)...", Qt::CTRL + Qt::Key_V);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Delete(&L)", Qt::Key_Delete);
if( ret )
{
menu->addAction(action);
}
menu->addSeparator();
ret = ret && makeAction(action, menu, "Find(&F)...", Qt::CTRL + Qt::Key_F);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Replace(&R)...", Qt::CTRL + Qt::Key_H);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Goto(&G)...", Qt::CTRL + Qt::Key_G);
if( ret )
{
menu->addAction(action);
}
menu->addSeparator();
ret = ret && makeAction(action, menu, "Select All(&A)", Qt::CTRL + Qt::Key_A);
if( ret )
{
menu->addAction(action);
}
}
if( ret )
{
mb->addMenu(menu);
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initFormatMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("Format(&O)", mb);
bool ret = (menu != NULL);
if( ret )
{
QAction* action = NULL;
ret = ret && makeAction(action, menu, "Auto Wrap(&W)", 0);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Font(&F)...", 0);
if( ret )
{
menu->addAction(action);
}
}
if( ret )
{
mb->addMenu(menu);
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initViewMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("View(&V)", mb);
bool ret = (menu != NULL);
if( ret )
{
QAction* action = NULL;
ret = ret && makeAction(action, menu, "Tool Bar(&T)", 0);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "Status Bar(&S)", 0);
if( ret )
{
menu->addAction(action);
}
}
if( ret )
{
mb->addMenu(menu);
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initHelpMenu(QMenuBar* mb)
{
QMenu* menu = new QMenu("Help(&H)", mb);
bool ret = (menu != NULL);
if( ret )
{
QAction* action = NULL;
ret = ret && makeAction(action, menu, "User Manual", 0);
if( ret )
{
menu->addAction(action);
}
ret = ret && makeAction(action, menu, "About NotePad...", 0);
if( ret )
{
menu->addAction(action);
}
}
if( ret )
{
mb->addMenu(menu);
}
else
{
delete menu;
}
return ret;
}
bool MainWindow::initFileToolItem(QToolBar* tb)
{
bool ret = true;
QAction* action = NULL;
ret = ret && makeAction(action, tb, "New", ":/res/pic/new.png");
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileNew()));
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Open", ":/res/pic/open.png");
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileOpen()));
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Save", ":/res/pic/save.png");
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileSave()));
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Save As", ":/res/pic/saveas.png");
if( ret )
{
connect(action, SIGNAL(triggered()), this, SLOT(onFileSaveAs()));
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Print", ":/res/pic/print.png");
if( ret )
{
tb->addAction(action);
}
return ret;
}
bool MainWindow::initEditToolItem(QToolBar* tb)
{
bool ret = true;
QAction* action = NULL;
ret = ret && makeAction(action, tb, "Undo", ":/res/pic/undo.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Redo", ":/res/pic/redo.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Cut", ":/res/pic/cut.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Copy", ":/res/pic/copy.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Paste", ":/res/pic/paste.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Find", ":/res/pic/find.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Replace", ":/res/pic/replace.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Goto", ":/res/pic/goto.png");
if( ret )
{
tb->addAction(action);
}
return ret;
}
bool MainWindow::initFormatToolItem(QToolBar* tb)
{
bool ret = true;
QAction* action = NULL;
ret = ret && makeAction(action, tb, "Auto Wrap", ":/res/pic/wrap.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Font", ":/res/pic/font.png");
if( ret )
{
tb->addAction(action);
}
return ret;
}
bool MainWindow::initViewToolItem(QToolBar* tb)
{
bool ret = true;
QAction* action = NULL;
ret = ret && makeAction(action, tb, "Tool Bar", ":/res/pic/tool.png");
if( ret )
{
tb->addAction(action);
}
ret = ret && makeAction(action, tb, "Status Bar", ":/res/pic/status.png");
if( ret )
{
tb->addAction(action);
}
return ret;
}
bool MainWindow::makeAction(QAction*& action, QWidget* parent, QString text, int key)
{
bool ret = true;
action = new QAction(text, parent);
if( action != NULL )
{
action->setShortcut(QKeySequence(key));
}
else
{
ret = false;
}
return ret;
}
bool MainWindow::makeAction(QAction*& action, QWidget* parent, QString tip, QString icon)
{
bool ret = true;
action = new QAction("", parent);
if( action != NULL )
{
action->setToolTip(tip);
action->setIcon(QIcon(icon));
}
else
{
ret = false;
}
return ret;
}
MainWindow::~MainWindow()
{
}
逻辑处理中使用到的槽函数以及抽离出来的可复用的函数
#include "MainWindow.h"
#include
#include
#include
#include
#include
void MainWindow::showErrorMessage(QString message)
{
QMessageBox msg(this);
msg.setWindowTitle("Error");
msg.setText(message);
msg.setIcon(QMessageBox::Critical);
msg.setStandardButtons(QMessageBox::Ok);
msg.exec();
}
int MainWindow::showQueryMessage(QString message)
{
QMessageBox msg(this);
msg.setWindowTitle("Query");
msg.setText(message);
msg.setIcon(QMessageBox::Question);
msg.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
return msg.exec();
}
QString MainWindow::showFileDialog(QFileDialog::AcceptMode mode, QString title)
{
QString ret = "";
QFileDialog fd(this);
QStringList filters;
filters.append("Text Files (*.txt)");
filters.append("All Files (*)");
fd.setWindowTitle(title);
fd.setAcceptMode(mode);
fd.setFilters(filters);
if( mode == QFileDialog::AcceptOpen )
{
fd.setFileMode(QFileDialog::ExistingFile);
}
if( fd.exec() == QFileDialog::Accepted )
{
ret = fd.selectedFiles()[0];
}
return ret;
}
void MainWindow::preEditorChange()
{
if( m_isTextChanged )
{
int r = showQueryMessage("Do you want to save the changes to file?");
switch(r)
{
case QMessageBox::Yes:
saveCurrentData(m_filePath);
break;
case QMessageBox::No:
m_isTextChanged = false;
break;
case QMessageBox::Cancel:
break;
}
}
}
void MainWindow::onFileNew()
{
preEditorChange();
if( !m_isTextChanged )
{
mainEditor.clear();
setWindowTitle("NotePad - [ New ]");
m_filePath = "";
m_isTextChanged = false;
}
}
void MainWindow::onFileOpen()
{
preEditorChange();
if( !m_isTextChanged )
{
QString path = showFileDialog(QFileDialog::AcceptOpen, "Open");
if( path != "" )
{
QFile file(path);
if( file.open(QIODevice::ReadOnly | QIODevice::Text) )
{
mainEditor.setPlainText(QString(file.readAll()));
file.close();
m_filePath = path;
m_isTextChanged = false;
setWindowTitle("NotePad - [ " + m_filePath + " ]");
}
else
{
showErrorMessage(QString("Open file error! \n\n") + "\"" + path + "\"");
}
}
}
}
QString MainWindow::saveCurrentData(QString path)
{
QString ret = path;
if( ret == "" )
{
ret = showFileDialog(QFileDialog::AcceptSave, "Save");
}
if( ret != "" )
{
QFile file(ret);
if( file.open(QIODevice::WriteOnly | QIODevice::Text) )
{
QTextStream out(&file);
out << mainEditor.toPlainText();
file.close();
setWindowTitle("NotePad - [ " + ret + " ]");
m_isTextChanged = false;
}
else
{
showErrorMessage(QString("Save file error! \n\n") + "\"" + ret + "\"");
ret = "";
}
}
return ret;
}
void MainWindow::onFileSave()
{
QString path = saveCurrentData(m_filePath);
if( path != "" )
{
m_filePath = path;
}
}
void MainWindow::onFileSaveAs()
{
QString path = saveCurrentData();
if( path != "" )
{
m_filePath = path;
}
}
void MainWindow::onTextChanged()
{
if( !m_isTextChanged )
{
setWindowTitle("*" + windowTitle());
}
m_isTextChanged = true;
}