人是新人,已经大四临近毕业。今天的毕业设计组会开完,在老师的提醒下,我就想写两篇文章,以纪念我的大学生活和惨不忍睹毕设项目。(我知道自己的程序写的很烂,第一次发文,也很紧张)
我的毕业设计是实现一个简易的Lua集成开发环境,最主要的要求是实现能够代码编辑,高亮显示,断点调试和单步调试。老师没有规定语言,但是我只有C++摸得比较熟,同时也是想挑战一下自己吧,想做点不一样的,在网上搜索的时候看到很多QML与C++的信息,瞬间感觉qml好厉害,所以选择了QML与C++来编写Lua的ide。使用的编辑器是Qt Creator。
到目前为止,我已经实现了本地文件读取,高亮,行号。下面是目前功能和代码的展示。
一.本地读取
本地读取是我在编写编译器时的第一个难点,首先我们要了解QML读取文件的两种方式:一种是通过qrc添加资源,另一种是通过文件路径读取。我选择了通过文件路径读取,编写一个组件File,在选择文件后将其中数据显示到控制台中的TextEdit区域。
以下为部分代码:
File.h
#ifndef QT_HUB_FILE_H
#define QT_HUB_FILE_H
#include <QObject>
class File : public QObject
{
Q_OBJECT
public:
File();
Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
QString source() const;
void setSource(const QString &source);
QString text() const;
void setText(const QString &text);
signals:
void sourceChanged();
void textChanged();
private slots:
void readFile();
private:
QString m_source;
QString m_text;
};
#endif // FILE_H
File.cpp
#include "File.h"
#include <QFile>
#include <QDebug>
File::File()
{
connect(this, SIGNAL(sourceChanged()), this, SLOT(readFile()));
}
void File::setSource(const QString &source)
{
m_source = source;
emit sourceChanged();
}
QString File::source() const
{
return m_source;
}
void File::setText(const QString &text)
{
QFile file(m_source);
if (!file.open(QIODevice::WriteOnly)) {
m_text = "";
qDebug() << "Error:" << m_source << "open failed!";
}
els