PDA

View Full Version : Getting a function to call another function



bmn
9th December 2010, 12:41
Hello everyone
I'm trying to get a function to call another function from the same class
this is the function i want called:



void MainWindow::locationUpdated(QGeoPositionInfo currentPosition) {
//Defines a geographical position on the surface of the Earth
QGeoCoordinate geoCoordinate = currentPosition.coordinate();

//Declares latitude and longitude
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();

//Replaces the textlabels with longitude and latitude values
ui->latitudeOutputLabel->setText(QString::number(latitude));
ui->longitudeOutputLabel->setText(QString::number(longitude));
ui->areaLabel->setText(currentPosition.coordinate().toString(QGeo Coordinate::DegreesMinutesWithHemisphere));
};


and i want it called from, so that i dont have to find the coordinates again in this function:

void MainWindow::calcMax() {

//gives maximum values of latitude and longitude
// geoCoordinate.setLatitude(geoCoordinate.latitude() +2);
// geoCoordinate.setLongitude(geoCoordinate.longitude ()+2);
// ui->maxLabel->setText(geoCoordinate.toString(QGeoCoordinate::Deg reesMinutesWithHemisphere));

};

Header:

class MainWindow : public QMainWindow
{
Q_OBJECT

public slots:
void locationUpdated(QGeoPositionInfo);
void calcMax();
void calcMin();


public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
QGeoPositionInfoSource* m_source;

Im pretty clueless at this point any help is welcome :)

franz
9th December 2010, 13:01
...

What exactly is the problem?


void MainWindow::calcMax()
{
locationUpdated(someLocation);
...
}


One remark: locationUpdated() is a bad choice for a function name. It is very much suitable for signals, but slots (basically normal functions) should get names like setLocation(). You can see in the above example how silly it looks in normal code...

bmn
9th December 2010, 13:06
thanks for your response
What i want is for the calcMax function to setLatitude and setLongitude to +2 of my current position.
To do so i need my current position, which i found in the function locationUpdated
if i follow your suggestion it needs a new declaration, which is not what i am after?

wysota
9th December 2010, 13:36
From what I understand you locationUpdated() is a slot called from a signal from the position tracking source and you wish to be able to do some calculations based on the received position but not immediately after you get it but instead at some later point in time, correct?

If so, declare a variable in your class and in locationUpdated() (change the name of the slot, it's really weird) assign the position object you receive to the newly declared variable. Then in other methods you will be able to fetch the position value from the variable.

bmn
9th December 2010, 14:49
sorry for the inconvenience
what i really want is to call the variables
qreal longitude
qreal latitude

in my other function calcMax();

what i did was add the variables to my head and write this

void MainWindow::calcMax() {


ui->maxLabel->setText(QString::number(latitude));




it cant make the connection to my previous function tho ::(

ChrisW67
9th December 2010, 21:06
If you declare latitude and longitude in the scope of locationUpdated() then that is the only place you can access them directly from. You need to find a good C++ reference and read about the C++ scope rules.

Make latitude and longitude private member variables of the class. It is also good practice to make sure that the constructor initialises them.


class MainWindow: ...
{
public:
...
private:
qreal latitude;
qreal longitude;
}

SixDegrees
9th December 2010, 21:46
If you declare latitude and longitude in the scope of locationUpdated() then that is the only place you can access them directly from. You need to find a good C++ reference and read about the C++ scope rules.

Make latitude and longitude private member variables of the class. It is also good practice to make sure that the constructor initialises them.


class MainWindow: ...
{
public:
...
private:
qreal latitude;
qreal longitude;
}


Minor nitpick: make them protected members of the class, rather than private members. This will make it easier on anyone extending the class in the future.

wysota
9th December 2010, 22:26
Another tip: use QGeoPositionInfo or QGeoCoordinate instead of latitude and longitude.