2011-07-06 3 views
0

두 클래스가 있습니다. 먼저 위치 데이터 (위도와 경도)를 생성하고, 두 번째 클래스에서이 데이터 (변수 latitude 및 longitute)에 액세스 할 수 있습니까? 내가 할 두 번째 클래스에 미친 수를 렸기 때문에 (여기QT 이동성에서 다른 클래스의 변수를 액세스하는 방법은 무엇입니까?

는 헤더와 클래스는 다음과 같습니다 첫 번째 헤더 :

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

두 번째 헤더

,210

두 번째 클래스 : 예를 들어

#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을 사용하여 매번 레이블 텍스트를 생성하고 위치가 업데이트됩니다. 하지만 지금은 미친 숫자가 나옵니다.

답변

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

당신은 MainWindow의 새로운 인스턴스를 생성하고 직접 latitude 멤버에 액세스 제발 도와주세요. 그러나 그것은 초기화 되었습니까? 나는이 회원을 쓰는 유일한 곳이 MainWindow 인 것을보고 positionUpdated입니다. mw->latitude에 액세스하기 전에이 메소드가 호출되는지 확인 하시겠습니까? 다른 qDebug 인쇄물을 사용하여 positionUpdated에서 쉽게 확인하거나 디버거를 사용하여 쉽게 확인할 수 있습니다.


일반적으로 코드 스타일에 대해 의견을 말하기 - 다른 클래스의 멤버에게 직접 액세스하는 것은 좋지 않습니다. 이 접근 방식으로 인해 발생한 문제 중 하나는 클래스의 유효성을 실제로 제어 할 수있는 방법이 없다는 것입니다. 액세서 메소드는 값이 계산되었는지 (또는 지연 계산의 대상이 될 수도 있기 때문에) 원시 멤버 액세스 대신 액세서 메소드를 사용하는 가장 기본적인 솔루션조차도 여기에서 이상한 일을 할 수 있습니다.

+0

내가 positionupdated에 qdebug를 만들면 정상적인 위치를 볼 수 있습니다. 당신이 더 자세히 말하면서 저를 설명해 주시겠습니까? 아니면 내 예제에 ??? – user707895

관련 문제