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?

Close QChartView with main window?

Scheduled Pinned Locked Moved Solved General and Desktop
qchartqchartviewcloseqtchartsmainwindow
3 Posts 2 Posters 2.2k Views 1 Watching
  • 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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on 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
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #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
      • pauleddP Offline
        pauleddP Offline
        pauledd
        wrote on last edited by
        #3

        Works, thank you.

        1 Reply Last reply
        0

        • Login

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