File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,9 @@ JsonTextEdit::JsonTextEdit(QWidget *parent):
55{
66 new JsonHighlighter (document ());
77 mSpacePattern = QRegularExpression (R"( ^\s*)" );
8+
9+ connect (this , &QPlainTextEdit::textChanged,
10+ this , &JsonTextEdit::updateJsonObject);
811}
912
1013void JsonTextEdit::keyPressEvent (QKeyEvent *event)
@@ -30,3 +33,25 @@ void JsonTextEdit::autoInsertSpace()
3033 }
3134 }
3235}
36+
37+ QJsonObject & JsonTextEdit::jsonObject ()
38+ {
39+ return mJsonObject ;
40+ }
41+
42+ void JsonTextEdit::updateJsonObject ()
43+ {
44+ QJsonDocument jsonDoc;
45+
46+ jsonDoc = QJsonDocument::fromJson (jsonString ().toUtf8 ());
47+
48+ if (!jsonDoc.isNull ()) {
49+ mJsonObject = jsonDoc.object ();
50+ emit updated (mJsonObject );
51+ }
52+ }
53+
54+ QString JsonTextEdit::jsonString ()
55+ {
56+ return document ()->toRawText ().simplified ();
57+ }
Original file line number Diff line number Diff line change 88#include < QRegularExpression>
99#include < QRegularExpressionMatch>
1010#include < QTextBlock>
11+ #include < QJsonArray>
12+ #include < QJsonDocument>
13+ #include < QJsonObject>
14+ #include < QJsonValue>
15+ #include < QString>
1116
1217#include " jsonhighlighter.h"
1318
@@ -19,14 +24,24 @@ Q_OBJECT
1924public:
2025 JsonTextEdit (QWidget *parent);
2126
27+ QString jsonString ();
28+ QJsonObject & jsonObject ();
29+
30+ signals:
31+ void updated (const QJsonObject &json);
32+
2233protected:
2334 virtual void keyPressEvent (QKeyEvent *event);
2435
2536private:
2637 void autoInsertSpace ();
2738
39+ private slots:
40+ void updateJsonObject ();
41+
2842private:
2943 QRegularExpression mSpacePattern ;
44+ QJsonObject mJsonObject ;
3045};
3146
3247#endif // JSONTEXTEDIT_H
You can’t perform that action at this time.
0 commit comments