How to send JS object to QML from C++



  • Hello everyone! I couldn't find answer to my question. I want to draw a some MapPolyLine on Map, but I get data from model using QSQLQueryModel.

    class TracksModel : public QSqlQueryModel
    {
        Q_OBJECT
    public:
        enum Roles {
            IdRole = Qt::UserRole + 1,
            NameRole,
            PointsRole
        };
    
        explicit TracksModel(QObject *parent = 0);
        QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
    
    protected:
        QVector<QVariantList> tracks;
        QHash<int, QByteArray> roleNames() const;
        QVector<QVariantList>  getPointsOfTracks();
    
    signals:
    
    public slots:
        void updateModel();
        int getId(int row);
    };
    

    I need that all points of a single track contained in model.points. I use it in QML:

    MapItemView{
            id: tracksLines
            model: tracksModel
            delegate: MapPolyline {
                line.width: 3
                line.color: 'blue'
                path: tracksModel.points
            }
        }
    

    But, It Doesn't work. Ok, I tested next code:

    MapItemView{
            id: tracksLines
            model: tracksModel
            delegate:
                MapPolyline {
                line.width: 3
                line.color: 'red'
                path: [
                    { latitude: points[0].latitude, longitude: points[0].longtitude},
                    { latitude: points[1].latitude, longitude: points[1].longtitude}
                ]
            }
        }
    

    And It works! But I don't know how many points will be in the "poinst" and I don't know how many tracks therefore I use the model. My data is stored in the database.

    Now let's move to c++.
    It's short tracksmodel.cpp:

    QVariant TracksModel::data(const QModelIndex & index, int role) const {
        if (role == PointsRole)
        {
            return tracks[index.row()];
        }
    
        int columnId = role - Qt::UserRole - 1;
        QModelIndex modelIndex = this->index(index.row(), columnId);
        return QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
    }
    
    QHash<int, QByteArray> TracksModel::roleNames() const {
        QHash<int, QByteArray> roles;
        roles[IdRole] = "id";
        roles[NameRole] = "name";
        roles[PointsRole] = "points";
        return roles;
    }
    
    void TracksModel::updateModel()
    {
        this->tracks = this->getPointsOfTracks();
        QString str_query("SELECT id, name FROM Tracks;");
        this->setQuery(str_query);
        qDebug() << str_query << endl;
        qDebug() << this->tracks[0][0] << endl;
    }
    
    QVector<QVariantList> TracksModel::getPointsOfTracks()
    {
        QVector<QVariantList> temp_tracks;
        for (int i = 0; i <= 3; i++) // обрабатываем каждый трек
        {
           // 2 constant points for expirement
           QVariantList path;
           QVariantMap point;
           point["latitude"] = QVariant(55.928848 + (0.000680 * i));
           point["longtitude"] = QVariant(37.519537 + (0.002200 * i));
           path.append(point);
          
           QVariantMap point2;
           point2["latitude"] = QVariant(55.928169 + (0.000680 * i));
           point2["longtitude"] = QVariant(37.521683 + (0.002200 * i));
           path.append(point2);
           temp_tracks.push_back(path);
        }
        return temp_tracks;
    }
    

    In general, how to draw MapPolyLine with C++ data?
    Thank you in advance!
    P.S.Sorry for Google translator.



  • I have found that the function processes the data: http://code.qt.io/cgit/qt/qtlocation.git/tree/src/imports/location/qdeclarativepolylinemapitem.cpp#n525 . I get error "QML MapPolyline: Unsupported path type" when I set path. I'm trying to send an QJSValue, but I receive an error.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.