Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Close QChartView with main window?
QtWS25 Last Chance

Close QChartView with main window?

Scheduled Pinned Locked Moved Solved General and Desktop
qchartqchartviewcloseqtchartsmainwindow
3 Posts 2 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pauledd
    wrote on 4 Oct 2016, 08:22 last edited by
    #1

    Hi, why doesnt a QChartView get closed if I close the mainwindow? Even when I put "cv.close" in the MainWindow destructor the chart window stays open...

    mainwindow.h

    class MainWindow : public QWidget
    {
    	Q_OBJECT
    public:
    	explicit MainWindow(QWidget *parent = 0);
    	~MainWindow();
    private:
    	QPushButton *pushButtonDrawChart;
    
    	QLineSeries *ls;
    	QChart *chart;
    	QChartView *cv;
    
    private slots:
    	void drawChart();
    };
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
    {
    	setGeometry(400,400,640,420);
    	pushButtonDrawChart = new QPushButton("Draw Chart", this);
    	connect(pushButtonDrawChart, &QPushButton::clicked, this, &MainWindow::drawChart);
    	}
    
    MainWindow::~MainWindow()
    {
    	qDebug() << "Destructor called";
    }
    
    void MainWindow::drawChart(){
    	
    	ls = new QLineSeries();
    	ls->append(0,1);
    	ls->append(1,2);
    	ls->append(2,3);
    	ls->append(3,4);
    	chart = new QChart();
    	chart->addSeries(ls);
    	chart->createDefaultAxes();
    	cv = new QChartView(chart);
    	cv->show();	
    }
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 4 Oct 2016, 09:07 last edited by VRonin 10 Apr 2016, 09:12
      #2
      • do not create a new chart and chartview every time you call drawChart, leave chart = new QChart(); and cv = new QChartView(chart); in the constructor.
      • call chart->removeAllSeries() in drawChart before adding a new one
      • use cv->show(); and cv->hide(); to togle the display of the chart
      • call chart->deleteLater(); and cv->deleteLater(); in MainWindow's destructor
      • no need to make ls a member, a local variable is enough
      • put pushButtonDrawChart in a layout to ensure it's displayed correctly

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • P Offline
        P Offline
        pauledd
        wrote on 5 Oct 2016, 08:53 last edited by
        #3

        Works, thank you.

        1 Reply Last reply
        0

        3/3

        5 Oct 2016, 08:53

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved