0

我有两个类:首先生成位置数据(纬度和经度),如何在第二类中访问这些数据(变量纬度和经度)?因为在二等舱我得到了疯狂的数字(

这是标题和类: 第一个标题:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGeoPositionInfoSource>
#include <QGeoPositionInfo>
#include <QtCore/QPointer>
#include <QGeoSatelliteInfo>
#include <QGeoSatelliteInfoSource>
#include "gpsform.h"
QTM_USE_NAMESPACE

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    double latitude;
    double longitude;
    double altitude;
    double speed;

public slots:
    void positionUpdated(QGeoPositionInfo geoPositionInfo);

private:
    Ui::MainWindow *ui;    
    QPointer<QGeoPositionInfoSource> locationDataSource;

private slots:
    void on_pushButton_2_clicked();
    void on_pushButton_4_clicked();
    void startGPS();
    void on_pushButton_clicked();

signals:
    void updated();
};

#endif // MAINWINDOW_H

头等舱

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "quitdiallog.h"
#include <QGeoCoordinate>
#include <QDebug>
#include <QtGui/QMessageBox>
#include <QList>
#include "gpsform.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    setWindowTitle("Мой кОмпаС");
    ui->setupUi(this);
    startGPS();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::startGPS()
{
    // Obtain the location data source if it is not obtained already
    if (!locationDataSource)
    {
        locationDataSource =
                QGeoPositionInfoSource::createDefaultSource(this);
        if (locationDataSource)
        {
            // Whenever the location data source signals that the current
            // position is updated, the positionUpdated function is called.
            QObject::connect(locationDataSource,
                             SIGNAL(positionUpdated(QGeoPositionInfo)),
                             this,
                             SLOT(positionUpdated(QGeoPositionInfo)));
            // Start listening for position updates
                    locationDataSource->setUpdateInterval(100);
                    locationDataSource->setPreferredPositioningMethods(QGeoPositionInfoSource::SatellitePositioningMethods);
            locationDataSource->startUpdates();
        } else {
            // Not able to obtain the location data source
            // TODO: Error handling
        }
    } else {
        // Start listening for position updates
        locationDataSource->setUpdateInterval(5000);
        locationDataSource->startUpdates();
    }
}

void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
    //gpsform *gpf=new gpsform;
    if (geoPositionInfo.isValid())
    {
        //locationDataSource->stopUpdates();
        QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
        latitude = geoCoordinate.latitude();
        longitude = geoCoordinate.longitude();
        altitude=geoCoordinate.altitude();
    ui->label->setNum(latitude);
    ui->label_2->setNum(longitude);
    /*if(QGeoPositionInfo::GroundSpeed)
    {
        speed=QGeoPositionInfo::GroundSpeed;
    ui->label_4->setNum(speed);
    }*/
    emit updated();
    //gpf->latitude=this->latitude;
    //gpsform *gpf=new gpsform;
    //gpf->show();
    //gpf->latitude=latitude;
    }
}



void MainWindow::on_pushButton_clicked()
{    
    /*ui->label_3->setNum(latitude);
    qDebug()<<latitude<<"    "<<longitude<<"   "<<altitude;*/
    gpsform *gps=new gpsform;
    this->hide();
    gps->show();

}

void MainWindow::on_pushButton_4_clicked()
{
    QuitDiallog *qi=new QuitDiallog;
    this->hide();
    qi->show();
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->label_3->setNum(latitude);
}

第二个标题

#ifndef GPSFORM_H
#define GPSFORM_H

#include <QWidget>
#include "mainwindow.h"
QTM_USE_NAMESPACE
namespace Ui {
    class gpsform;
}

class gpsform : public QWidget
{
    Q_OBJECT

public:
    explicit gpsform(QWidget *parent = 0);
    ~gpsform();
    double latitude;

private:
    Ui::gpsform *ui;    

private slots:    
    void on_pushButton_clicked();
    void updatedata();
};

#endif // GPSFORM_H

第二类:

#include "gpsform.h"
#include "ui_gpsform.h"
#include "mainwindow.h"
#include <QTimer>
#include <QDebug>

gpsform::gpsform(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::gpsform)
{
    ui->setupUi(this);
    /*ui->label->setNum(mw->latitude);*/
   /* QTimer * timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    timer->start(50);*/
   /* MainWindow *mw = new MainWindow;
    QObject::connect(mw,SIGNAL(updated()),this,SLOT(updatedata()));*/
}

gpsform::~gpsform()
{
    delete ui;
}

void gpsform::updatedata()
{
   /* MainWindow *mw = new MainWindow;
    this->latitude=mw->latitude;
    ui->label->setNum(mw->latitude);*/
}

void gpsform::on_pushButton_clicked()
{
        MainWindow *mw = new MainWindow;
        //latitude=mw->latitude;
        qDebug()<<mw->latitude;
        ui->label->setNum(latitude);
}

例如,我想通过按下按钮查看二等舱的纬度。将来我将通过 Signal/slot 执行此操作,以在每次更新位置时生成标签文本。但现在我会得到疯狂的数字。请帮帮我

4

1 回答 1

0
    MainWindow *mw = new MainWindow;
    //latitude=mw->latitude;
    qDebug()<<mw->latitude;

您创建一个新实例MainWindow并直接访问其latitude成员。但它被初始化了吗?我看到写入该成员的唯一地方MainWindowpositionUpdated. 您确定此方法在您访问之前被调用mw->latitude吗?qDebug您可以使用来自 的另一个打印输出positionUpdated或使用调试器轻松验证这一点。


总体上评论代码风格 - 像这样直接访问其他类的成员不是一个好习惯。这种方法的一个问题(正如您刚刚遇到的那样!)是该类无法实际控制其成员的有效性。即使是最基本的解决方案 - 使用访问器方法而不是原始成员访问也可以在这里为您创造奇迹,因为访问器方法可能会检查该值是否已被计算(甚至懒惰地计算它)。

于 2011-07-07T03:40:47.497 回答