» »

QMap z dvema kljucema - nadgradnja podatkovne strukture v QAbstractListModel

QMap z dvema kljucema - nadgradnja podatkovne strukture v QAbstractListModel

KernelPanic ::

Spostovane dame in gospodje!

Ne vem zakaj ne morem nadaljevati prejsnje teme, javlja mi neke html napake, upam, da me ne bo kdo kaznoval, saj bi rad nadaljeval z temo. Torej, na technolog-ov predlog uporabljam sedaj sledeco podatkovno strukturo:
typedef QMap<QPair<QString, QString>, QList<UeOrderRecord*>> UeTypeOrders;

Kot sem ze v prejsnji temi omenil, delam na Qt aplikaciji z QML graficnim vmesnikom in prvo vprasanje je tu:
Ce hocem to podatkovno strukturo uporabiti z QML ListView, ali moram to podatkovno strukturo "nadgraditi" v QAbstractListModel? Ker se mi dozdeva, da je odgovor pritrdilen, vam pokazem, kaj sem postoril do sedaj znotraj modela UeOrdersModel.
Header file:
#ifndef UEORDERSMODEL_H
#define UEORDERSMODEL_H

#include <QAbstractListModel>
#include <QList>
#include <QModelIndex>
#include <QVariant>
#include <QMap>
#include <QString>

#include "../core/uetypes.h"

class UeOrdersModel : public QAbstractListModel
{
    Q_OBJECT

private:
    UeTypeOrders* m_ueOrders;
    QString m_ueUserId;
    QString m_uePlaceId;

public:
    explicit UeOrdersModel(QObject *parent = 0,
                           const QString& userId="",
                           const QString& placeId="");

    inline UeTypeOrders* ueOrders() const
        { return this->m_ueOrders; }
    inline QString ueUserId() const
        { return this->m_ueUserId; }
    inline QString uePlaceId() const
        { return this->m_uePlaceId; }

    inline void ueSetOrders(UeTypeOrders* const ordersList)
        { this->m_ueOrders=ordersList; }
    inline void ueSetUserId(const QString& userId)
        { this->m_ueUserId=userId; }
    inline void ueSetPlaceId(const QString& placeId)
        { this->m_uePlaceId=placeId; }

    int rowCount(const QModelIndex &parent=QModelIndex()) const;
    QVariant data(const QModelIndex &index,
                  int role=Qt::DisplayRole) const;
    UeTypeRoles roleNames() const;
//    bool setData(const QModelIndex &index,
//                 const QVariant &value,
//                 int role);
//    Qt::ItemFlags flags(const QModelIndex &index) const;
    bool insertRows(int row,
                    int count,
                    const QModelIndex &parent);
    bool removeRows(int row,
                    int count,
                    const QModelIndex &parent);

public:
    static const int ueRoleUserId=Qt::UserRole+1;
    static const int ueRolePlaceId=Qt::UserRole+2;
    static const int ueRoleProductId=Qt::UserRole+3;
    static const int ueRoleProductName=Qt::UserRole+4;
    static const int ueRoleProductPriceSell=Qt::UserRole+5;
    static const int ueRoleProductQuantity=Qt::UserRole+6;
    static const int ueRoleOrderAmount=Qt::UserRole+7;

signals:

public slots:
};

#endif // UEORDERSMODEL_H
in njegova implementacija:
#include "ueordersmodel.h"

UeOrdersModel::UeOrdersModel(QObject *parent,
                             const QString& userId,
                             const QString& placeId)
    : QAbstractListModel(parent)
{
    QMap<QPair<QString, QString>, QList<UeOrderRecord*>> userPlaceOrders;

    this->ueSetUserId(userId);
    this->ueSetPlaceId(placeId);
    this->ueSetOrders(new UeTypeOrders());

    userPlaceOrders.insert(QPair<QString, QString>(this->ueUserId(), this->uePlaceId()), QList<UeOrderRecord*>());
}   // constructor

int UeOrdersModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);

    return this->ueOrders()->count();
}   // rowCount

QVariant UeOrdersModel::data(const QModelIndex &index,
                             int role) const
{
    switch(role)
    {
        case ueRoleUserId:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->ueUserId();
        } break;

        case ueRolePlaceId:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->uePlaceId();
        } break;

        case ueRoleProductId:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->ueProductId();
        } break;

        case ueRoleProductName:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->ueProductName();
        } break;

        case ueRoleProductPriceSell:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->ueProductPriceSell();
        } break;

        case ueRoleProductQuantity:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->ueProductQuantity();
        } break;

        case ueRoleOrderAmount:
        {
            return this->ueOrders()->value(QPair<QString, QString>(this->ueUserId(),this->uePlaceId())).at(index.row())->ueOrderAmount();
        } break;

        default:
        {
            return QVariant();
        } break;    // default
    }   // switch

    return QVariant();
}   // data

UeTypeRoles UeOrdersModel::roleNames() const
{
    UeTypeRoles roles;

    const int iRoleUserId=UeOrdersModel::ueRoleUserId;
    const int iRolePlaceId=UeOrdersModel::ueRolePlaceId;
    const int iRoleProductId=UeOrdersModel::ueRoleProductId;
    const int iRoleProductName=UeOrdersModel::ueRoleProductName;
    const int iRoleProductPriceSell=UeOrdersModel::ueRoleProductPriceSell;
    const int iRoleProductQuantity=UeOrdersModel::ueRoleProductQuantity;
    const int iRoleOrderAmount=UeOrdersModel::ueRoleOrderAmount;

    roles.insert(iRoleUserId,
                 "ueRoleUserId");
    roles.insert(iRolePlaceId,
                 "ueRolePlaceId");
    roles.insert(iRoleProductId,
                 "ueRoleProductId");
    roles.insert(iRoleProductName,
                 "ueRoleProductName");
    roles.insert(iRoleProductPriceSell,
                 "ueRoleProductPriceSell");
    roles.insert(iRoleProductQuantity,
                 "ueRoleProductQuantity");
    roles.insert(iRoleOrderAmount,
                 "ueRoleOrderAmount");

    return roles;
}   // roleNames

/*
bool UeOrdersModel::setData(const QModelIndex &index,
             const QVariant &value,
             int role)
{
}   // setData
*/

/*
Qt::ItemFlags UeOrdersModel::flags(const QModelIndex &index) const
{
}   // flags
*/

bool UeOrdersModel::insertRows(int row,
                int count,
                const QModelIndex &parent)
{
    bool result=false;

    beginInsertRows(parent,
                    0,
                    count);
    endInsertRows();

    return result;
}   // insertRows

bool UeOrdersModel::removeRows(int row,
                int count,
                const QModelIndex &parent)
{
    return false;
}   // removeRows

Za zacetek me zanima virtualna metoda bool UeOrdersModel::insertRows(int row, int count, const QModelIndex &parent), znotraj katere bi rad dodal record v zograj omenjeno privatno podatkovno strukturo ter nato emitiral signal o spremembi modela. Kako naj dopolnem to metodo, da bo vzela podatke iz QML GUIja (za1x ID_UPORABNIKA) in jih znotraj te metode prenesem v podatkovno strukturo?

Lp,
M


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Qt/QML zajem podatkov iz podatkovne baze, dobim samo opise fieldov ter prazne vrednos

Oddelek: Programiranje
71723 (1466) windigo
»

[Qt/C++]problem z Undefined reference na konstanti znotraj razreda

Oddelek: Programiranje
102340 (2010) phantom
»

c# - mssql kombinacija / problem pri zapisu row-a v bazo

Oddelek: Programiranje
121596 (1405) darkolord
»

Baza & c#

Oddelek: Programiranje
214156 (3214) xardas
»

C++ programi

Oddelek: Programiranje
72518 (2114) ERGY

Več podobnih tem