用qt纯代码做一个记事本,并将所有简单的功能实现
网上有很多做记事本的例子都是用的是designer设计师操作ui界面来实现的,包括官方的notepad实例也是这样子,所以决定用纯代码的方式,做一个记事本的小例子,并将所有简单的功能实现出来
1.新建一个基类为mainwindow的工程,取消ui界面的创建
2.在工程中添加资源文件,用以存放图片,图片的尺寸最好是符合菜单工具栏大小的,推荐32*32大小
3.h头文件中添加所需要的控件头文件
#include <QMenu>
#include <QMenuBar>
#include <QAction>
#include <QToolBar>
#include <QLineEdit>
#include <QTextEdit>
#include <QVBoxLayout>
然后在头文件中初始化声明
private:
//新建
QMenu *fileMenu, *editMenu, *helpMenu;
QToolBar *fileToolBar;
QToolBar *editToolBar; //新建两个工具栏,用以存放四个菜单动作
QAction *newAct;
QAction *openAct;
QAction *saveAct;
QAction *saveasAct;
QAction *printAct;
QAction *cutAct;
QAction *copyAct;
QAction *pasteAct;
QAction *undoAct;
QAction *redoAct;
QAction *aboutQtAct;
QAction *exitAct;
QTextEdit *textEdit;
QLineEdit *lineEdit;
QVBoxLayout *vboxlayout;
初始化所需要的槽函数,用以实现各种功能,槽函数的名字最好取来简单易懂,让自己一眼就知道是什么意思
private slots:
void newFile();
void openFile();
void saveFile();
void saveasFile();
void printFile();
void cutAction();
void copyAction();
void pasteAction();
void undoAction();
void redoAction();
void exitAction();
4.cpp文件中的操作
在cpp文件中添加相应的文件操作以及所需要的头文件,文件操作的相关流程在上一篇博客里使用QFile文件操作
#include
#include
#include
#include
#include
对于菜单栏和工具栏的实例化在ui界面的实现方法里是直接通过拖动创建的,因为我这里用的是纯代码写的,可能有些繁琐,但是理解起来更简单,有助于理解界面编程的精髓以及项目的移植,所以倡导多用纯代码写界面。相关的代码实现如下
this->resize(500,400);
this->setWindowTitle("记事本");
this->setWindowIcon(QIcon(":/images/file/notepad.png")); //设置应用显示图标
//实例菜单和控件
fileMenu = new QMenu(this);
editMenu = new QMenu(this);
helpMenu = new QMenu(this);
textEdit = new QTextEdit(this);
vboxlayout = new QVBoxLayout(this);
this->setCentralWidget(textEdit);
//填充菜单子节点
newAct = new QAction(QIcon( ":/images/file/new_file.png" ), tr( "新建" ), this );
newAct->setShortcut(tr("Ctrl+N" )); //快捷键
newAct->setStatusTip(tr("新建文件" )); //底部状态提示
openAct = new QAction(QIcon( ":/images/file/open_file.png" ), tr( "打开" ), this );
openAct->setShortcut(tr("Ctrl+O" ));
openAct->setStatusTip(tr("打开文件" ));
saveAct = new QAction(QIcon( ":/images/file/save_file.png" ), tr( "保存" ), this );
saveAct->setShortcut(tr("Ctrl+S" ));
saveAct->setStatusTip(tr("保存文件" ));
saveasAct = new QAction(QIcon( ":/images/file/save_as.png" ), tr( "另存为" ), this );
saveasAct->setShortcut(tr("Ctrl+Shift+S" ));
saveasAct->setStatusTip(tr("另存为文件" ));
printAct = new QAction(QIcon( ":/images/file/print.png" ), tr( "打印" ), this );
printAct->setShortcut(tr("Ctrl+P" ));
printAct->setStatusTip(tr("打印" ));
exitAct = new QAction(QIcon( ":/images/file/exit.png" ), tr( "退出" ), this );
exitAct->setShortcut(tr("Ctrl+E"));
exitAct->setStatusTip(tr("退出"));
//信号与槽
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
connect(openAct,SIGNAL(triggered()),this,SLOT(openFile()));
connect(saveAct,SIGNAL(triggered()),this,SLOT(saveFile()));
connect(saveasAct,SIGNAL(triggered()),this,SLOT(saveasFile()));
connect(printAct,SIGNAL(triggered()),this,SLOT(printFile()));
cutAct = new QAction(QIcon( ":/images/file/cut.png" ), tr( "剪切" ), this );
cutAct->setShortcut(tr("Ctrl+X" ));
cutAct->setStatusTip(tr("剪切内容" ));
copyAct = new QAction(QIcon( ":/images/file/复制.png" ), tr( "复制" ), this );
copyAct->setShortcut(tr("Ctrl+C" ));
copyAct->setStatusTip(tr("复制内容" ));
pasteAct = new QAction(QIcon( ":/images/file/粘贴.png" ), tr( "粘贴" ), this );
pasteAct->setShortcut(tr("Ctrl+V" ));
pasteAct->setStatusTip(tr("粘贴内容" ));
undoAct = new QAction(QIcon( ":/images/file/undo.png" ), tr( "向后" ), this );
undoAct->setShortcut(tr("Ctrl+Z" ));
undoAct->setStatusTip(tr("向后一步" ));
redoAct = new QAction(QIcon( ":/images/file/redo.png" ), tr( "向前" ), this );
redoAct->setShortcut(tr("Ctrl+Y" ));
redoAct->setStatusTip(tr("向前一步" ));
aboutQtAct = new QAction(QIcon(":/images/file/about.png"),tr( "关于 Qt" ), this );
aboutQtAct->setStatusTip(tr("关于 Qt 信息" ));
//信号与槽
connect(copyAct, SIGNAL(triggered()), this, SLOT(copyAction()));
connect(cutAct, SIGNAL(triggered()), this, SLOT(cutAction()));
connect(pasteAct, SIGNAL(triggered()), this, SLOT(pasteAction()));
connect(undoAct, SIGNAL(triggered()), this, SLOT(undoAction()));
connect(redoAct, SIGNAL(triggered()), this, SLOT(redoAction()));
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
connect(exitAct, SIGNAL(triggered()), this, SLOT(exitAction()));
//填充菜单
fileMenu = menuBar()->addMenu(tr( "文件" ));
fileMenu->addAction(newAct);
fileMenu->addAction(openAct);
fileMenu->addAction(saveAct);
fileMenu->addAction(saveasAct);
fileMenu->addAction(exitAct);
fileMenu->addSeparator();
editMenu = menuBar()->addMenu(tr("编辑" ));
editMenu->addAction(cutAct);
editMenu->addAction(copyAct);
editMenu->addAction(pasteAct);
editMenu->addAction(undoAct);
editMenu->addAction(redoAct);
menuBar()->addSeparator();
helpMenu = menuBar()->addMenu(tr("帮助" ));
helpMenu->addAction(aboutQtAct);
//toolBar 工具条
//fileTooBar添加
fileToolBar = addToolBar(tr("新建")); //右键添加改变
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
fileToolBar->addAction(saveAct);
fileToolBar->addAction(saveasAct);
fileToolBar->addAction(printAct);
//editToolBar添加
editToolBar = addToolBar(tr("修改"));
editToolBar->addAction(cutAct);
editToolBar->addAction(copyAct);
editToolBar->addAction(pasteAct);
editToolBar->addAction(undoAct);
editToolBar->addAction(redoAct);
editToolBar->addAction(exitAct);
}
MainWindow::~MainWindow()
{
delete ui;
}
/*
void MainWindow::showMainwindow2()
{
w2.show();
}
*/
/*槽函数实现*/
//子菜单事件
void MainWindow::newFile()
{
/*
*
QMessageBox::warning(this,tr("Warning"),
tr("创建新文件?"),
QMessageBox::Yes | QMessageBox::Default,
QMessageBox::No);
*
*/
currentFile.clear();
textEdit->setText(QString());
}
void MainWindow::openFile()
{
QString fileName = QFileDialog::getOpenFileName(this, "打开文件");
QFile file(fileName);
currentFile = fileName;
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
textEdit->setText(text);
file.close();
}
void MainWindow::saveFile()
{
QString fileName;
// 若没有文件,重新创建一个
if (currentFile.isEmpty()) {
fileName = QFileDialog::getSaveFileName(this, "Save");
currentFile = fileName;
} else {
fileName = currentFile;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = textEdit->toPlainText();
out << text;
file.close();
}
void MainWindow::saveasFile()
{
QString fileName = QFileDialog::getSaveFileName(this, "另存为");
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "警告", "无法打开文件: " + file.errorString());
return;
}
currentFile = fileName;
setWindowTitle(fileName);
QTextStream out(&file);
QString text = textEdit->toPlainText();
out << text;
file.close();
}
void MainWindow::printFile()
{
//暂时空
/*
#if QT_CONFIG(printer)
QPrinter printDev;
#if QT_CONFIG(printdialog)
QPrintDialog dialog(&printDev, this);
if (dialog.exec() == QDialog::Rejected)
return;
#endif // QT_CONFIG(printdialog)
ui->textEdit->print(&printDev);
#endif // QT_CONFIG(printer)
*/
}
void MainWindow::cutAction()
{
textEdit->cut();
}
void MainWindow::pasteAction()
{
textEdit->paste();
}
void MainWindow::copyAction()
{
textEdit->copy();
}
void MainWindow::undoAction()
{
textEdit->undo();
}
void MainWindow::redoAction()
{
textEdit->redo();
}
void MainWindow::exitAction()
{
this->close();
}
敲出来的代码感觉很有成就感,当然,在大佬面前这种东西不值一提(萌新哭唧唧┭┮﹏┭┮),效果实现如下
记事本的所有简单的功能就这样实现了。这里没有对界面部分做进一步的优化,还是能让人感觉到Qt的魅力以及界面编程的魅力。附上文件下载链接Qt纯代码记事本