Results 1 to 2 of 2

Thread: Qcustomplot converter data in "setData"

  1. #1
    Join Date
    Dec 2024
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Qcustomplot converter data in "setData"

    Hello everyone,

    I made a program that collects data from an Arduino
    in x,y format in a listwidget. These x, y values ??must then be sent to the Qcustomplot and that's where there is a problem.

    There is a comma between x and y. They arrive like that from the Arduino to be used in the Excel graph.

    My problem is when collecting the data in the ui->customplot->graph(0)->setData(x,y) function;

    I can't convert the data from the "QString(data)" to convert them

    Here is the blocking part: (after ui->listwidget..)

    Qt Code:
    1. void NetworkPlot::readData()
    2. {
    3. qDebug() <<"Serialport works"; // it's ok (control port com)
    4. auto data = arduino->readAll(); // it's ok
    5. ui->listWidget->addItem(QString(data)); // it's ok
    6.  
    7. QString str = QString(data); // start of problem
    8. QStringList parts = str.split(" ");
    9. if (parts.size() == 2) {
    10. qDebug() << parts.at(1).toDouble() << parts.at(2).toDouble();
    11. mag.append(x) = parts.at(1).toDouble();
    12. num.append(y) = parts.at(2).toDouble();
    13.  
    14. QVector<double> mag;
    15. QVector<double> num;
    16. ui->customplot->graph(0)->setData(x,y);
    17. ui->customplot->replot();
    18. ui->customplot->update();
    19. }
    To copy to clipboard, switch view to plain text mode 
    My first test;

    Qt Code:
    1. void NetworkPlot::readData()
    2. {
    3. qDebug() <<"Serialport works"; //ok
    4. auto data = arduino->readAll(); //ok
    5. ui->listWidget->addItem(QString(data)); //ok
    6. QVector<double> x,y;
    7. ui->customplot->graph(0)->setData(x,y);
    8. ui->customplot->replot();
    9. ui->customplot->update();
    10. }
    To copy to clipboard, switch view to plain text mode 
    There is a formatting that totally escapes me and I spent hours testing. This is my first application in C++ and I only have knowledge in basic and arduino but it is progressing.

    I specify that I am French-speaking and I translate my request.

    Thank you for your help

    [IMG][/IMG]
    Last edited by d_stranz; 27th December 2024 at 23:03.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,266
    Thanks
    308
    Thanked 868 Times in 855 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qcustomplot converter data in "setData"

    Your first code example shouldn't even compile. You are using your QVector variables mag and num before you have declared them. In your second example, your QVector variables x and y have no content.

    There is a comma between x and y. They arrive like that from the Arduino to be used in the Excel graph.
    If that is the case, then why are you using QString ::split( " " )? This is telling QString to split the string on a blank space, not a comma.

    First question: What is arduino->readAll() returning? One (x,y) point or multiple points? From your code, it looks like you are receiving only one (x,y) pair for each call to readAll(). In that case, calling setData() on the QCustomPlot will only plot a single point, not a line.

    Your code should look something like this:

    Qt Code:
    1. // NetworkPlot.h
    2.  
    3. class NetworkPlot : public QWidget
    4. {
    5. Q_OBJECT
    6.  
    7. // ... Normal code
    8.  
    9. protected slots:
    10. void onDataReady();
    11.  
    12. private:
    13. QVector< double > xValues;
    14. QVector< double > yValues;
    15. };
    16.  
    17. // NetwokPlot.cpp
    18.  
    19. // This slot reads data from the arduino when the serial port emits the readyRead() signal
    20. void NetworkPlot::onDataReady()
    21. {
    22. auto data = QString( arduino->readAll() );
    23. if ( !data.empty() )
    24. {
    25. QStringList parts = data.split( "," );
    26. if ( parts.size() == 2 )
    27. {
    28. x = parts[0].toDouble();
    29. y = parts[1].toDouble();
    30.  
    31. xValues.push_back( x );
    32. yValues.push_back( y );
    33.  
    34. ui->customplot->graph(0)->setData( xValues, yValues );
    35. ui->customplot->replot();
    36. ui->customplot->update();
    37. }
    38. }
    39. }
    40.  
    41. // Call this method to start the data collection
    42. void NetworkPlot::collectAndPlotData()
    43. {
    44. // Connect the Arduino's serial port signal to the data reader slot so data can be received
    45. connect( arduino, &QIODevice::readyRead, this, &NetworkPlot::onDataReady() );
    46.  
    47. // Loop forever
    48. while ( true )
    49. {
    50. // Wait 5 seconds for the arduino to respond with a readyRead() signal
    51. if ( !arduino->waitForReadyRead( 5000 ) )
    52. break; // An error occurred or the wait timed out
    53.  
    54. // Keep the UI "alive" by processing any events in the event queue
    55. QCoreApplication::processEvents();
    56. }
    57.  
    58. // Disconnect the Arduino's serial port signal from the data reader slot; no more data will be read
    59. disconnect( arduino, &QIODevice::readyRead, this, &NetworkPlot::onDataReady() );
    60. }
    To copy to clipboard, switch view to plain text mode 

    There are several important parts to this code:

    1 - Reading data from a serial port is asynchronous. The port tells your program that data is available by sending a readyRead() signal.
    2 - Your program must have code that receives the readyRead() signal. This is the onDataReady() slot.
    3 - The data has to be stored outside of the read method. If you do not keep the data in member variables of your class, but use local variables instead, then every time the local variables go out of scope, the data disappears. That is why xValues and yValues are members of the NetworkPlot class.
    4 - The loop in collectAndPlotData() will run forever, until an error occurs on the arduino or no data is received for 5 seconds. Since waitForReadyRead() will block the execution of the program until more data is ready, the ui will "freeze" if the loop is always waiting. To make sure this doesn't happen, the loop calls processEvents() so other parts of the ui can update themselves (like the plot).

    I can't test this code so you will have to experiment to see if it works for you. I hope when you translate it into French it makes sense.
    Last edited by d_stranz; 28th December 2024 at 00:35.
    <=== 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: 1
    Last Post: 20th November 2015, 11:02
  2. Replies: 3
    Last Post: 16th March 2015, 08:31
  3. Replies: 1
    Last Post: 7th April 2010, 22:46
  4. Replies: 3
    Last Post: 25th August 2009, 14:03
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 20:05

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.