Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Problem integrating qt quick project with qt widgets project.
Forum Updated to NodeBB v4.3 + New Features

Problem integrating qt quick project with qt widgets project.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 329 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    saumayd
    wrote on 27 Jan 2021, 07:18 last edited by
    #1

    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
            }
    
    
        }
    
    }
    
    K 1 Reply Last reply 27 Jan 2021, 09:20
    0
    • S saumayd
      27 Jan 2021, 07:18

      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
              }
      
      
          }
      
      }
      
      K Offline
      K Offline
      KillerSmath
      wrote on 27 Jan 2021, 09:20 last edited by KillerSmath
      #2

      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

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      0

      1/2

      27 Jan 2021, 07:18

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved