Results 1 to 8 of 8

Thread: Getting a function to call another function

  1. #1
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Getting a function to call another function

    Hello everyone
    I'm trying to get a function to call another function from the same class
    this is the function i want called:
    Qt Code:
    1. void MainWindow::locationUpdated(QGeoPositionInfo currentPosition) {
    2. //Defines a geographical position on the surface of the Earth
    3. QGeoCoordinate geoCoordinate = currentPosition.coordinate();
    4.  
    5. //Declares latitude and longitude
    6. qreal latitude = geoCoordinate.latitude();
    7. qreal longitude = geoCoordinate.longitude();
    8.  
    9. //Replaces the textlabels with longitude and latitude values
    10. ui->latitudeOutputLabel->setText(QString::number(latitude));
    11. ui->longitudeOutputLabel->setText(QString::number(longitude));
    12. ui->areaLabel->setText(currentPosition.coordinate().toString(QGeoCoordinate::DegreesMinutesWithHemisphere));
    13. };
    To copy to clipboard, switch view to plain text mode 


    and i want it called from, so that i dont have to find the coordinates again in this function:
    Qt Code:
    1. void MainWindow::calcMax() {
    2.  
    3. //gives maximum values of latitude and longitude
    4. // geoCoordinate.setLatitude(geoCoordinate.latitude()+2);
    5. // geoCoordinate.setLongitude(geoCoordinate.longitude()+2);
    6. // ui->maxLabel->setText(geoCoordinate.toString(QGeoCoordinate::DegreesMinutesWithHemisphere));
    7.  
    8. };
    To copy to clipboard, switch view to plain text mode 

    Header:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public slots:
    6. void locationUpdated(QGeoPositionInfo);
    7. void calcMax();
    8. void calcMin();
    9.  
    10.  
    11. public:
    12. explicit MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. private:
    16. Ui::MainWindow *ui;
    17. QGeoPositionInfoSource* m_source;
    To copy to clipboard, switch view to plain text mode 

    Im pretty clueless at this point any help is welcome
    Last edited by bmn; 9th December 2010 at 12:45.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Getting a function to call another function

    ...

    What exactly is the problem?
    Qt Code:
    1. void MainWindow::calcMax()
    2. {
    3. locationUpdated(someLocation);
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    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...
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: Getting a function to call another function

    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?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting a function to call another function

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    bmn (9th December 2010)

  6. #5
    Join Date
    May 2007
    Posts
    131
    Thanks
    17
    Thanked 4 Times in 2 Posts

    Default Re: Getting a function to call another function

    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
    Qt Code:
    1. void MainWindow::calcMax() {
    2.  
    3.  
    4. ui->maxLabel->setText(QString::number(latitude));
    To copy to clipboard, switch view to plain text mode 

    it cant make the connection to my previous function tho :

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting a function to call another function

    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.
    Qt Code:
    1. class MainWindow: ...
    2. {
    3. public:
    4. ...
    5. private:
    6. qreal latitude;
    7. qreal longitude;
    8. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Getting a function to call another function

    Quote Originally Posted by ChrisW67 View Post
    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.
    Qt Code:
    1. class MainWindow: ...
    2. {
    3. public:
    4. ...
    5. private:
    6. qreal latitude;
    7. qreal longitude;
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Getting a function to call another function

    Another tip: use QGeoPositionInfo or QGeoCoordinate instead of latitude and longitude.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt function call in vb.net
    By abghosh in forum Qt Programming
    Replies: 7
    Last Post: 6th March 2010, 17:00
  2. Call to database function.
    By retto in forum Qt Programming
    Replies: 1
    Last Post: 14th September 2009, 12:29
  3. call function as Qstring
    By jcr in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 01:35
  4. call variable from other function
    By walito in forum Newbie
    Replies: 7
    Last Post: 31st August 2007, 18:36
  5. function call
    By Walsi in forum Qt Programming
    Replies: 3
    Last Post: 12th June 2007, 09:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.