From 7ca314116cc2fa6bdeaee81efcd049e786cd8e51 Mon Sep 17 00:00:00 2001 From: gfdgd_xi <3025613752@qq.com> Date: Mon, 22 Apr 2024 22:47:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=BE=E7=A4=BA=E5=86=85?= =?UTF-8?q?=E6=A0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernelinformation.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++ kernelinformation.h | 32 ++++++++++++++++- mainwindow.cpp | 27 ++++++++++++++ mainwindow.h | 3 ++ mainwindow.ui | 19 ++++++++-- 5 files changed, 161 insertions(+), 3 deletions(-) diff --git a/kernelinformation.cpp b/kernelinformation.cpp index dd0c5c5..8443ea7 100644 --- a/kernelinformation.cpp +++ b/kernelinformation.cpp @@ -4,3 +4,86 @@ KernelInformation::KernelInformation() { } + +void KernelInformation::LoadInfo() +{ + QUrl url(this->url); + QUrlQuery query; + query.addQueryItem("format", "j1"); + url.setQuery(query.toString(QUrl::FullyEncoded)); + qDebug() << url; + QNetworkRequest request(url); + QNetworkAccessManager *m_http = new QNetworkAccessManager(this); + QNetworkReply *reply = m_http->get(request); + connect(reply, &QNetworkReply::finished, this, [this, m_http](){ + QNetworkReply *reply = qobject_cast(sender()); + QByteArray data = reply->readAll(); + qDebug() << data; + qDebug() << reply->error(); + this->listData = QJsonDocument::fromJson(data).array(); + emit loadFinished(reply); + }); +} + +QJsonArray KernelInformation::get_listData() const +{ + return this->listData; +} + +int KernelInformation::get_count() const +{ + return this->listData.count(); +} + +QJsonObject KernelInformation::get_kernelData(int value) const +{ + return this->listData.at(value).toObject(); +} + +QString KernelInformation::get_name(int value) const +{ + return get_kernelData(value).value("Name").toString(); +} + +QString KernelInformation::get_author(int value) const +{ + return get_kernelData(value).value("Author").toString(); +} + +QString KernelInformation::get_des(int value) const +{ + return get_kernelData(value).value("Des").toString().replace("\\n", "\n"); +} + +QStringList KernelInformation::get_pkgName(int value) const +{ + QJsonArray list = get_kernelData(value).value("PkgName").toArray(); + int count = list.count(); + QStringList result; + for(int i = 0; i <= count; i++) { + result << list.at(i).toString(); + } + return result; +} + +QStringList KernelInformation::get_system(int value) const +{ + QJsonArray list = get_kernelData(value).value("System").toArray(); + int count = list.count(); + QStringList result; + for(int i = 0; i <= count; i++) { + result << list.at(i).toString(); + } + return result; +} + +QStringList KernelInformation::get_arch(int value) const +{ + QJsonArray list = get_kernelData(value).value("Arch").toArray(); + int count = list.count(); + QStringList result; + for(int i = 0; i <= count; i++) { + result << list.at(i).toString(); + } + return result; +} diff --git a/kernelinformation.h b/kernelinformation.h index 24ea357..5b072c4 100644 --- a/kernelinformation.h +++ b/kernelinformation.h @@ -1,11 +1,41 @@ #ifndef KERNELINFORMATION_H #define KERNELINFORMATION_H +#include -class KernelInformation +#include +#include +#include +#include + +#include +#include +#include + +class KernelInformation : public QObject { + Q_OBJECT + public: KernelInformation(); + void LoadInfo(); + QJsonArray get_listData() const; + QJsonObject get_kernelData(int value) const; + int get_count() const; + QString get_name(int value) const; + QString get_author(int value) const; + QString get_des(int value) const; + QStringList get_pkgName(int value) const; + QStringList get_system(int value) const; + QStringList get_arch(int value) const; + +signals: + void loadFinished(QNetworkReply *reply); + + +private: + QString url = "http://127.0.0.1:8000/information.json"; + QJsonArray listData; }; #endif // KERNELINFORMATION_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 41a26bd..75aa778 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,11 +1,38 @@ #include "mainwindow.h" #include "ui_mainwindow.h" +#include "kernelinformation.h" + +#include + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); + RefreshKernelList(); +} + +void MainWindow::RefreshKernelList() +{ + KernelInformation *information = new KernelInformation(); + connect(information, &KernelInformation::loadFinished, this, [this, information](){ + qDebug() << information->get_listData(); + RefreshKernelListView(information); + delete information; + }); + information->LoadInfo(); +} + +void MainWindow::RefreshKernelListView(KernelInformation *info) +{ + // 更新列表 + int count = info->get_count(); + QStandardItemModel *model = new QStandardItemModel(); + for(int i = 0; i <= count; i++) { + model->setItem(0, i, new QStandardItem(info->get_name(i))); + } + ui->m_kernelShow->setModel(model); } MainWindow::~MainWindow() diff --git a/mainwindow.h b/mainwindow.h index 4643e32..ff651d4 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include "kernelinformation.h" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } @@ -17,5 +18,7 @@ public: private: Ui::MainWindow *ui; + void RefreshKernelList(); + void RefreshKernelListView(KernelInformation *info); }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index b232854..6a62532 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -13,8 +13,23 @@ MainWindow - - + + + + + + + + + + + 0 + 0 + 800 + 23 + + +