mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-12-20 03:01:36 +08:00
chore:将信息显示在主界面上
This commit is contained in:
@@ -1,5 +1,39 @@
|
|||||||
#include "appdelegate.h"
|
#include "appdelegate.h"
|
||||||
|
#include <QIcon>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
AppDelegate::AppDelegate(QObject *parent)
|
AppDelegate::AppDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
|
||||||
: QObject{parent}
|
|
||||||
{}
|
void AppDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
QStyledItemDelegate::paint(painter, option, index);
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
QString name = index.data(Qt::DisplayRole).toString();
|
||||||
|
QString currentVersion = index.data(Qt::UserRole + 2).toString();
|
||||||
|
QString newVersion = index.data(Qt::UserRole + 3).toString();
|
||||||
|
QString iconPath = index.data(Qt::UserRole + 4).toString();
|
||||||
|
|
||||||
|
// 绘制图标
|
||||||
|
QIcon icon(iconPath);
|
||||||
|
QRect iconRect(option.rect.x() + 10, option.rect.y() + 10, 32, 32);
|
||||||
|
icon.paint(painter, iconRect);
|
||||||
|
|
||||||
|
// 绘制文本
|
||||||
|
QRect textRect(iconRect.right() + 10, option.rect.y() + 10, option.rect.width() - iconRect.width() - 20, option.rect.height() - 20);
|
||||||
|
painter->drawText(textRect, Qt::TextWordWrap, QString("%1\n当前版本: %2 → 新版本: %3").arg(name, currentVersion, newVersion));
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize AppDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
return QSize(option.rect.width(), 60); // 每行高度 60
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AppDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
|
||||||
|
{
|
||||||
|
if (event->type() == QEvent::MouseButtonRelease) {
|
||||||
|
qDebug() << "点击了第" << index.row() << "行";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return QStyledItemDelegate::editorEvent(event, model, option, index);
|
||||||
|
}
|
||||||
@@ -1,15 +1,21 @@
|
|||||||
#ifndef APPDELEGATE_H
|
#ifndef APPDELEGATE_H
|
||||||
#define APPDELEGATE_H
|
#define APPDELEGATE_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QStyledItemDelegate>
|
||||||
|
#include <QPainter>
|
||||||
|
// 添加 QEvent 头文件
|
||||||
|
#include <QEvent>
|
||||||
|
|
||||||
class AppDelegate : public QObject
|
class AppDelegate : public QStyledItemDelegate
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit AppDelegate(QObject *parent = nullptr);
|
explicit AppDelegate(QObject *parent = nullptr);
|
||||||
|
|
||||||
signals:
|
// 重写方法
|
||||||
|
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||||
|
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
|
||||||
|
bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // APPDELEGATE_H
|
#endif // APPDELEGATE_H
|
||||||
|
|||||||
@@ -1,5 +1,40 @@
|
|||||||
#include "applistmodel.h"
|
#include "applistmodel.h"
|
||||||
|
|
||||||
AppListModel::AppListModel(QObject *parent)
|
AppListModel::AppListModel(QObject *parent) : QAbstractListModel(parent) {}
|
||||||
: QObject{parent}
|
|
||||||
{}
|
int AppListModel::rowCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
if (parent.isValid())
|
||||||
|
return 0;
|
||||||
|
return m_data.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant AppListModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
if (!index.isValid() || index.row() >= m_data.size())
|
||||||
|
return QVariant();
|
||||||
|
|
||||||
|
const QJsonObject obj = m_data.at(index.row()).toObject();
|
||||||
|
|
||||||
|
switch (role) {
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
return obj["name"].toString();
|
||||||
|
case Qt::UserRole + 1: // 包名
|
||||||
|
return obj["package"].toString();
|
||||||
|
case Qt::UserRole + 2: // 当前版本
|
||||||
|
return obj["current_version"].toString();
|
||||||
|
case Qt::UserRole + 3: // 新版本
|
||||||
|
return obj["new_version"].toString();
|
||||||
|
case Qt::UserRole + 4: // 图标路径
|
||||||
|
return obj["icon"].toString();
|
||||||
|
default:
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AppListModel::setUpdateData(const QJsonArray &data)
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
m_data = data;
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,15 +1,26 @@
|
|||||||
#ifndef APPLISTMODEL_H
|
#ifndef APPLISTMODEL_H
|
||||||
#define APPLISTMODEL_H
|
#define APPLISTMODEL_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QAbstractListModel>
|
||||||
|
#include <QJsonArray>
|
||||||
|
// 添加 QJsonObject 头文件
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
class AppListModel : public QObject
|
class AppListModel : public QAbstractListModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit AppListModel(QObject *parent = nullptr);
|
explicit AppListModel(QObject *parent = nullptr);
|
||||||
|
|
||||||
signals:
|
// 重写方法
|
||||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||||
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||||
|
|
||||||
|
// 设置更新数据
|
||||||
|
void setUpdateData(const QJsonArray &data);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QJsonArray m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // APPLISTMODEL_H
|
#endif // APPLISTMODEL_H
|
||||||
|
|||||||
@@ -4,8 +4,21 @@
|
|||||||
MainWindow::MainWindow(QWidget *parent)
|
MainWindow::MainWindow(QWidget *parent)
|
||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
, ui(new Ui::MainWindow)
|
, ui(new Ui::MainWindow)
|
||||||
|
, m_model(new AppListModel(this))
|
||||||
|
, m_delegate(new AppDelegate(this))
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
// 创建 QListView 并设置父控件为 ui->appWidget
|
||||||
|
listView = new QListView(ui->appWidget);
|
||||||
|
listView->setModel(m_model);
|
||||||
|
listView->setItemDelegate(m_delegate);
|
||||||
|
|
||||||
|
// 设置 QListView 填充 ui->appWidget
|
||||||
|
QVBoxLayout *layout = new QVBoxLayout(ui->appWidget);
|
||||||
|
layout->addWidget(listView);
|
||||||
|
layout->setContentsMargins(0, 0, 0, 0);
|
||||||
|
|
||||||
checkUpdates();
|
checkUpdates();
|
||||||
initStyle();
|
initStyle();
|
||||||
}
|
}
|
||||||
@@ -102,9 +115,8 @@ void MainWindow::initStyle()
|
|||||||
void MainWindow::checkUpdates()
|
void MainWindow::checkUpdates()
|
||||||
{
|
{
|
||||||
aptssUpdater updater;
|
aptssUpdater updater;
|
||||||
|
QJsonArray updateInfo = updater.getUpdateInfoAsJson();
|
||||||
// 获取可更新包列表
|
m_model->setUpdateData(updateInfo);
|
||||||
updater.getUpdateInfoAsJson();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#include <QMainWindow>
|
#include <QMainWindow>
|
||||||
#include "aptssupdater.h"
|
#include "aptssupdater.h"
|
||||||
|
#include "applistmodel.h"
|
||||||
|
#include "appdelegate.h"
|
||||||
|
#include <QListView>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class MainWindow;
|
class MainWindow;
|
||||||
@@ -21,5 +25,8 @@ private:
|
|||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
void checkUpdates();
|
void checkUpdates();
|
||||||
void initStyle();
|
void initStyle();
|
||||||
|
AppListModel *m_model;
|
||||||
|
AppDelegate *m_delegate;
|
||||||
|
QListView *listView; // 声明 QListView 指针
|
||||||
};
|
};
|
||||||
#endif // MAINWINDOW_H
|
#endif // MAINWINDOW_H
|
||||||
|
|||||||
Reference in New Issue
Block a user