Results 1 to 4 of 4

Thread: Calling a method from a different .cpp file

  1. #1
    Join Date
    Jan 2017
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Calling a method from a different .cpp file

    I want to save some data as a text file, the first txt file will contain header information, the other text file will save data streamed from sensors, so with the help from the internet I created the following "datalogger.cpp" file

    Qt Code:
    1. #include "datalogger.h"
    2. #include <QDebug>
    3. #include <iostream>
    4. #include <QFile>
    5.  
    6. DataLogger::DataLogger(QObject *parent) : QObject(parent)
    7. {
    8.  
    9. }
    10.  
    11. DataLogger::~DataLogger(){
    12.  
    13. }
    14. void DataLogger::save(DataStream &input){
    15. saveAsText(input);
    16.  
    17.  
    18. }
    19. void DataLogger::saveAsText(DataStream &input){
    20.  
    21. QTextStream outHeader(&outFileHeader);
    22.  
    23. outHeader << "[CAPTURE SETTINGS]\n"
    24. << "Filename: " << SettingsSingleton::instance().getFileName() << ".txt \n"
    25. << "Samples: " << QString::number(input.size()) << "\n"
    26. << "Duration: " << QString::number(input.back().time) << "ms \n"
    27. << "Sample rate: " << QString::number(SettingsSingleton::instance().getSampleRate()) << " Hz\n"
    28. << "Source: " << SettingsSingleton::instance().getSource() << "\n"
    29.  
    30. outFileHeader.close();
    31.  
    32. }
    33.  
    34. QFile outFile(SettingsSingleton::instance().getFileName() + ".txt");
    35.  
    36. QTextStream out(&outFile);
    37.  
    38. for (int i ; i<input.size();i++){
    39. const EcgStreamObject tmp=input.at(i);
    40. out << tmp.toText() << endl; //"\n";
    41.  
    42. }
    43.  
    44. outFile.close();
    45. }
    46.  
    47. }
    To copy to clipboard, switch view to plain text mode 

    I have my "DataStream" input variable that I want to pass to the method and save as a ".txt" file, however I do no know how to call the method "void DataLogger::save(DataStream &input)" from a different ".cpp" file where the DataStream variable is located.
    I am extremely new to c++ please it as simple as possible please.
    Thank you in advance

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,688
    Thanks
    13
    Thanked 1,598 Times in 1,526 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Calling a method from a different .cpp file

    Qt Code:
    1. #include "datalogger.h"
    2. ...
    3. DataStream myDataStream;
    4. ...
    5. DataLogger logger;
    6. logger.save(myDataStream);
    To copy to clipboard, switch view to plain text mode 
    This assumes that DataLogger::save() is declared public.

  3. The following user says thank you to ChrisW67 for this useful post:

    VitaminG (20th January 2017)

  4. #3
    Join Date
    Jan 2017
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Calling a method from a different .cpp file

    How would I know if it is declared as public?
    Orhow would I declare it as public?
    Thanks for your reply

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,776
    Thanks
    262
    Thanked 773 Times in 763 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Calling a method from a different .cpp file

    I think that instead of asking a lot of questions here about basic C++ things that you should already know -before- you start trying to use Qt, you should work your way through a C++ book or some of the many online C++ tutorials. Here is a good one, on a site I use all the time for C++ information.

    This really is a forum for answering questions about Qt, not for teaching basic C++ concepts.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 4
    Last Post: 30th August 2015, 14:07
  2. Calling a method from a non-member method
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 19th March 2011, 11:38
  3. Calling a method to a parent window/widget
    By johnnyturbo3 in forum Newbie
    Replies: 4
    Last Post: 6th November 2010, 16:52
  4. error calling method using connect
    By jeffmetal in forum Newbie
    Replies: 4
    Last Post: 22nd April 2010, 19:09
  5. Calling same method from separate threads
    By steg90 in forum Qt Programming
    Replies: 2
    Last Post: 19th July 2007, 09:55

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.