AmonRaNet / QGeoView

QGeoView is a Qt / C ++ widget for visualizing geographic data.
GNU Lesser General Public License v3.0
149 stars 56 forks source link

Using QGeoView in ui.qml #24

Open Tevhit opened 2 years ago

Tevhit commented 2 years ago

How can I use QGeoView widget in a sample ui.qml file?

embeddedmz commented 2 years ago

This project was created to offer an alternative to Qt maps that are only available in QML. So use Qt maps (QLocation ) if you need to add a map to your QML program.

Tevhit commented 2 years ago

Qt Location and Qt Positioning are removed modules for QT6. So, how can I use QLocation in my QML program?

embeddedmz commented 2 years ago

Are you sure ?

Tevhit commented 2 years ago

https://doc-snapshots.qt.io/qt6-dev/whatsnew60.html

Tevhit commented 2 years ago

Is it correct?

AmonRaNet commented 2 years ago

QGeoView is QtWidget solution and not well adopted to be used in QML. In any case in theory it is possible (but I have not tried yet, therefore in theory). QGeoView based on QGraphicsView, which has method render (https://doc.qt.io/qt-5/qgraphicsview.html#render) and as result you can render view to any painter, including QML ones. You can create custom QQuickPaintedItem and override paint method to render QGeoView directly to painter (instance of QGeoView will be no rendered by itself). It can be still fast enough if OpenGL is enabled (https://doc.qt.io/qt-5/qquickpainteditem.html). But this is only about render - it mean you will need to propagate all events from QML (resize, mouse, keyboard) to QGeoView. Also all QGeoView widgets (compas, scale, zoom buttons) will not work and you will need to create QML based alternative. At the end you will have QGeoView rendered as "image" in QML scene with control by QML events. If you will give a try - please inform us about results :wink:

P.S. here is related topic: https://forum.qt.io/topic/74727/rendering-qgraphicsscene-into-qml-scene-texture

embeddedmz commented 2 years ago

Stick to Qt 5 if you can.

Regards.

Tevhit commented 2 years ago

Hello @AmonRaNet, We tried as that; we created a class, it has inheritance from QQuickPaintedItem Class. We overrided paint() method for rendering. Initial the QGeoView map rendered and painted. It can shown in QML as an image. So, it is not possible touching on the map. It was an image on screen. Do you have an idea for touching on the map in QML, of course we have to keep in mind it is an image in QML. Thanks all.

AmonRaNet commented 2 years ago

@Tevhit You need to forward mouse/touch events from QML to QGeoView.

Lets say you will create some proxy QObject class and will add it instance as property to QQuickPaintedItem, than you can access it over QML. Very simplified (you need to send all events press, release, etc):

in cpp:

class Proxy: public QObject
{
Q_OBJECT
public:
Proxy(QGeoView* view) : m_view{view} {}

public slots:
void mousePress(x,y,...) {
  QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress, QPoint{x,y}, Qt::LeftButton, Qt::NoButton,  Qt::NoModifier);
  QCoreApplication::postEvent(m_view, event);
}
// other events...
}

class MyQQuickPaintedItem: public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(Proxy* myProxy MEMBER myProxy CONSTANT)
}

in QML:

MyQQuickPaintedItem {
    id: painter
    width: 100; height: 100

    MouseArea {
        anchors.fill: parent
        onPressed: { 
            painter.myProxy.mousePress(mouse.x, mouse.y,...);
            painter.update();
       }
    }
}