- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试从 C++ 向我的 map 添加 QML map 项,例如 MapQuickItem 或 MapCircle。不幸的是,它们没有显示在 map 上。只需一个 QML 矩形的相同代码就可以工作。rect v1 是用于测试的矩形。 rect v2 是不起作用的 mapCircle。当我将我的圈子 QML 代码 1:1 添加到我的 map QML 代码中时,它工作正常。
main.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView *view = new QQuickView;
view->setSource(QUrl("qrc:/map.qml"));
QQmlComponent compRect (view->engine(), QUrl("qrc:/rect.qml"));
view->setWidth(1000);
view->setHeight(650);
view->setTitle("GUI");
QQuickItem *map = view->findChild<QQuickItem*>("map1");
QQuickItem *rect = qobject_cast<QQuickItem*>(compRect.create(view->rootContext()));
rect->setParentItem(map);
rect->setParent(map); //know this is not for visual objects, just for test
view->show();
return app.exec();
}
rect.qml v1
import QtQuick 2.14
import QtLocation 5.14
import QtPositioning 5.14
Rectangle
{
color: "grey"
opacity: .8
width: 100
height: 100
radius: 4
Text
{
id: text
anchors.centerIn: parent
text: "hi"
color: "orangered"
font.weight: Font.Bold
}
}
rect.qml v2
import QtQuick 2.14
import QtLocation 5.14
import QtPositioning 5.14
MapCircle
{
center
{
latitude: 47.5
longitude: 8.9
}
radius: 5000.0
color: 'green'
border.width: 3
}
map.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtPositioning 5.14
import QtLocation 5.14
Map
{
objectName: "map1"
id: map_map
anchors.centerIn: parent;
anchors.fill: parent
plugin: Plugin
{
name: "mapboxgl" // "osm", "esri", ...
}
center: QtPositioning.coordinate(47.6,9.5)
zoomLevel: 8
// get position of device and set map center to it
PositionSource
{
active: true
onPositionChanged:
{
map.center(position.coordinate)
console.log(position.coordinate)
}
}
Timer
{
id: timerReload
interval: 1000
repeat: true
running: true
onTriggered:
{
controller.triggerReload();
}
}
}
最佳答案
确定一个项目是 map 的子项并不意味着它显示在 map 上,如果你想添加一个项目你必须使用 addMapItem()
方法,在 C++ 中你可以使用 QMetaObject::invokeMethod()
调用该方法,但您需要访问属于私有(private) Qt API 的类型 QDeclarativeGeoMapItemBase
,考虑到上述解决方案是:
*.pro
QT += quick location location-private
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp
RESOURCES += qml.qrc
main.cpp
#include <QGuiApplication>
#include <QQuickItem>
#include <QQuickView>
#include <QtLocation/private/qdeclarativegeomapitembase_p.h>
int main(int argc, char *argv[]){
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl("qrc:/map.qml"));
view.resize(1000, 650);
view.setTitle("GUI");
view.show();
if(QQuickItem *map = view.findChild<QQuickItem*>("map1")){
QQmlComponent component(view.engine(), QUrl("qrc:/rect.qml"));
if(QDeclarativeGeoMapItemBase *rect = qobject_cast<QDeclarativeGeoMapItemBase*>(component.create(view.rootContext()))){
bool status = QMetaObject::invokeMethod(map,
"addMapItem",
Qt::DirectConnection,
Q_ARG(QDeclarativeGeoMapItemBase*, rect));
Q_ASSERT(status);
}
}
return app.exec();
}
前面的方法的问题是很危险,因为 map 或任何QML元素随时都可能被移除,这样就可能产生问题,而且它限制了QML的修改,最后需要访问私有(private) Qt API 可以更改,恕不另行通知。因此我会提供更好的选择:
使用 QObject 并将其导出到 QML,在 QML 中创建项目并将其添加到 map :
#include <QGuiApplication>
#include <QQuickView>
#include <QGeoCoordinate>
#include <QQmlContext>
class Helper: public QObject{
Q_OBJECT
public:
void addCircle(const QGeoCoordinate & coordinate){
Q_EMIT circleSignal(coordinate);
}
Q_SIGNALS:
void circleSignal(const QGeoCoordinate & coordinate);
};
int main(int argc, char *argv[]){
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView view;
Helper helper;
view.rootContext()->setContextProperty("helper", &helper);
view.setSource(QUrl("qrc:/map.qml"));
view.resize(1000, 650);
view.setTitle("GUI");
view.show();
helper.addCircle(QGeoCoordinate(47.6, 9.5));
return app.exec();
}
#include "main.moc"
import QtQuick 2.14
import QtPositioning 5.14
import QtLocation 5.14
Map{
id: map_map
anchors.centerIn: parent;
anchors.fill: parent
plugin: Plugin {
name: "mapboxgl" // "osm", "esri", ...
}
center: QtPositioning.coordinate(47.6,9.5)
zoomLevel: 8
Connections{
target: helper
onCircleSignal: {
var component = Qt.createComponent("rect.qml");
if (component.status === Component.Ready){
var o = component.createObject(map_map);
o.center = coordinate
map_map.addMapItem(o)
}
}
}
}
或
import QtQuick 2.14
import QtPositioning 5.14
import QtLocation 5.14
Map{
id: map_map
anchors.centerIn: parent;
anchors.fill: parent
plugin: Plugin {
name: "mapboxgl" // "osm", "esri", ...
}
center: QtPositioning.coordinate(47.6,9.5)
zoomLevel: 8
Component{
id: provider
MapCircle{
center{
latitude: 47.6
longitude: 9.5
}
radius: 5000.0
color: 'green'
border.width: 3
}
}
function createCirle(map, position){
var o = provider.createObject(map)
o.center = position
map.addMapItem(o)
return o
}
Connections{
target: helper
onCircleSignal: {
var o = createCirle(map_map, coordinate)
}
}
}
将模型与 MapItemView 结合使用:
#include <QGuiApplication>
#include <QQuickView>
#include <QGeoCoordinate>
#include <QQmlContext>
#include <QStandardItemModel>
int CoordinateRole = Qt::UserRole + 1000;
int main(int argc, char *argv[]){
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQuickView view;
QStandardItemModel model;
QHash<int, QByteArray> roles;
roles[CoordinateRole] = QByteArray("coordinate");
model.setItemRoleNames(roles);
view.rootContext()->setContextProperty("circle_model", &model);
view.setSource(QUrl("qrc:/map.qml"));
view.resize(1000, 650);
view.setTitle("GUI");
view.show();
QStandardItem *item = new QStandardItem;
item->setData(QVariant::fromValue(QGeoCoordinate(47.6, 9.5)), CoordinateRole);
model.appendRow(item);
return app.exec();
}
import QtQuick 2.14
import QtPositioning 5.14
import QtLocation 5.14
Map{
id: map_map
anchors.centerIn: parent;
anchors.fill: parent
plugin: Plugin {
name: "mapboxgl" // "osm", "esri", ...
}
center: QtPositioning.coordinate(47.6,9.5)
zoomLevel: 8
MapItemView{
model: circle_model
delegate: MapCircle{
center: model.coordinate
radius: 5000.0
color: 'green'
border.width: 3
}
}
}
关于c++ - QT通过C++添加Map QML Items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377720/
如果下一个元素的宽度超过指定布局的宽度,是否有 QML 布局或某些配置会自动将 QML 项目包装到下一行? 当我使用 QML GridLayout ,项目刚好离开窗口的边缘并被剪裁: GridLayo
如果下一个元素的宽度超过指定布局的宽度,是否有 QML 布局或某些配置会自动将 QML 项目包装到下一行? 当我使用 QML GridLayout ,项目刚好离开窗口的边缘并被剪裁: GridLayo
如何在 qml 文件之间发送变量或信号? http://i.stack.imgur.com/MChCG.png Mainwindow -> 创建组件Item2.qml MainWindow -> 创建
我正在做一些事情,我有一个名为“FloatingMenu”的类(它应该在 C++ 中管理菜单)及其在文件 FloatingMenu.qml 中用于 GUI 的 QML alter-ego。我有一个文件
我正在尝试做一些看似简单的事情,但失败了:定义一个简单的内联文本格式组件,然后用不同的文本多次实例化它。这是代码 Item { . . . Component { id: favButtonL
我可以在页面中使用 InvokeActionItem 轻松共享项目,但我需要能够在 ListView 项目中调用它。我设法触发了一个调用,但我不知道如何在触发它时添加数据。我不断收到错误消息 Invo
我如何在 QML 中检测 Window {} 之外的点击? Rectangle { id: topLevel height: 400; width: 400 Window {
我试过 : var child = grid.children[slot1]; grid.children[slot1] = grid.children[slot2]; grid.children[s
例如,我希望创建一个包含 100 个文本编辑器的 qml 窗口,如何在循环中创建它?那可能吗? 最佳答案 循环是命令式代码,所以它不是 QML,而是 Javascript 或 C++。所以当然,你可以
这是我的 QML 文件,其中包含一个文本组件: import QtQuick 2.0 Item { id: idItmWrapText Text { id: idTxt
我正在寻找一种方法来显示一个文本提示,说明预期的输入作为对用户的建议。以谷歌搜索栏为例: 是否有我缺少的属性,或者这是必须通过脚本来实现的? 最佳答案 Qt Quick 输入项上不存在该属性。您可以为
为 qml 项设置背景的最简单方法是让子矩形的 anchor 完全填满父项: Item { width: 320 height: 240 Rectangle {
我想将属性动态添加到 QML 元素: Item { id: dynamicProperty; property int first; Component.onCompleted: { /*
我用 PySide 和 QML 编写了某种安装程序。按照设计,它必须是多页的。而且我想将要从 QML 表单调用的插槽划分为不同的对象(理想情况下,划分为模块,但据我了解,带有插槽的对象必须继承 QOb
QML 中有没有办法用 opacity: 0 创建一个矩形?仍然有可见的边界?如果没有,有关如何解决它的任何建议? 谢谢 最佳答案 不,不透明度适用于项目的完整视觉方面(并且不透明度:0 使项目完全不
属性变体 a:{}似乎不起作用。 a 最终未定义,而不是一个空字典。 我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么? 以下 qml 在控制台上打印“qrc:/m
我在 SO 上查看了大量关于 QML 内容边距的问题,但所有答案都指向缺少 spacing: 0 属性。我已经完成了所有这些,但仍然有一些我无法消除的奇怪空间。任何人都可以解释为什么这个 QML 代码
我有一个用于样式定义的 QML 单例,定义如下: pragma Singleton import QtQuick 2.2 import QtQuick.Controls 1.1 import QtQu
这是以下代码的结果: 主.qml import QtQuick 2.8 Item { Reusable { index: 1234 // reusable with
属性变体 a:{}似乎不起作用。 a 最终未定义,而不是一个空字典。 我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么? 以下 qml 在控制台上打印“qrc:/m
我是一名优秀的程序员,十分优秀!