chore:将信息显示在主界面上

This commit is contained in:
momen 2025-06-07 23:34:38 +08:00
parent 9e15e701c2
commit 50adf55762
6 changed files with 120 additions and 15 deletions

View File

@ -1,5 +1,39 @@
#include "appdelegate.h"
#include <QIcon>
#include <QDebug>
AppDelegate::AppDelegate(QObject *parent)
: QObject{parent}
{}
AppDelegate::AppDelegate(QObject *parent) : QStyledItemDelegate(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);
}

View File

@ -1,15 +1,21 @@
#ifndef APPDELEGATE_H
#define APPDELEGATE_H
#include <QObject>
#include <QStyledItemDelegate>
#include <QPainter>
// 添加 QEvent 头文件
#include <QEvent>
class AppDelegate : public QObject
class AppDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
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

View File

@ -1,5 +1,40 @@
#include "applistmodel.h"
AppListModel::AppListModel(QObject *parent)
: QObject{parent}
{}
AppListModel::AppListModel(QObject *parent) : QAbstractListModel(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();
}

View File

@ -1,15 +1,26 @@
#ifndef APPLISTMODEL_H
#define APPLISTMODEL_H
#include <QObject>
#include <QAbstractListModel>
#include <QJsonArray>
// 添加 QJsonObject 头文件
#include <QJsonObject>
class AppListModel : public QObject
class AppListModel : public QAbstractListModel
{
Q_OBJECT
public:
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

View File

@ -4,8 +4,21 @@
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_model(new AppListModel(this))
, m_delegate(new AppDelegate(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();
initStyle();
}
@ -102,9 +115,8 @@ void MainWindow::initStyle()
void MainWindow::checkUpdates()
{
aptssUpdater updater;
// 获取可更新包列表
updater.getUpdateInfoAsJson();
QJsonArray updateInfo = updater.getUpdateInfoAsJson();
m_model->setUpdateData(updateInfo);
}

View File

@ -3,6 +3,10 @@
#include <QMainWindow>
#include "aptssupdater.h"
#include "applistmodel.h"
#include "appdelegate.h"
#include <QListView>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
@ -21,5 +25,8 @@ private:
Ui::MainWindow *ui;
void checkUpdates();
void initStyle();
AppListModel *m_model;
AppDelegate *m_delegate;
QListView *listView; // 声明 QListView 指针
};
#endif // MAINWINDOW_H