在 QML 里使用 C++ 类
.h
文件
需要注意 Q_INVOKABLE
的函数应以首字母小写开头
#ifndef LL1PARSINGTOQML_H
#define LL1PARSINGTOQML_H
#include <QObject>
#include <vector>
#include <string>
#include <set>
#include <map>
#include "LL1Parsing.h"
class LL1ParsingToQML : public QObject
{
Q_OBJECT
public:
explicit LL1ParsingToQML(QObject *parent = nullptr);
Q_INVOKABLE void test();
Q_INVOKABLE void add_token(QString token);
Q_INVOKABLE void add_token_left(QString token);
Q_INVOKABLE bool add_production();
Q_INVOKABLE void set_start_token(QString start_token);
Q_INVOKABLE void clear();
Q_INVOKABLE QString get_parsing_table_str();
Q_INVOKABLE QString get_first_s_set_str();
Q_INVOKABLE QString get_follow_set_str();
private:
std::wstring m_start_token;
std::vector<QString> m_production;
std::wstring m_production_left;
std::map<std::wstring, std::set<std::vector<std::wstring>>> m_productions_map;
M6::LL1Parsing m_parser;
std::vector<QString> m_re; // table str
std::vector<QString> m_firsts;
std::vector<QString> m_follow;
};
#endif // LL1PARSINGTOQML_H
.cpp
文件
#include "ll1parsingtoqml.h"
#include <QDebug>
#include <iostream>
LL1ParsingToQML::LL1ParsingToQML(QObject *parent):
QObject (parent)
{
}
void LL1ParsingToQML::test()
{
qDebug()<<"RunParsing ... ";
if(m_start_token.length() < 1)
{
qDebug()<<"start_token length is 1";
}
m_parser.RunParsing();
// Get FIRST_S Set
m_firsts.clear();
auto first_s_set = std::map<std::tuple<M6::token, std::vector<M6::token>>, std::set<M6::token>>();
m_parser.GetFirstSSet(first_s_set);
for(auto production:first_s_set)
{
std::wstring first = std::get<0