Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: Transfer of a QByteArray to main.cpp

  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]

  19. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Transfer of a QByteArray to main.cpp

    Qt Code:
    1. void FileDownloader::fileDownloaded(QNetworkReply* pReply)
    2. {
    3. m_DownloadedData = pReply->readAll();
    4. pReply->deleteLater();
    5. emit downloaded();
    6. emit DataA(m_DownloadedData);
    7. mainWidget *mW=new mainWidget();
    8. connect(SIGNAL(DataA(const QByteArray&)),mW,SLOT(Receiver(const QByteArray &QBA)));
    9. }
    To copy to clipboard, switch view to plain text mode 
    Are you sure you know what you're doing ? First, send a signal DataA() (line 6) and then create the recipient (line 7).
    In general, in such a situation you do not need signals and slots. You can transfer the data directly as a parameter to the constructor class mainWidget.

  20. The following user says thank you to Lesiok for this useful post:

    dolin93 (21st January 2015)

  21. #14
    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

    In your main() function you create a mainWidget and show it, which is very reasonable.

    But neither main() nor the mainWidget create a FileDownloader.

    My suggestion would be to create the FileDownloader in mainWidget and also set up the connection between the downloader and the widget right there.

    Cheers,
    _

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

    dolin93 (21st January 2015)

  23. #15
    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!
    At first I tried in vain to transfer the downloaded data without connect to mainWidget and then used connect in mainWidget
    Qt Code:
    1. class mainWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. mainWidget(const QUrl &Urlh);
    6. public slots:
    7. void Receiver(const QByteArray &QBA);
    8. protected:
    9. void paintEvent(QPaintEvent *event);
    10. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. mainWidget::mainWidget(const QUrl &Urlh):histUrl(Urlh)
    2. {
    3. setWindowTitle ( "paintEvent - Demo");
    4. resize( 1000, 400 );
    5. FileDownloader demo(histUrl);
    6. connect(SIGNAL(DataA(const QByteArray&)),demo,
    7. SLOT(Receiver(const QByteArray &QBA)));
    8. }
    9.  
    10.  
    11.  
    12. void mainWidget::paintEvent ( QPaintEvent * event ) {
    13. QPainter painter(this);
    14. painter.begin( this );
    15. painter.setPen( QPen(Qt::black, 2) );
    16. painter.drawEllipse( 40, 40, 100, 100 );
    17. painter.drawLine(0,200,80,200);
    18. painter.drawLine(340,200,800,200);
    19. painter.setPen(
    20. QPen(Qt::black, 5, Qt::DotLine, Qt::RoundCap));
    21. //painter.drawRect( 160, 40, 120, 150 );
    22. painter.setPen( QPen(Qt::green, 2) );
    23. int u,x1,ku,ku1;
    24. for(int i=1;i<8;i++)
    25. {
    26. u=10*i;
    27. x1=15*i;
    28. ku=CloseAdj[i+2];
    29. ku1=CloseAdj[i+7];
    30. painter.drawLine(u,ku,x1,ku1);
    31. }
    32.  
    33. }
    34. void mainWidget::Receiver(const QByteArray &QBA){
    35. A=QBA;
    36. int j=0;
    37. while(A[j]!='2')
    38. {j++;
    39. }
    40. QVector<QString> days;
    41. QVector<float> CloseAdj;
    42. QVector<int> Volume;
    43. for(int i=j;i<1100;i++)
    44. {QString day =" ";
    45. QString volume = " ";
    46. QString CloseAd =" ";
    47. qint32 volume_=0;
    48. float Close=0;
    49. for (int k=0;k<10;k++)
    50. {day+=A[i+k];
    51. }
    52. i+=9;
    53. while(A[i]!=',')
    54. {//qDebug(" Problem bis Komma ");
    55. i++;
    56. }
    57. while(A[i+1]!=',')
    58. {i++;}
    59. while(A[i+2]!=',')
    60. {i++;}
    61. while(A[i+3]!=',')
    62. {i++;}
    63. while(A[i+4]!=',')
    64. {i++;}
    65. while(A[i+5]!=',')
    66. {volume+=A[i+5];
    67. i++;
    68. }
    69. 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') )
    70. {CloseAd+=A[i+6];
    71. i++;
    72. }
    73. i+=6;
    74. Close=CloseAd.toFloat();
    75. days.append(day);
    76. CloseAdj.append(Close);
    77. volume_=volume.toInt();
    78. Volume.append(volume_);
    79. }
    80. for(int l=0;l<10;l++)
    81. {QString tag=days[l];
    82. }
    83. return;
    84. }
    To copy to clipboard, switch view to plain text mode 
    . The other files are
    Qt Code:
    1. class FileDownloader : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. FileDownloader(const QUrl &KUrl);
    6. virtual ~FileDownloader();
    7. signals:
    8. void downloaded();
    9. void DataA(m_DownloadedData);
    10. // private slots:
    11. public slots:
    12. void fileDownloaded(QNetworkReply* pReply);
    13. private:
    14. QUrl kUrl;
    15. QNetworkAccessManager m_WebCtrl;
    16. QByteArray m_DownloadedData;
    17. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. FileDownloader::FileDownloader( const QUrl &KUrl) : kUrl(KUrl)
    2. {
    3. connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)),
    4. SLOT(fileDownloaded(QNetworkReply*)),downloadedData());
    5. QNetworkRequest request(kUrl);
    6. m_WebCtrl.get(request);
    7. }
    8.  
    9. FileDownloader::~FileDownloader()
    10. {
    11.  
    12. }
    13.  
    14. void FileDownloader::fileDownloaded(QNetworkReply* pReply)
    15. {
    16. m_DownloadedData = pReply->readAll();
    17. pReply->deleteLater();
    18. emit downloaded();
    19. emit DataA(m_DownloadedData);
    To copy to clipboard, switch view to plain text mode 
    and
    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. QString row4;
    11. mainWidget w(const QUrl &Urlh);
    12. w.show();
    13. return a.exec();
    To copy to clipboard, switch view to plain text mode 
    . I received the error messages
    /../YahooGUI1/filedownloader.h:22: Fehler:'m_DownloadedData' has not been declared
    GUI1/main.cpp:26: Fehler:request for member 'show' in 'w', which is of non-class type 'mainWidget(const QUrl&)''
    .
    The difficulties in bringing the QUrl into mainWidget were almost the same as those in transfering the downloaded data without connect into mainWidget.
    I think gradually comes the time to give up.
    Last edited by anda_skoa; 18th January 2015 at 09:31. Reason: changed [quote] to [code]

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

    This line of main.cpp:
    Qt Code:
    1. mainWidget w(const QUrl &Urlh);
    To copy to clipboard, switch view to plain text mode 
    is not what you think it is. This is a declaration of a function w() that takes a QUrl and returns a mainWidget (that function does not exist). What you are trying to do is construct a mainWidget object and provide its constructor with an argument (a QUrl value) thus:
    Qt Code:
    1. mainWidget w(histUrl);
    2. w.show();
    To copy to clipboard, switch view to plain text mode 
    This is precisely the same sort of thing you do in the mainWidget constructor when you pass the QUrl value into the FileDownloader constructor.

    What is at line 22 of filedownloader.h?

    Edit: Answered my own question

    Line 22 of filedownloader.h must be this one:
    Qt Code:
    1. void DataA(m_DownloadedData);
    To copy to clipboard, switch view to plain text mode 
    The header is a declaration not an implementation. You are declaring a function DataA(...) that returns nothing and takes an argument. The stuff in the parentheses is the type (and optionally name) of the argument. What you have supplied is a name that is unknown as a type: hence the error. What you should be doing is:
    Qt Code:
    1. void DataA(const QByteArray &someName);
    To copy to clipboard, switch view to plain text mode 
    While this is a Qt signal, to C++ it is simply another function. The implementation of this function is provided by the Qt tools (moc), which is why you do not need/see this function implemented in filedownloader.cpp.


    It seems that a lack of understanding of C++, not Qt, is a reasonable part of the problems here. Rather than run up a white flag perhaps taking a step back and doing some generic C++ tutorials would be useful.
    Last edited by ChrisW67; 18th January 2015 at 02:32. Reason: updated contents

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

    dolin93 (21st January 2015)

  26. #17
    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

    ChrisW67 explained the most pressing issues already, here are some more.
    Quote Originally Posted by dolin93 View Post
    Qt Code:
    1. mainWidget::mainWidget(const QUrl &Urlh):histUrl(Urlh)
    2. {
    3. setWindowTitle ( "paintEvent - Demo");
    4. resize( 1000, 400 );
    5. FileDownloader demo(histUrl);
    6. connect(SIGNAL(DataA(const QByteArray&)),demo,
    7. SLOT(Receiver(const QByteArray &QBA)));
    8. }
    To copy to clipboard, switch view to plain text mode 
    The third line creates a FileDownloader instance on the stack of the mainWidget constructor, it will be gone when the constructor returns.
    The next line will not compile, the sender object needs to be the first argument of connect() and it needs to be a pointer.

    Cheers,
    _

  27. #18
    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 the correction of the first 2 errors and changes in mainWidget
    Qt Code:
    1. mainWidget::mainWidget(const QUrl &Urlh):histUrl(Urlh)
    2. {
    3. setWindowTitle ( "paintEvent - Demo");
    4. resize( 1000, 400 );
    5. }
    6. void mainWidget::paintEvent ( QPaintEvent * event ) {
    7. FileDownloader demo(histUrl);
    8. connect(&demo,SIGNAL(DataA(const QByteArray&)),
    9. SLOT(Receiver(const QByteArray &QBA)));
    10. QPainter painter(this);
    11. painter.begin( this );
    12. painter.setPen( QPen(Qt::black, 2) );
    13. painter.drawEllipse( 40, 40, 100, 100 );
    14. painter.drawLine(0,200,80,200);
    15. painter.drawLine(340,200,800,200);
    16. painter.setPen(
    17. QPen(Qt::black, 5, Qt::DotLine, Qt::RoundCap));
    18. //painter.drawRect( 160, 40, 120, 150 );
    19. painter.setPen( QPen(Qt::green, 2) );
    20. int u,x1,ku,ku1;
    21. for(int i=1;i<8;i++)
    22. {
    23. u=10*i;
    24. x1=15*i;
    25. ku=CloseAdj[i+2];
    26. ku1=CloseAdj[i+7];
    27. painter.drawLine(u,ku,x1,ku1);
    28. }
    29.  
    30. }
    31. void mainWidget::Receiver(const QByteArray &QBA){
    32. A=QBA;
    33. int j=0;
    34. while(A[j]!='2')
    35. {j++;
    36. }
    37. QVector<QString> days;
    38. // QVector<float> CloseAdj;
    39. QVector<int> Volume;
    40. for(int i=j;i<1100;i++)
    41. {QString day =" ";
    42. QString volume = " ";
    43. QString CloseAd =" ";
    44. qint32 volume_=0;
    45. float Close=0;
    46. for (int k=0;k<10;k++)
    47. {day+=A[i+k];
    48. }
    49. i+=9;
    50. while(A[i]!=',')
    51. {//qDebug(" Problem bis Komma ");
    52. i++;
    53. }
    54. while(A[i+1]!=',')
    55. {i++;}
    56. while(A[i+2]!=',')
    57. {i++;}
    58. while(A[i+3]!=',')
    59. {i++;}
    60. while(A[i+4]!=',')
    61. {i++;}
    62. while(A[i+5]!=',')
    63. {volume+=A[i+5];
    64. i++;
    65. }
    66. 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') )
    67. {CloseAd+=A[i+6];
    68. i++;
    69. }
    70. i+=6;
    71. Close=CloseAd.toFloat();
    72. days.append(day);
    73. CloseAdj.append(Close);
    74. volume_=volume.toInt();
    75. Volume.append(volume_);
    76. }
    77. for(int l=0;l<10;l++)
    78. {QString tag=days[l];
    79. }
    80. return;
    81. }
    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(const QUrl &Urlh);
    6. public slots:
    7. void Receiver(const QByteArray&);
    8. //void Receiver(const QByteArray &QBA);
    9. protected:
    10. void paintEvent(QPaintEvent *event);
    11. QUrl histUrl;
    12. QVector<float> CloseAdj;
    13. };
    To copy to clipboard, switch view to plain text mode 
    the program is compiled without error messages and "paintEvent - Demo" is showed according to
    Qt Code:
    1. painter.begin( this );
    2. painter.setPen( QPen(Qt::black, 2) );
    3. painter.drawEllipse( 40, 40, 100, 100 );
    4. painter.drawLine(0,200,80,200);
    5. painter.drawLine(340,200,800,200);
    6. painter.setPen(
    7. QPen(Qt::black, 5, Qt::DotLine, Qt::RoundCap));
    8. //painter.drawRect( 160, 40, 120, 150 );
    9. painter.setPen( QPen(Qt::green, 2) );
    10. int u,x1,ku,ku1;
    11. for(int i=1;i<8;i++)
    12. {
    13. u=10*i;
    14. x1=15*i;
    15. ku=CloseAdj[i+2];
    16. ku1=CloseAdj[i+7];
    17. painter.drawLine(u,ku,x1,ku1);
    18. }
    To copy to clipboard, switch view to plain text mode 
    with CloseAdj[ ]=0 because filedownloader.cpp is not called and I receive the message
    Qt Code:
    1. Object::connect: No such slot mainWidget::Receiver(const QByteArray &QBA)
    2. QPainter::begin: Painter already active
    3. Object::connect: No such slot mainWidget::Receiver(const QByteArray &QBA)
    4. QPainter::begin: Painter already active
    To copy to clipboard, switch view to plain text mode 
    . Some errors like mainWidget w(const QUrl &Urlh) or the former missing of the creation of a FileDownloader came only into the program because of the many changes and the copying of lines.
    Last edited by anda_skoa; 20th January 2015 at 08:08. Reason: changed [quote] to [code]

  28. #19
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Transfer of a QByteArray to main.cpp

    Do you understand what is meant in C++ by "object lifetime" or "object scope"? In the first line of your paintEvent(), you create a FileDownloader instance on the stack. As soon as your paintEvent() exits, this instance goes out of scope. Its lifetime is the duration of the paintEvent() call. Going out of scope means the object is destroyed. In Qt, any connections made using an object are disconnected when the object is destroyed. So your code creates a FileDownloader instance, connects it to a slot, and then as soon as the paintEvent exits, the instance is destroyed and the connection is broken.

    So all you did was to move a stack-based instance from your constructor to the paintEvent, but you didn't change what happens as a result at all.

    The error message you see is because you still have not used correct syntax in the connect() statement.

    Qt Code:
    1. connect(&demo,SIGNAL(DataA(const QByteArray&)), SLOT(Receiver(const QByteArray &QBA))); // << WRONG
    2.  
    3. connect(&demo,SIGNAL(DataA(const QByteArray&)), SLOT(Receiver(const QByteArray & ))); // << CORRECT (except for the "&demo" part)
    To copy to clipboard, switch view to plain text mode 

    Please, please learn to use CODE tags when you post code. They look like this [CODE ] and [/CODE]. (With no spaces between the [ and ]). Paste your code between them. Use the "Go Advanced" and "Preview Post" buttons to see what your post will look like before you post it.

  29. The following user says thank you to d_stranz for this useful post:

    dolin93 (21st January 2015)

  30. #20
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Transfer of a QByteArray to main.cpp

    Sorry, what I posted was still not correct. The connect() should look like this:

    Qt Code:
    1. connect(&demo,SIGNAL(DataA(const QByteArray&)), SLOT(Receiver(const QByteArray &QBA))); // << WRONG
    2.  
    3. connect(&demo,SIGNAL(DataA(const QByteArray&)), this, SLOT(Receiver(const QByteArray & ))); // << CORRECT (except for the "&demo" part)
    To copy to clipboard, switch view to plain text mode 

    I guess I was overwhelmed by the overwhelming number of errors.

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.