代码之家  ›  专栏  ›  技术社区  ›  kontiki

从代理筛选器中的子类访问数据

  •  0
  • kontiki  · 技术社区  · 6 年前

    我正在尝试从我自己的NavaidsModel类中的QGeoCoordinate获取数据。

    以下是NavaidsModel的构造函数:

    class NavaidsModel : public QAbstractListModel
    {
        Q_OBJECT
    public:
    
    NavaidsModel(QObject *parent = Q_NULLPTR):QAbstractListModel(parent){
    }
    enum NavaidsRoles {
        PositionRole = Qt::UserRole + 1, 
        OACICodeRole,
        CountryCodeRole
    };
    

    以下是proxyfilter NavaidsFilter的filterAcceptsRow():

    bool NavaidsFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
    {
        QRegExp rx("ROBU");
    
        QAbstractItemModel *model = sourceModel();
    
            QHashIterator<int, QByteArray> it(sourceModel()->roleNames());
            while (it.hasNext()) {
                it.next();
                QModelIndex sourceIndex = model->index(sourceRow, 0, sourceParent);
    
                //Here are the tests to get the data
                qDebug() <<"Data 257 :" << sourceIndex.data(257); //PositionRole
                qDebug() <<"Data 258 :" << sourceIndex.data(258); //OACICodeRole
                qDebug() <<"Data 259 :" << sourceIndex.data(259); //CountryCodeRole
    
                QString key = model->data(sourceIndex, it.key()).toString();
                if (key.contains(rx))
                    return true;
            }
            return false;
    }
    

    以下是qDebug()结果:

    index.row =  0 role =  257
    Point :  "MM"   "ROBSO"   QGeoCoordinate(21.75, -107.12556, 0)
    PositionRole  QGeoCoordinate(21.75, -107.12556, 0) 
    Data 257 : QVariant(QGeoCoordinate, )
    
    index.row =  0 role =  258
    Point :  "MM"   "ROBSO"   QGeoCoordinate(21.75, -107.12556, 0)
    OACICodeRole  "ROBSO" 
    Data 258 : QVariant(QString, "ROBSO")
    
    
    index.row =  0 role =  259
    Point :  "MM"   "ROBSO"   QGeoCoordinate(21.75, -107.12556, 0)
    CountryCodeRole  "MM" 
    Data 259 : QVariant(QString, "MM")
    

    正如我们在结果中所看到的那样,OACICode和CountryCode都可以。 但是,对于数据257,我希望得到值(lat=21.75;lon=-107.12556;alt=0),以便在边界限制内进行比较,目前,我无法以任何方式获得它们。

    我怎样才能做到这一点?

    谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  0
  •   eyllanesc    6 年前

    假设您使用的是我在前一个问题中向您展示的模型:

    QVariant data(const QModelIndex & index, int role=Qt::DisplayRole) const {
        if (index.row() < 0 || index.row() >= mPoints.count())
            return QVariant();
    
        const NavaidsPoint &point = mPoints[index.row()];
        if (role == PositionRole)
            return QVariant::fromValue(point.position());
        else if (role == OACICodeRole)
            return point.oaciCode();
        else if (role == CountryCodeRole)
            return point.countryCode();
        return QVariant();
    }
    

    这个 data() 方法返回 QVariant ,但有一些默认情况下不支持的类型,要解决此问题,请使用 QVariant::fromValue() ,因此,如果要获取应使用的值 QVariant::value() const,如下所示。

    #ifndef NAVAIDSFILTER_H
    #define NAVAIDSFILTER_H
    
    #include "navaidsmodel.h"
    
    #include <QSortFilterProxyModel>
    #include <QRegularExpression>
    
    #include <QDebug>
    
    class NavaidsFilter : public QSortFilterProxyModel
    {
    public:
        NavaidsFilter(QObject *parent = Q_NULLPTR):QSortFilterProxyModel(parent){}
    protected:
        bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const{
    
            QRegularExpression rx("ROBU");
    
            QModelIndex ix = sourceModel()->index(source_row, 0, source_parent);
            QGeoCoordinate pos = ix.data(NavaidsModel::PositionRole).value<QGeoCoordinate>();
            QString code = ix.data(NavaidsModel::OACICodeRole).toString();
            QString country = ix.data(NavaidsModel::CountryCodeRole).toString();
    
            qDebug()<<pos<<code<<country;
    
            if(code.contains(rx)){
                return true;
            }
            return false;
        }
    };
    
    #endif // NAVAIDSFILTER_H
    

    注:

    • 不建议使用与角色关联的数字,最好使用枚举的值,因为它们使代码更具可读性。

    • 使用 QRegularExpression 而不是 QRegExp 因为这门课很快就会被淘汰。