开发者

how to access variable from one class in another in QT mobility?

I have two classes: first generate position data (latitude and longitude), how I can access this data (variables latitude and longitute) in second class? becouse in second class I get crazy number(

Here are headers and classes: first header:

#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

first class

#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)
    {
      开发者_Python百科  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);
}

second header

#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

second class:

#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);
}

For example I want to see latitude in second class, by pressing button. In future I'll do this by Signal/slot to generate label text every time, the position is updated. But now I'll get crazy number. Help me please


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

You create a new instance of MainWindow and directly access its latitude member. But was it initialized? I see the only place writing into this member in MainWindow is positionUpdated. Are you sure this method gets invoked before you access mw->latitude? You could easily verify that with another qDebug printout from positionUpdated, or by using the debugger.


To comment on the code style in general - it's not good practice to directly access members of other classes like this. One of the problems (as you've just encountered!) with this approach is that the class has no way to actually control the validity of its member. Even the most basic solution - having an accessor method instead of raw member access could do wonders for you here, because the accessor method could potentially check if the value has been computed (or even compute it lazily).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜