Results 1 to 17 of 17

Thread: Embedded Map in QWidget c++

  1. #1
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Embedded Map in QWidget c++

    Hi Community,

    i would like to embed an QML-Map into a widget.
    From general: i have successfully embedded a text (QML) with the following code:

    Qt Code:
    1. QQmlEngine *engine = new QQmlEngine(this);
    2. QQuickWidget *view = new QQuickWidget(engine, this);
    3. view->setSource(QUrl("qrc:/test.qml"));
    4. this->setCentralWidget(view);
    To copy to clipboard, switch view to plain text mode 

    test.qml is taken from an example.

    Embedding the following Map.qml does yield to syntax error at line 13. I doubt the reason is that i don't have a "parent".
    Qt Code:
    1. import QtQuick 2.0
    2. import QtLocation 5.3
    3.  
    4. Plugin {
    5. name: "osm"
    6. PluginParameter { name: "osm.useragent"; value "MapViewer"}
    7. PluginParameter { name: "osm.mapping.host"; value: "http://osm.tile.server.address/" }
    8. PluginParameter { name: "osm.mapping.copyright"; value: "All mine" }
    9. PluginParameter { name: "osm.routing.host"; value: "http://osrm.server.address/viaroute" }
    10. PluginParameter { name: "osm.geocoding.host"; value: "http://geocoding.server.address" }
    11. }
    12.  
    13. Map {
    14. id: map
    15.  
    16. plugin: osm
    17.  
    18. zoomLevel: map.minimumZoomLevel
    19.  
    20. center {
    21. // The Qt Company in Oslo
    22. latitude: 59.9485
    23. longitude: 10.7686
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    The code shall be embedded in a qwidget because it is part of an existing library that is loaded into a main application.
    Could someone give me a hint where i can find a good example? The mapviewer example vom QT-documentation does not hold enough background information how to create a qwidget holding a QML-Map.

    Thx!!!!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    A QML file has one and only one top level element, yours has two.

    Cheers,
    _

    P.S.: if you are looking for a widget based map view, look here https://inqlude.org/libraries/marble.html

  3. The following user says thank you to anda_skoa for this useful post:

    froeben (15th February 2016)

  4. #3
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    I will have a look at the mentioned library. Btw. ist "Item" the best container for what i am supposed to do?


    Added after 5 minutes:


    The project seems to be much to complex. Do they make use of QML?

    Quote Originally Posted by anda_skoa View Post
    A QML file has one and only one top level element, yours has two.

    Cheers,
    _

    P.S.: if you are looking for a widget based map view, look here https://inqlude.org/libraries/marble.html
    The project seems to be much to complex. Do they make use of QML?


    Added after 48 minutes:


    After searching the web, i found something which seems to yield to a valid qml-file. But i need some advice if i am on the right track.
    Qt Code:
    1. import QtQuick 2.0
    2. import QtPositioning 5.6
    3. import QtLocation 5.6
    4.  
    5. Item{
    6. id: container
    7.  
    8. Plugin {
    9. id: myplugin
    10.  
    11. name: "osm"
    12. PluginParameter { name: "osm.useragent"; value: "My great Qt OSM application" }
    13. PluginParameter { name: "osm.mapping.host"; value: "http://osm.tile.server.address/" }
    14. PluginParameter { name: "osm.mapping.copyright"; value: "All mine" }
    15. PluginParameter { name: "osm.routing.host"; value: "http://osrm.server.address/viaroute" }
    16. PluginParameter { name: "osm.geocoding.host"; value: "http://geocoding.server.address" }
    17. }
    18.  
    19. Map {
    20. id: map
    21.  
    22. plugin: myplugin
    23. center {
    24. latitude: -27
    25. longitude: 153
    26. }
    27. zoomLevel: map.minimumZoomLevel
    28. gesture.enabled: true
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    Is the way of bringing data into the widget correct?

    Qt Code:
    1. QQmlEngine *engine = new QQmlEngine(this);
    2. QQuickWidget *view = new QQuickWidget(engine, this);
    3.  
    4. view->setSource(QUrl("qrc:/map.qml"));
    5.  
    6. this->setCentralWidget(view);
    To copy to clipboard, switch view to plain text mode 

    I don't see any error during build ....


    Advice appreciated!

    Thx!
    Last edited by froeben; 15th February 2016 at 16:32.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    Quote Originally Posted by froeben View Post
    The project seems to be much to complex. Do they make use of QML?
    Marble provides a map widget. I just thought I mention it since you have a widget based application.

    Quote Originally Posted by froeben View Post
    After searching the web, i found something which seems to yield to a valid qml-file. But i need some advice if i am on the right track.
    Yes, that looks ok.
    You could also try to have the Map as the root element and put the plugin into it.
    Qt Code:
    1. import QtPositioning 5.6
    2. import QtLocation 5.6
    3.  
    4. Map {
    5. id: map
    6.  
    7. plugin: Plugin {
    8.  
    9. name: "osm"
    10. PluginParameter { name: "osm.useragent"; value: "My great Qt OSM application" }
    11. PluginParameter { name: "osm.mapping.host"; value: "http://osm.tile.server.address/" }
    12. PluginParameter { name: "osm.mapping.copyright"; value: "All mine" }
    13. PluginParameter { name: "osm.routing.host"; value: "http://osrm.server.address/viaroute" }
    14. PluginParameter { name: "osm.geocoding.host"; value: "http://geocoding.server.address" }
    15. }
    16.  
    17.  
    18. //...
    19. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by froeben View Post
    Is the way of bringing data into the widget correct?
    Don't know, never worked with the QtLocation API before.

    Quote Originally Posted by froeben View Post
    Qt Code:
    1. QQmlEngine *engine = new QQmlEngine(this);
    2. QQuickWidget *view = new QQuickWidget(engine, this);
    3.  
    4. view->setSource(QUrl("qrc:/map.qml"));
    5.  
    6. this->setCentralWidget(view);
    To copy to clipboard, switch view to plain text mode 

    I don't see any error during build ....
    That looks fine. You don't even need to explictly create the QQmlEngine.

    Cheers,
    _

  6. #5
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    Quote Originally Posted by anda_skoa View Post
    Marble provides a map widget. I just thought I mention it since you have a widget based application.


    Yes, that looks ok.
    You could also try to have the Map as the root element and put the plugin into it.
    Qt Code:
    1. import QtPositioning 5.6
    2. import QtLocation 5.6
    3.  
    4. Map {
    5. id: map
    6.  
    7. plugin: Plugin {
    8.  
    9. name: "osm"
    10. PluginParameter { name: "osm.useragent"; value: "My great Qt OSM application" }
    11. PluginParameter { name: "osm.mapping.host"; value: "http://osm.tile.server.address/" }
    12. PluginParameter { name: "osm.mapping.copyright"; value: "All mine" }
    13. PluginParameter { name: "osm.routing.host"; value: "http://osrm.server.address/viaroute" }
    14. PluginParameter { name: "osm.geocoding.host"; value: "http://geocoding.server.address" }
    15. }
    16.  
    17.  
    18. //...
    19. }
    To copy to clipboard, switch view to plain text mode 


    Don't know, never worked with the QtLocation API before.


    That looks fine. You don't even need to explictly create the QQmlEngine.

    Cheers,
    _
    So, the qml is loaded correctly, the QQmlError list is empty (have checked, code returns "OK"):
    Qt Code:
    1. QQmlEngine *engine = new QQmlEngine(this);
    2. QQuickWidget *view = new QQuickWidget(engine, this);
    3. view->setSource(QUrl("qrc:/map.qml"));
    4.  
    5. this->setCentralWidget(view);
    6.  
    7. QList<QQmlError> err = view->errors();
    8. for (QList<QQmlError>::iterator i = err.begin(); i != err.end(); ++i)
    9. {
    10. qDebug() << *i;
    11. }
    12. if (err.isEmpty())
    13. {
    14. qDebug() << "OK";
    15. }
    To copy to clipboard, switch view to plain text mode 

    But I don't see an image!

    So i assume some possible problems:
    1. The size of the map is not set and thus, the object is not drawn.
    -- When using text, the size is given by "font.pointSize". Will check tomorrow? Any idea, anyone?
    2. Do i need to set the Map to visible?
    -- ???
    3. My OSM plugin is trying to contact the wrong server.
    -- Can anyone check? I didnot find different server information for "OSM" server url.
    4. My firewall is blocking access.
    -- Which port does Qt use for requesting data from OSM server? Is it port 80? Can it be configured?

    Regards,
    Frank

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    Quote Originally Posted by froeben View Post
    1. The size of the map is not set and thus, the object is not drawn.
    -- When using text, the size is given by "font.pointSize". Will check tomorrow? Any idea, anyone?
    Qt Code:
    1. view->setResizeMode(QQuickWidget::SizeRootObjectToView);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by froeben View Post
    2. Do i need to set the Map to visible?
    -- ???
    That should not be necessary.

    Quote Originally Posted by froeben View Post
    3. My OSM plugin is trying to contact the wrong server.
    -- Can anyone check? I didnot find different server information for "OSM" server url.
    No idea.

    Quote Originally Posted by froeben View Post
    4. My firewall is blocking access.
    -- Which port does Qt use for requesting data from OSM server? Is it port 80? Can it be configured?
    If you provide http URLs it will be port 80 unless the URL has a port number.

    Cheers,
    _

  8. #7
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    So, taking a look at my possible issues:

    1. The size of the map is not set and thus, the object is not drawn.
    Setting
    Qt Code:
    1. view->setResizeMode(QQuickWidget::SizeRootObjectToView);
    To copy to clipboard, switch view to plain text mode 
    did not change anything. No image visualized. :-(

    2. Do i need to set the Map to visible?
    Setting
    Qt Code:
    1. view->setVisible(true);
    To copy to clipboard, switch view to plain text mode 
    did not yield to a displayed map. :-/

    3. My OSM plugin is trying to contact the wrong server.
    I currently don't find any better servers as mentioned in the examples. Can anyone provide working tile server from Open Streetmap?

    4. My firewall is blocking access.
    Since it shall be a port 80 request, the firewall should not be a problem.


    Further ideas:
    How about additional debugging options for the qml plugin object? Are there any debug options that could give me more information about what might get wrong.

    Thx.
    Frank

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    Quote Originally Posted by froeben View Post
    1. The size of the map is not set and thus, the object is not drawn.
    Setting
    Qt Code:
    1. view->setResizeMode(QQuickWidget::SizeRootObjectToView);
    To copy to clipboard, switch view to plain text mode 
    did not change anything. No image visualized. :-(
    And Map is your top level element in the QML file?

    Quote Originally Posted by froeben View Post
    2. Do i need to set the Map to visible?
    Setting
    Qt Code:
    1. view->setVisible(true);
    To copy to clipboard, switch view to plain text mode 
    did not yield to a displayed map. :-/
    view is the window's central widget, it is visible when the window is unless you explicitly hide it.

    Quote Originally Posted by froeben View Post
    Further ideas:
    How about additional debugging options for the qml plugin object? Are there any debug options that could give me more information about what might get wrong.
    Have you investigates the values of the Map's error and errorString properties?

    Cheers,
    _

  10. #9
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    Looking into the example (http://doc.qt.io/qt-5/qtlocation-map...-main-cpp.html), is see a difference in loading the information:
    Qt Code:
    1. engine.addImportPath(QStringLiteral(":/imports"));
    2. engine.load(QUrl(QStringLiteral("qrc:///mapviewer.qml")));
    To copy to clipboard, switch view to plain text mode 

    In my code, i create a new QQuickWidget and assign the source of the widget:
    Qt Code:
    1. QQmlEngine *engine = new QQmlEngine(this);
    2. QQuickWidget *view = new QQuickWidget(engine, this);
    3. view->setSource(QUrl("qrc:/map.qml"));
    To copy to clipboard, switch view to plain text mode 

    Is the effect the same or is there something i do not get from the examples?


    Added after 8 minutes:


    My QML-file currently is this:

    Qt Code:
    1. import QtQuick 2.3
    2. import QtPositioning 5.6
    3. import QtLocation 5.6
    4.  
    5. Item{
    6. id: container
    7.  
    8. Plugin {
    9. id: myplugin
    10.  
    11. name: "osm"
    12. PluginParameter { name: "osm.useragent"; value: "My great Qt OSM application" }
    13. PluginParameter { name: "osm.mapping.host"; value: "http://osm.tile.server.address/" }
    14. PluginParameter { name: "osm.mapping.copyright"; value: "All mine" }
    15. PluginParameter { name: "osm.routing.host"; value: "http://osrm.server.address/viaroute" }
    16. PluginParameter { name: "osm.geocoding.host"; value: "http://geocoding.server.address" }
    17. }
    18. Map {
    19. id: map
    20.  
    21. plugin: myplugin
    22. center {
    23. latitude: -27
    24. longitude: 153
    25. }
    26. zoomLevel: map.minimumZoomLevel
    27. gesture.enabled: true
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    From my understanding of the example, "plugin" cannot be a child of Map.
    If i modify it to have the plugin as a child of Map, the project crashes during runtime.
    Last edited by froeben; 16th February 2016 at 10:03.

  11. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    Quote Originally Posted by froeben View Post
    Is the effect the same or is there something i do not get from the examples?
    Yes, that's basically the same.

    Quote Originally Posted by froeben View Post
    My QML-file currently is this:

    Qt Code:
    1. import QtQuick 2.3
    2. import QtPositioning 5.6
    3. import QtLocation 5.6
    4.  
    5. Item{
    6. id: container
    7.  
    8. Plugin {
    9. id: myplugin
    10.  
    11. name: "osm"
    12. PluginParameter { name: "osm.useragent"; value: "My great Qt OSM application" }
    13. PluginParameter { name: "osm.mapping.host"; value: "http://osm.tile.server.address/" }
    14. PluginParameter { name: "osm.mapping.copyright"; value: "All mine" }
    15. PluginParameter { name: "osm.routing.host"; value: "http://osrm.server.address/viaroute" }
    16. PluginParameter { name: "osm.geocoding.host"; value: "http://geocoding.server.address" }
    17. }
    18. Map {
    19. id: map
    20.  
    21. plugin: myplugin
    22. center {
    23. latitude: -27
    24. longitude: 153
    25. }
    26. zoomLevel: map.minimumZoomLevel
    27. gesture.enabled: true
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 
    Ah, so Map is not your top level element, then of course it still has no size.
    You need to make the Map element follow the size of the Item, since that is the element resized by the view.

    Qt Code:
    1. Map {
    2. anchors.fill: parent
    3. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by froeben View Post
    From my understanding of the example, "plugin" cannot be a child of Map.
    Ok. Putting it directly into the plugin property should still have worked.

    Can I ask which features you are targetting that makes you choose an embedded QtQuick scene over the widget solution of Marble?

    Cheers,
    _

  12. #11
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    Quote Originally Posted by anda_skoa View Post
    Yes, that's basically the same.


    Ah, so Map is not your top level element, then of course it still has no size.
    You need to make the Map element follow the size of the Item, since that is the element resized by the view.

    Qt Code:
    1. Map {
    2. anchors.fill: parent
    3. }
    To copy to clipboard, switch view to plain text mode 


    Ok. Putting it directly into the plugin property should still have worked.

    Can I ask which features you are targetting that makes you choose an embedded QtQuick scene over the widget solution of Marble?

    Cheers,
    _
    I have had a look at Marble, which seems to be a nice tool. But i want to understand from scratch how it works. Using another framework to get something working that you don't understand is also a pain in the ass. The problem remains: if something goes wrong, how do you find the problem...

    The features are available in QT and shall be usable. The question to me is, am i doing it correctly? :-D Do I have a wrong understanding of the tools?
    If it turns out, that the official documentation does not give you enough information to get it running, than something is weird.

  13. #12
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    Well, I was just asking because you are mixing different UI technologies which increases the complexity of any error analysis.
    So I assumed you needed QtQuick for something in particular, e.g. certain effects, animations, etc.

    Cheers,
    _

  14. #13
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    Nope, don't need QtQuick but even if you delete it, there is no image :-D

  15. #14
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    I am not sure I follow.

    If you remove the QQuickWidget, what image to you expect to see?

    Cheers,
    _

  16. #15
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    Forget my last post ...

    Small changes to QML (without success):
    Qt Code:
    1. import QtQuick 2.3
    2. import QtPositioning 5.6
    3. import QtLocation 5.6
    4.  
    5. Map {
    6. id: map
    7. anchors.fill: parent;
    8.  
    9. plugin: Plugin {
    10. name: "osm"
    11. }
    12.  
    13. center {
    14. latitude: -27
    15. longitude: 153
    16. }
    17. zoomLevel: map.minimumZoomLevel
    18. gesture.enabled: true
    19. }
    To copy to clipboard, switch view to plain text mode 

  17. #16
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Embedded Map in QWidget c++

    anchors.fill: parent only makes sense if there is a parent.

    I guess you need to look at the error properties.

    maybe something like

    Qt Code:
    1. onErrorChanged: console.log("error=" + error);
    2. onErrorStringChanged: console.log("errorString=" + errorString);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  18. #17
    Join Date
    Feb 2016
    Posts
    16
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Embedded Map in QWidget c++

    So, network proxy was the problem.
    Needed to add qnetwork and needed to add
    Qt Code:
    1. QNetworkProxyFactory::setUseSystemConfiguration(true);
    To copy to clipboard, switch view to plain text mode 
    to get the system proxy settings.

    Only small problem left:
    i do get exceptions from sysfer.dll which happen if the following is executed:
    Qt Code:
    1. view->setSource(QUrl("qrc:/map.qml"));
    To copy to clipboard, switch view to plain text mode 

    Here are the files:
    QML:
    Qt Code:
    1. import QtQuick 2.3
    2. import QtPositioning 5.6
    3. import QtLocation 5.6
    4.  
    5. Map {
    6. id: map
    7. width: 640
    8. height:480
    9.  
    10. plugin: Plugin {
    11. name: "osm"
    12. }
    13.  
    14. center {
    15. latitude: -27
    16. longitude: 153
    17. }
    18. zoomLevel: 10
    19. gesture.enabled: true
    20.  
    21. Component.onCompleted: {
    22. console.log("Dimensions: ", width, height)
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Mainwindow.cpp:
    Qt Code:
    1. MainWindow::MainWindow() : QMainWindow ()
    2. {
    3. // embed resources into binary
    4. // infos: http://doc.qt.io/qt-5/resources.html
    5. Q_INIT_RESOURCE(qmlsources);
    6.  
    7. this->resize(1024, 768);
    8.  
    9. createActions();
    10. createMenus();
    11.  
    12. QQmlEngine *engine = new QQmlEngine(this);
    13. QQuickWidget *view = new QQuickWidget(engine, this);
    14. view->setSource(QUrl("qrc:/map.qml"));
    15. this->setCentralWidget(view);
    16. view->setResizeMode(QQuickWidget::SizeRootObjectToView);
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. // load resources from binary
    5. // infos: http://doc.qt.io/qt-5/resources.html
    6. Q_INIT_RESOURCE(qmlsources);
    7.  
    8. QNetworkProxyFactory::setUseSystemConfiguration(true);
    9. MainWindow mw;
    10. mw.show();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by froeben; 16th February 2016 at 17:34.

Similar Threads

  1. Replies: 2
    Last Post: 16th April 2013, 18:49
  2. Replies: 1
    Last Post: 16th September 2010, 16:57
  3. Multiple apps using Qt/Embedded+Qtopia on Embedded Linux
    By drahardja in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 17th February 2008, 22:46
  4. Replies: 1
    Last Post: 2nd May 2006, 22:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.