首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Qt :无法从外部QQuickWidget更新MainWindow

Qt :无法从外部QQuickWidget更新MainWindow
EN

Stack Overflow用户
提问于 2021-01-28 08:29:24
回答 1查看 159关注 0票数 0

我已经尝试解决这个问题2天了,我有一个gmaps项目,我使用以下代码将其集成到Qt项目中:

代码语言:javascript
复制
gmap locator;
ui->quickWidget->setSource(QUrl::fromLocalFile("/maps/main.qml"));

ui->quickWidget->rootContext()->setContextProperty("gmap",&locator);
ui->quickWidget->show();

当我从MainWindow设置地图上的位置时,使用

代码语言:javascript
复制
    locator.setData( "28.6082819", "77.0350079");

但是,当我添加一个工具按钮并从它的插槽中执行相同的操作时,它就不起作用了,我已经将它连接到我的MainWindow中的一个自定义插槽,如下所示:

代码语言:javascript
复制
   connect(ui->toolButton_5, SIGNAL(clicked()), this, SLOT(plotmap(QString(ui->lineEdit_3->text()),QString(ui->lineEdit_4->text()),&locator,&ui->quickWidget)));

这是定制的插槽:

代码语言:javascript
复制
    void MainWindow::plotmap(QString lat , QString lon, gmap *loc,QQuickWidget *view)
{
  
       loc->setData("02.60","77.04");
       view->show();
       view->update();


}

这是我的qml文件

gmap.cpp

代码语言:javascript
复制
    #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

代码语言:javascript
复制
    #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

代码语言:javascript
复制
    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
        }


    }

}

我的QQuickWidget中的地图不是update.What,我可能做错了,请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-28 09:16:26

您的连接语法是错误的。

locator作为成员存储在某个地方,例如在MainWindow中:

代码语言:javascript
复制
public:
    void setLocator(gmap* loc) { m_locator = loc; }

private:
    gmap* m_locator;

向MainWindow添加一个插槽:

代码语言:javascript
复制
private slot:
    plotmapFromLineEdits() { plotmap(ui->lineEdit_3->text(), ui->lineEdit_4->text(), m_locator, ui->quickWidget); }

然后像这样把它连接起来:

代码语言:javascript
复制
connect(ui->toolButton_5, SIGNAL(clicked()), this, SLOT(plotmapFromLineEdits()));

或者在C++11或更高版本中,您可以通过使用lambda来连接而无需定义插槽:

代码语言:javascript
复制
connect(ui->toolButton_5, SIGNAL(clicked()), [&](){plotmap(ui->lineEdit_3->text(), ui->lineEdit_4->text(), m_locator, ui->quickWidget);}));

我不认为你每次都要showupdate QQuickWidget。只需删除quickwidget参数即可。

最后,请确保类名以uppercase字母开头: gmap => GMap。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65933498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档