Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem integrating qt quick project with qt widgets project.



  • I don't know a lot about qt so please bear with me.
    I have a qt widgets application that I made as a part of a project , a friend has a Qt quick applications project which I'd like to integrate into my qt widgets app.This is what I've added in my mainwindow.cpp

    QQuickView *view = new QQuickView();
        QWidget *container = QWidget::createWindowContainer(view, this);
        container->setMinimumSize(300, 200);
        container->setMaximumSize(600, 400);
    
        view->setSource(QUrl("/maps/main.qml")); file.
        ui->qmlwidget->addWidget(container);
    

    However , the integration doesn't work correctly.The qt quick project is a map which takes coordinates as inputs and then points them on the map.It works as expected when it is run standalone but when I integrate it using the above method , clicking the button which is supposed to point out the location does nothing.This is the Qt quick app code:

    gmap.cpp

        #include "gmap.h"
    
    gmap::gmap()
    {
    
    }
    
    void gmap::setData(QString lat,QString lang)
    {
        qDebug(lat.toLatin1());
    
        emit getLat(lat.toDouble());
        emit getLang(lang.toDouble());
    }
    

    gmap.h

        #ifndef GMAP_H
    #define GMAP_H
    
    #include <QObject>
    
    class gmap : public QObject
    {
        Q_OBJECT
    
    
    public:
        gmap();
    
    
    signals:
        void getLat(double lat);
        void getLang(double lang);
    
    
    public slots:
        void setData(QString lat,QString lang);
    
    
    
    };
    #endif // GMAP_H
    

    main.qml

        import QtQuick 2.6
    import QtQuick.Window 2.2;
    import QtPositioning 5.6;
    import QtLocation 5.9
    import Qt3D.Input 2.1
    import QtQuick.Controls 2.2;
    
    
    Window {
        width: Qt.platform.os == "android" ? Screen.width : 512
        height: Qt.platform.os == "android" ? Screen.height : 512
        visible: true
    
        Plugin {
            id: mapPlugin
            name: "osm"
            PluginParameter {
                 name: 'osm.mapping.highdpi_tiles'
                value: !!1      }
        }
    
        Connections{
    
            target: gmap
            onGetLat : mapmarker.center.latitude = lat
    
        }
        Connections{
    
            target: gmap
            onGetLang : mapmarker.center.longitude = lang
    
        }
        Connections{
    
            target: gmap
            onGetLang : map.center = QtPositioning.coordinate(mapmarker.center.latitude,mapmarker.center.longitude,150);
    
    
    
        }
    
        Map {
            id: map
            anchors.fill: parent
            anchors.rightMargin: -15
            anchors.bottomMargin: -10
            anchors.leftMargin: 15
            anchors.topMargin: 10
            plugin: mapPlugin
            center: QtPositioning.coordinate() // NSUT
            zoomLevel: 14
            activeMapType: supportedMapTypes[2]
    
    
            Button {
                x: 389
                y: 445
                text: "ADD MARKER"
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 27
                anchors.rightMargin: 23
                padding: 7
                onClicked: gmap.setData(textField.text,textField1.text)
            }
    
    
    
            MapCircle {
    
                id: mapmarker
                    center {
                        latitude: 28.6078
                        longitude: 77.0406
    
    
                    }
                    radius: 50.0
                    color: 'green'
                    border.width: 3
            }
    
    
            TextField {
                id: textField
                x: 176
                y: 397
                text: qsTr("")
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 75
                anchors.rightMargin: 136
            }
    
            TextField {
                id: textField1
                x: 176
                y: 445
                anchors.right: parent.right
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 27
                anchors.rightMargin: 136
                font.hintingPreference: Font.PreferDefaultHinting
            }
    
    
        }
    
    }


  • Hey @saumayd, welcome to Qt Forum.

    Where are you exporting the instance of object gmap to the qml ?

    [ERROR]: qrc:/maps/main.qml:26: ReferenceError: gmap is not defined

    Connections{
            target: gmap
            onGetLat : mapmarker.center.latitude = lat
     }
    

    Are you passing the object to the qml context by calling setProperty before of line: view->setSource(QUrl("/maps/main.qml")); ?

    #include <QQmlContext>
    
    ...
    
    Gmap *gmap = new Gmap(this); // wherever you're creating the instance of object gmap
    
    ...
    
    QQmlContext *ctxt = view->rootContext();
    ctxt->setContextProperty("gmap", gmap); // the property gmap is refered to gmap object in c++
    view->setSource(QUrl("/maps/main.qml"));
    

    Read more:
    https://doc.qt.io/qt-5/qtqml-cppintegration-overview.html
    https://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html


Log in to reply