QGeoView icon indicating copy to clipboard operation
QGeoView copied to clipboard

Using QGeoView in ui.qml

Open Tevhit opened this issue 3 years ago • 9 comments

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

Tevhit avatar Feb 11 '22 08:02 Tevhit

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.

embeddedmz avatar Feb 11 '22 11:02 embeddedmz

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

Tevhit avatar Feb 11 '22 11:02 Tevhit

Are you sure ?

embeddedmz avatar Feb 11 '22 11:02 embeddedmz

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

Tevhit avatar Feb 11 '22 11:02 Tevhit

Is it correct?

Tevhit avatar Feb 11 '22 12:02 Tevhit

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

AmonRaNet avatar Feb 11 '22 12:02 AmonRaNet

Stick to Qt 5 if you can.

Regards.

embeddedmz avatar Feb 11 '22 17:02 embeddedmz

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.

Tevhit avatar Feb 22 '22 05:02 Tevhit

@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();
       }
    }
}

AmonRaNet avatar Feb 22 '22 10:02 AmonRaNet