学习第二步:创建一个基于Qt Widget的NotePad应用程序。
详情请查看参考链接:Getting Started Programming with Qt Widgets
最终效果图展示如下:
和参考链接做的不太一样,不过问题不大。
第一步:创建项目
不一一详述了,直接截图
第二步:设计UI
拖动一个Text Editor到窗体中,在窗体中选中Text Editor并点击Lay out Vertically,然后ctrl + s保存
添加和用户交互的功能:
在type here输入File,然后单击File,然后再单击type here,输入New,接着点击New下面的type here,输入Open,如此接着输入Save,Save as,Print,exit。
输入完后,在下面的Action Editor中会出现五行内容,分别在这五行内容上点击go to slot然后双击选择triggered(),然后界面会跳到编辑代码模式,可以先不用管继续回到设计UI界面,接下来将他们分开一个一个拖到窗体中File菜单栏下的工具栏中,界面设计到此算是初步完成。
第三步:完善代码
首先打开Notepad.cpp文件,在创建项目的时候,软件会自动生成一些代码,现在我们要完善一下。
1、添加头文件
#include "QFile"
#include "QFileDialog"
#include "QMessageBox"
#include "QTextStream"
2、在构造函数中添加如下代码:
this->setCentralWidget(ui->textEdit);
connect(ui->actionNew, &QAction::triggered, this, &Notepad::newDocument);
connect(ui->actionOpen, &QAction::triggered, this, &Notepad::open);
connect(ui->actionSave, &QAction::triggered, this, &Notepad::save);
connect(ui->actionSave_as, &QAction::triggered, this, &Notepad::saveAs);
connect(ui->actionPrint, &QAction::triggered, this, &Notepad::print);
connect(ui->actionExit, &QAction::triggered, this, &Notepad::exit);
3、添加触发函数,在文件的最后面粘贴
void Notepad::newDocument()
{
currentFile.clear();
ui->textEdit->setText(QString());
}
void Notepad::open()
{
QString fileName = QFileDialog::getOpenFileName(this, "Open the file");
QFile file(fileName);
currentFile = fileName;
if (!file.open(QIODevice::ReadOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot open file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream in(&file);
QString text = in.readAll();
ui->textEdit->setText(text);
file.close();
}
void Notepad::save()
{
QString fileName;
// If we don't have a filename from before, get one.
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, "Warning", "Cannot save file: " + file.errorString());
return;
}
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
}
void Notepad::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, "Save as");
QFile file(fileName);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, "Warning", "Cannot save file: " + file.errorString());
return;
}
currentFile = fileName;
setWindowTitle(fileName);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.close();
}
void Notepad::print()
{
//#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 Notepad::exit()
{
QCoreApplication::quit();
}
4、然后打开Notepad.h文件,把私有slot中的代码全部删掉,然后加入如下代码:
void newDocument();
void open();
void save();
void saveAs();
void print();
void exit();
5、接着在Notepad.h文件中,在私有函数中加入QString currentFile,如下:
private:
Ui::NotePad *ui;
QString currentFile;
};
现在整个代码已经全部完成,可以编译运行了。
第四步:更改图标
有没有感觉你编译出来的界面没有别人的好看,那是因为你的界面少了一些图标,在我的参考网址的最后有一些图标可以下载,下载好后在你的项目文件夹里面新建一个myImages的文件夹并放入下载的图片,我这里说一下接下来怎么做。
首先新建一个Qt resource文件
在下一步名称填入Notepad,路径选择你自己的项目路径
接着直接下一步就行
选择Add > Add Prefix,在Prefix处用 “/” 替换默认的路径,然后选择添加文件,选择前面下载好的图片。
接下来在UI设计界面找到Action Editor,双击一个action,会出现一个页面如下:
点击图中图标后面的三个点的按钮,然后选择和你的按键名称相对应的图标,然后点击确定,接下来你就会看到在你设计的软件UI界面里面的action已经从单词变成图标了,至此,大功告成。
我的这个相对参考文档来说缺了很多东西,不过人给有所需,自己满意就好。