Results 1 to 20 of 31

Thread: Transfer of a QByteArray to main.cpp

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Transfer of a QByteArray to main.cpp

    Hallo,
    with the attached program I can get a csv file from Yahoo Finance and store it in a QByteArray. But till now I didn`t succeed in transmitting the QByteArray or a QVector from filedownloader.cpp to main.cpp. I have already in vain tried many proceedings. Has anybody an idea how to overcome these difficulties?
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Transfer of a QByteArray to main.cpp

    You are calling your data transfer function transmit() right after creating the downloader object.
    There has been no time to actually get the data yet, the QNetworkAccessManager get() request is processed once the application has started event processing, i.e. when app.exec() is being called.

    You already have a signal that is emitted when you've received the data.
    Connect to that.

    Cheers,
    _

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

    dolin93 (21st January 2015)

  4. #3
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Transfer of a QByteArray to main.cpp

    Thanks for Your help!
    After insertion of
    connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
    SLOT(transmit(QList<QByteArray>)));
    I got the massage
    Object::connect: No such slot FileDownloader::transmit(QList<QByteArray>)
    and unfortunately there was no improvement.

    A happy new year!

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Transfer of a QByteArray to main.cpp

    Why on earth did you even try that?
    What could that possibly improve?

    Just because some random code compiles doesn't mean it will do anything useful and even less often mean it will do what you need.

    Take a step back and think about what you actually need.
    And then come back and ask for that.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    dolin93 (21st January 2015)

  7. #5
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Transfer of a QByteArray to main.cpp

    Thanks for Your help!
    With PHP it is not difficult to get csv files from Yahoo Finance, use them for simple calculations and display the results in tables.
    But with Qt/C++ I`d be more flexible and could even graphically display results and charts. Therefore I tried to get the csv files also with Qt/C++ and transfer the data to main.cpp or another source file where I could use them.

    Cheers

  8. #6
    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: Transfer of a QByteArray to main.cpp

    I don't think you understood what anda_skoa means. Please explain what you though this:

    Qt Code:
    1. connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
    2. SLOT(transmit(QList<QByteArray>)));
    To copy to clipboard, switch view to plain text mode 
    would do and how you think it would solve your problem. Then take a piece of paper, a pencil and write down a step-by-step algorithm that achieves what you want. Then we will help you "translate" it to C++. Remember you are dealing with an asynchronous system.
    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.


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

    dolin93 (21st January 2015)

  10. #7
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Transfer of a QByteArray to main.cpp

    Thanks for Your answer!
    Only after failure with
    connect(SIGNAL(finished(QNetworkReply*)),
    SLOT(transmit(QList<QByteArray>)));
    I tried the above mentioned Qt Code
    connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
    SLOT(transmit(QList<QByteArray>)));
    .
    Now I use a class PrCalClass for calculations and call this class in filedownloader.cpp through
    PrCalClass demo(m_DownloadedData);
    demo.PrintM();
    . For the drawing of charts and results I`ll use another class.
    But nevertheless I`d be very pleased if You showed me how one can bring the QByteArray m_DownloadedData into main.cpp.

  11. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Transfer of a QByteArray to main.cpp

    Quote Originally Posted by dolin93 View Post
    But nevertheless I`d be very pleased if You showed me how one can bring the QByteArray m_DownloadedData into main.cpp.
    You have to understand that, for the rest of us, this goal does not make any sense.

    Once the linker has linked all object code into the executable, there is no distinction anymore which source file the code came from.

    You could just put the code of your file downloader class into main.cpp, remove the now unnecessary header and source file and still have the same program.
    That would not change anything your program is doing, it would just increase the length of the now only cpp file.

    Cheers,
    _

  12. The following user says thank you to anda_skoa for this useful post:

    dolin93 (21st January 2015)

  13. #9
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Transfer of a QByteArray to main.cpp

    Thank You for Your answer!
    Unfortunately I didn`t succeed in bringing downloaded data to the class mainWidget for drawing.

  14. #10
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Transfer of a QByteArray to main.cpp

    I downloaded your zip file that contains your project. You are trying to fit a square peg into a round hole... You are dead set on getting a QByteArray back into your main routine, but here is the problem with your design.

    You instantiate FileDownloader in your main() routine, which creates your QNetworkAccessManager, does the get request for your URL, etc. The signals for the get request won't actually be executed until your main routine runs the following code:
    Qt Code:
    1. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    This is because there is no message loop running yet, so no signals will be dispatched. What you should be doing, is doing all of your processing in your FileDownloader class and forget about trying to get the result into your main() routine.

    Once your download completes in FileDownloader::fileDownloaded, what do you want to do next? Write new methods for FileDownloader to process the results of the download or use signals/slots to process the result, for example:
    Qt Code:
    1. void FileDownloader::fileDownloaded(QNetworkReply* reply)
    2. {
    3. m_DownloadedData = reply->readAll();
    4. // do all of your processing of the result here or add a new method, emit a signal, or call another method in FileDownloader, etc.
    5. return;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Hope that helps.

  15. The following user says thank you to jefftee for this useful post:

    dolin93 (21st January 2015)

  16. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Transfer of a QByteArray to main.cpp

    Quote Originally Posted by dolin93 View Post
    Unfortunately I didn`t succeed in bringing downloaded data to the class mainWidget for drawing.
    You can emit the data as part of a signal and connect it to a matching slot in this "mainWidget" class.

    Cheers,
    _

  17. The following user says thank you to anda_skoa for this useful post:

    dolin93 (21st January 2015)

  18. #12
    Join Date
    Jan 2015
    Posts
    12
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Transfer of a QByteArray to main.cpp

    Thanks for Your answers!
    In my program there are now the header files (abbreviated)
    Qt Code:
    1. class FileDownloader : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. FileDownloader(const QUrl &KUrl);
    6. virtual ~FileDownloader();
    7. QByteArray downloadedData() const;
    8.  
    9. signals:
    10. void downloaded();
    11. void DataA(const QByteArray&);
    12.  
    13. public slots:
    14. void fileDownloaded(QNetworkReply* pReply);
    15. private:
    16. QUrl kUrl;
    17. QNetworkAccessManager m_WebCtrl;
    18. QByteArray m_DownloadedData;
    19. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. class mainWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. mainWidget();
    6. public slots:
    7. void Receiver(const QByteArray &QBA);
    8. protected:
    9. void paintEvent(QPaintEvent *event);
    10. private:
    11. };
    To copy to clipboard, switch view to plain text mode 
    and the source files
    Qt Code:
    1. FileDownloader::FileDownloader( const QUrl &KUrl) : kUrl(KUrl)
    2. {
    3. connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
    4. SLOT(fileDownloaded(QNetworkReply*)));
    5. QNetworkRequest request(kUrl);
    6. m_WebCtrl.get(request);
    7. }
    8.  
    9. FileDownloader::~FileDownloader()
    10. {
    11. }
    12. void FileDownloader::fileDownloaded(QNetworkReply* pReply)
    13. {
    14. m_DownloadedData = pReply->readAll();
    15. pReply->deleteLater();
    16. emit downloaded();
    17. emit DataA(m_DownloadedData);
    18. mainWidget *mW=new mainWidget();
    19. connect(SIGNAL(DataA(const QByteArray&)),mW,
    20. SLOT(Receiver(const QByteArray &QBA)));
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. mainWidget::mainWidget()
    2. {
    3. setWindowTitle ( "paintEvent - Demo");
    4. resize( 1000, 400 );
    5. }
    6. void mainWidget::paintEvent ( QPaintEvent * event ) {
    7. QPainter painter(this);
    8. painter.begin( this );
    9. painter.setPen( QPen(Qt::black, 2) );
    10. painter.drawEllipse( 40, 40, 100, 100 );
    11. painter.setPen( QPen(Qt::green, 2) );
    12. int u,z,x1,v1,ku,ku1;
    13. for(int i=1;i<100;i++){
    14. u=4*i;
    15. x1=4*i+4;
    16. ku=CloseAdj[i+2];
    17. ku1=CloseAdj[i+3];
    18. painter.drawLine(u,z,x1,v1);
    19. }
    20. }
    21. void mainWidget::Receiver(const QByteArray &QBA){
    22. A=QBA;
    23. int j=0;
    24. while(A[j]!='2')
    25. {j++;
    26. }
    27. QVector<QString> days;
    28. QVector<float> CloseAdj;
    29. QVector<int> Volume;
    30. for(int i=j;i<1100;i++)
    31. {QString day =" ";
    32. QString volume = " ";
    33. QString CloseAd =" ";
    34. qint32 volume_=0;
    35. float Close=0;
    36. for (int k=0;k<10;k++)
    37. {day+=A[i+k];
    38. }
    39. i+=9;
    40. while(A[i]!=',')
    41. {//qDebug(" Problem bis Komma ");
    42. i++;
    43. }
    44. while(A[i+1]!=',')
    45. {i++;}
    46. while(A[i+2]!=',')
    47. {i++;}
    48. while(A[i+3]!=',')
    49. {i++;}
    50. while(A[i+4]!=',')
    51. {i++;}
    52. while(A[i+5]!=',')
    53. {volume+=A[i+5];
    54. i++;
    55. }
    56. while(((A[i+6]>='0' && A[i+6]<='9') || A[i+6]== '.')&& !(A[i+6]=='2' && A[i+7]=='0' && A[i+8]<='1' && A[i+9]<='5') )
    57. {CloseAd+=A[i+6];
    58. i++;
    59. }
    60. i+=6;
    61. Close=CloseAd.toFloat();
    62. days.append(day);
    63. CloseAdj.append(Close);
    64. volume_=volume.toInt();
    65. Volume.append(volume_);
    66. }
    67. for(int l=0;l<10;l++)
    68. {QString tag=days[l];
    69. }
    70. return;
    71. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a( argc, argv );
    4. char separator, step='d',tags;
    5. QString symbol="AAPL";
    6. int startDay=20,startMonth=7,startYear=2014,endDay=17, endMonth=10,endYear=2014,sM,eM;
    7. sM=startMonth-1;
    8. eM=endMonth-1;
    9. QUrl histUrl(QString("http://ichart.finance.yahoo.com/table.csv?s=%1&a=%2&b=%3&c=%4&d=%5&e=%6&f=%7&g=step&y=0&ignore=.cvs").arg(symbol).arg(sM).arg(startDay).arg(startYear).arg(eM).arg(endDay).arg(endYear),QUrl::TolerantMode);
    10. mainWidget w;
    11. w.show();
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    and I received the error message
    Fehler:no matching function for call to 'FileDownloader::connect(const char [26], mainWidget*&, const char [33])'
    Can somebody the necessary changes post?
    Last edited by anda_skoa; 16th January 2015 at 13:40. Reason: changed [quote] to [code]

Similar Threads

  1. Replies: 2
    Last Post: 8th August 2014, 19:08
  2. Replies: 6
    Last Post: 14th May 2014, 12:14
  3. Replies: 1
    Last Post: 22nd June 2011, 08:12
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. Replies: 11
    Last Post: 11th August 2008, 09:14

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.