Hi,
for my little QT-project i want to write a function, which detect the current rate of stock.
Something like "double FinanceAPI::GetStockRate(QString stock_symbol)". The committed stock symbol represents the stock.

For these job i want to use the yahoo-finance-api. With a HTTP-request like...

http://finance.yahoo.com/d/quotes.cs...sn&ignore=.cvs

...in my webbrowser, i get a right answer. It is a CSV-file with the rate of stock "847402". My problem is how get these information with a qt-function.

This is what i try:

Qt Code:
  1. #include "financeapi.h"
  2.  
  3. FinanceAPI::FinanceAPI(QObject *parent) :
  4. QObject(parent)
  5. {
  6.  
  7. manager = new QNetworkAccessManager(this);
  8. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
  9.  
  10. }
  11.  
  12.  
  13. FinanceAPI::~FinanceAPI(){
  14.  
  15. delete manager;
  16. }
  17.  
  18.  
  19.  
  20.  
  21. void FinanceAPI::replyFinished(QNetworkReply *reply)
  22. {
  23. if(reply->error()!=QNetworkReply::NoError){
  24. qDebug()<<"Fehlerhafte Anfrage\n";
  25. }
  26. else{
  27.  
  28.  
  29. QByteArray bytes = reply->readAll();
  30. QString answer = QString::fromUtf8(bytes);
  31. QFile file("test.csv");
  32. if(!file.open(QFile::WriteOnly))
  33. {
  34. qDebug()<<"test";
  35. }
  36.  
  37. //file.write(*answer);
  38. QTextStream out(&file);
  39. out<<answer;
  40. file.flush();
  41. file.close();
  42.  
  43. }
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. double FinanceAPI::GetStockRate(QString stock_symbol){
  51.  
  52. QString tmpQuery="http://finance.yahoo.com/d/quotes.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//"http://ichart.finance.yahoo.com/table.csv?s="+stock_symbol+"&f=l1sn&ignore=.cvs";//
  53. manager->get(QNetworkRequest(QUrl(tmpQuery)));
  54.  
  55. QNetworkRequest request;
  56. request.setUrl(QUrl(tmpQuery));
  57. manager->get(request);
  58.  
  59. return 0.0;
  60. }
To copy to clipboard, switch view to plain text mode 


My call of the function looks like:

Qt Code:
  1. FinanceAPI *tmpFinance = new FinanceAPI();
  2. double rate =tmpFinance->GetStockRate("847402.DE"));
To copy to clipboard, switch view to plain text mode 


Somethings goes wrong respectively i don't understand the working of the...

Qt Code:
  1. connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
To copy to clipboard, switch view to plain text mode 

Because I think the answer from the request (-> the rate) come not in the "GetStockRate()", but rather "replyFinished()". But I needed at the end of the "GetStockRate()"-function to use it as return value.

Thanks for helping