子类化 QAbstractTableModel

发布于 2024-11-02 08:17:35 字数 2094 浏览 7 评论 0原文

我对 QAbstractTableModel 进行了子类化以表示来自 QMap 的数据。该 QMap 具有 QSqlRecords 的 QList,并且该映射由我的代码的其他部分修改。我想将此模型与 QTableView 结合使用,以显示此映射中每个键的 sql 记录。这是我的代码。

//mymodel.h

class MyModel : public QAbstractTableModel
{
Q_OBJECT

public:
    MyModel(QObject *parent = 0);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    void setRecordMap(QMap<int, QList<QSqlRecord>> *map);
    void setSelectedSerMsgIndex(QModelIndex *index);

private:
    QMap<int, QList<QSqlRecord>> *recordMap;
    QModelIndex *selectedSerendibMsgIndex;
};

//mymodel.cpp

MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
{

}

int MyModel::rowCount(const QModelIndex &parent) const
{
    if(recordMap->isEmpty())
        return 0;

    int row = selectedSerendibMsgIndex->row();
    return recordMap->value(row).size();
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    if(recordMap->isEmpty())
        return 0;

    int row = selectedSerendibMsgIndex->row();
    return recordMap->value(row).at(0).count();
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if(recordMap->isEmpty())
        return QVariant();

    if (!index.isValid())
        return QVariant();

    int row = selectedSerendibMsgIndex->row();
    if (index.row() >= recordMap->value(row).size())
        return QVariant();

    if (role == Qt::DisplayRole) 
    {
        return  recordMap->value(row).value(index.row()).value(index.column()); /* QVariant("hello");*/
    } 
    else 
    {
        return QVariant();
    }
}

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    recordMap = map;
}

void MyModel::setSelectedSerMsgIndex(QModelIndex *index)
{
    selectedSerendibMsgIndex = index;
}

抱歉这篇文章太长了。但问题是,我看不到地图上的数据。我猜这是因为我的 data() 方法的实现有问题。但我不知道它是什么。请善意地帮助我。谢谢。

I have subclassed a QAbstractTableModel to represent data from a QMap. This QMap has QLists of QSqlRecords and this map is modified by some other part of my code. I want to use this model with a QTableView to display the sql records in this map for each key. Here is my code.

//mymodel.h

class MyModel : public QAbstractTableModel
{
Q_OBJECT

public:
    MyModel(QObject *parent = 0);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role) const;
    void setRecordMap(QMap<int, QList<QSqlRecord>> *map);
    void setSelectedSerMsgIndex(QModelIndex *index);

private:
    QMap<int, QList<QSqlRecord>> *recordMap;
    QModelIndex *selectedSerendibMsgIndex;
};

//mymodel.cpp

MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
{

}

int MyModel::rowCount(const QModelIndex &parent) const
{
    if(recordMap->isEmpty())
        return 0;

    int row = selectedSerendibMsgIndex->row();
    return recordMap->value(row).size();
}

int MyModel::columnCount(const QModelIndex &parent) const
{
    if(recordMap->isEmpty())
        return 0;

    int row = selectedSerendibMsgIndex->row();
    return recordMap->value(row).at(0).count();
}

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if(recordMap->isEmpty())
        return QVariant();

    if (!index.isValid())
        return QVariant();

    int row = selectedSerendibMsgIndex->row();
    if (index.row() >= recordMap->value(row).size())
        return QVariant();

    if (role == Qt::DisplayRole) 
    {
        return  recordMap->value(row).value(index.row()).value(index.column()); /* QVariant("hello");*/
    } 
    else 
    {
        return QVariant();
    }
}

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    recordMap = map;
}

void MyModel::setSelectedSerMsgIndex(QModelIndex *index)
{
    selectedSerendibMsgIndex = index;
}

Sorry for the huge post. But the problem is, I cannot see the data from the map. I am guessing it is because there's something wrong with my implementation of the data() method. But I can't figure out what it is. Please be kind enough to help me. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

め可乐爱微笑 2024-11-09 08:17:35

尝试将其更改

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    recordMap = map;
}

为:

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    beginResetModel();
    recordMap = map;
    endResetModel();
}

try changing this:

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    recordMap = map;
}

to this:

void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
{
    beginResetModel();
    recordMap = map;
    endResetModel();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文