Results 1 to 3 of 3

Thread: Dynamically change data to QCharts

  1. #1
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Dynamically change data to QCharts

    Hello.

    I studied this example http://doc.qt.io/qt-5/qtcharts-scatt...t-example.html
    and wonder how can I dynamically change my data to plot?

    this is main
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. ChartView *chartView = new ChartView();
    6. QMainWindow window;
    7. window.setCentralWidget(chartView);
    8. window.resize(400, 300);
    9. window.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    and since window.show(); I cannot do anything with my plot...
    Is it utterly possible dynamically change my plot?

  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: Dynamically change data to QCharts

    Quote Originally Posted by Blitzor DDD View Post
    and since window.show(); I cannot do anything with my plot...
    What do you mean with "can't to anything"?
    What do you want to do?

    Quote Originally Posted by Blitzor DDD View Post
    Is it utterly possible dynamically change my plot?
    Why would that be impossible?

    The QChartView has a setter for a QChart and QChart has several methods to change its data.
    Does any of these methods not work when you call it?

    Cheers,
    _

  3. #3
    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: Dynamically change data to QCharts

    I cannot do anything with my plot...
    Your problem is that you have written your code in a way that gives you no ability to access your ChartView. You use a plain QMainWindow as the main window for your app and create your ChartView in main() where the pointer is not exposed to anything else.

    Derive your own MainWindow class from QMainWindow. In the constructor for that class, create your ChartView, save the pointer to a member variable of the MainWindow class, and add it as the central widget:

    Qt Code:
    1. // MainWindow.h
    2.  
    3. #include <QMainWindow>
    4. #include <ChartView.h> // or whatever the name of the file is
    5.  
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow( QWidget * parent = 0 );
    12.  
    13. private:
    14. ChartView * pChartView;
    15. }
    16.  
    17. // MainWindow.cpp
    18.  
    19. #include "MainWindow.h"
    20.  
    21. MainWindow::MainWindow( QWidget * parent )
    22. : QMainWindow( parent )
    23. {
    24.  
    25. pChartView = new ChartView();
    26. setCentralWidget( pChartView );
    27. resize( 400, 300 );
    28. }
    29.  
    30. // main.cpp
    31.  
    32. int main(int argc, char *argv[])
    33. {
    34. QApplication a(argc, argv);
    35.  
    36. MainWindow window;
    37. window.show();
    38.  
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Now you have a pointer to your ChartView inside your MainWindow instance, so you can do whatever you want to change the plot or update its data.
    <=== 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: 5
    Last Post: 19th March 2014, 12:05
  2. Change the ListModel dynamically
    By stereoMatching in forum Qt Quick
    Replies: 1
    Last Post: 6th July 2013, 21:11
  3. Dynamically change slider max range
    By Don_Hannes in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 16:19
  4. Replies: 3
    Last Post: 10th October 2011, 08:44
  5. Dynamically change the layout
    By pippo42 in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2009, 13:01

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.