提交 3a4ef971 编写于 作者: mahuifa's avatar mahuifa

feat:创建json读写示例程序

上级 274ad9cf
TEMPLATE = subdirs
SUBDIRS += QtJson # qt使用内置json库读写json示例
#---------------------------------------------------------------------------------------
# @功能: 使用内置json库读写json示例
# @编译器: Desktop Qt 5.12.5 MSVC2017 64bit(也支持其它编译器)
# @Qt IDE D:/Qt/Qt5.12.5/Tools/QtCreator/share/qtcreator
#
# @开发者 mhf
# @邮箱 1603291350@qq.com
# @时间 2022-12-12 22:49:26
# @备注
#---------------------------------------------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
# 定义程序版本号
VERSION = 1.0.0
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
contains(QT_ARCH, i386){ # 使用32位编译器
DESTDIR = $$PWD/../bin # 程序输出路径
}else{
DESTDIR = $$PWD/../bin64 # 使用64位编译器
}
# msvc 编译器使用utf-8编码
msvc {
QMAKE_CFLAGS += /utf-8
QMAKE_CXXFLAGS += /utf-8
}
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
#include "widget.h"
#include "ui_widget.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>472</width>
<height>366</height>
</rect>
</property>
<property name="windowTitle">
<string>Widget</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>解析Json</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>生成Json</string>
</property>
</widget>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0" colspan="3">
<widget class="QTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
......@@ -13,7 +13,7 @@ MainWindow::MainWindow(QWidget *parent)
QWebEnginePage* page = m_view->page();
connect(page, &QWebEnginePage::featurePermissionRequested, this, &MainWindow::on_featurePermissionRequested);
#if 0
#if 1
page->load(QUrl(QStringLiteral("https://app.xunjieshipin.com/luping/")));
#else
page->load(QUrl(QStringLiteral("qrc:/video.html")));
......
......@@ -9,6 +9,7 @@ Widget::Widget(QWidget *parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle(QString("使用QtWebChannel实现Qt与Web通信交互 - V%1").arg(APP_VERSION)); // 设置窗口标题
connect(Core::getInstance(), &Core::webButClicked, this, &Widget::on_webButClicked);
......
......@@ -16,11 +16,13 @@ int main(int argc, char *argv[])
#if DEMO == 0
QString html("<h1 align='center' style='font-size:80px; color:red;'>hello </h1>");
view.setHtml(html); // 直接在QWebEngineView中显示Html代码
#elif DEMO == 1
// 百度搜索界面的图标地址 https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png
// setHtml使用参数2 (baseUrl)后,可以在html代码中使用【相对路径】引用baseUrl中的资源
QString html("<img src='/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png'/>"); // 注意:字符串html不能超过2MB
view.setHtml(html, QUrl("https://www.baidu.com"));
#elif DEMO == 2
QString html("<h1 align='center' style='font-size:80px; color:red;'>hello </h1>");
view.setContent(html.toUtf8(), "text/html; charset=UTF-8");
......
TEMPLATE = subdirs
SUBDIRS += QtWebExamples
SUBDIRS += QtWebExamples \
JsonExamples
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册