This commit is contained in:
2025-09-28 17:05:51 +08:00
parent 8eb80ab66d
commit d97fa3e0fe
398 changed files with 18737 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.16)
project(2-XmlHttpRequest VERSION 0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)
qt_standard_project_setup()
qt_add_executable(app2-XmlHttpRequest
main.cpp
)
qt_add_qml_module(app2-XmlHttpRequest
URI 2-XmlHttpRequest
VERSION 1.0
QML_FILES Main.qml
)
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
set_target_properties(app2-XmlHttpRequest PROPERTIES
# MACOSX_BUNDLE_GUI_IDENTIFIER com.example.app2-XmlHttpRequest
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)
target_link_libraries(app2-XmlHttpRequest
PRIVATE Qt6::Quick
)
include(GNUInstallDirs)
install(TARGETS app2-XmlHttpRequest
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

View File

@@ -0,0 +1,99 @@
import QtQuick
import QtQuick.Controls
Window {
width: 640
height: 480
visible: true
title: qsTr("XmlHttpRequest")
//Process the data right away
/*
function downloadData(url){
//Create the object
var xhr = new XMLHttpRequest()
//Apply initial settings to the object
xhr.onreadystatechange = function(){
//HEADERS_RECEIVED
//DONE
if(xhr.readyState === XMLHttpRequest.HEADERS_RECEVED){
console.log("Headers received")
}else if(xhr.readyState === XMLHttpRequest.DONE){
if(xhr.status == 200){
//console.log("Got the data from the server it is: " + xhr.responseText.toString())
textAreaId.text = xhr.responseText.toString()
}else{
console.log("Something went wrong")
}
}
}
xhr.open("GET",url)
xhr.send()
}
*/
//Go through a callback
function downloadData(url,callback){
//Create the object
var xhr = new XMLHttpRequest()
//Apply initial settings to the object
xhr.onreadystatechange = function(){
//HEADERS_RECEIVED
//DONE
if(xhr.readyState === XMLHttpRequest.HEADERS_RECEVED){
console.log("Headers received")
}else if(xhr.readyState === XMLHttpRequest.DONE){
if(xhr.status == 200){
//console.log("Got the data from the server it is: " + xhr.responseText.toString())
//textAreaId.text = xhr.responseText.toString()
callback(xhr.responseText.toString())
}else{
console.log("Something went wrong")
}
}
}
xhr.open("GET",url)
xhr.send()
}
TextArea{
id: textAreaId
anchors.fill: parent
textFormat: TextArea.RichText
text: "Click to download html data"
}
MouseArea{
anchors.fill: parent
onClicked: function(){
/*
Urls to try :
. https://www.qt.io/
. https://github.com/
. https://jsonplaceholder.typicode.com/
*/
//Download data right away
/*
console.log("Clicked")
downloadData("https://www.qt.io")
*/
//Download the data through a callback
downloadData("https://jsonplaceholder.typicode.com/",function(response){
if(response){
textAreaId.text = response
}else{
console.log("Something went wrong")
}
})
}
}
}

View File

@@ -0,0 +1,19 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
const QUrl url(u"qrc:/2-XmlHttpRequest/Main.qml"_qs);
QObject::connect(
&engine,
&QQmlApplicationEngine::objectCreationFailed,
&app,
[]() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.load(url);
return app.exec();
}