QtChart window disappears



  • Hello all,

    I'm trying to use the simple linechart example to work in my code. I'm trying to get the chart window to display from within a button function that does some calculations to get the data points. When the time comes for the chart to display, I can see it pop up and disappear almost instantly. After a lot of reading, I think it has to do with trying to create a new QMainWindow from inside the button? Here is my most recent code that is supposed to set up the chart window:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QtCharts/QChartView"
    #include "QtCharts/QLineSeries"
    #include "QtCharts/QLegend"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        //  Hide all Buttons            //
        ui->btnHeading->setVisible(false);
    }
        QMainWindow *chartWindow = new QMainWindow(0);
        chartWindow->setCentralWidget(&ChartView);
        chartWindow->resize(800,600);
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    And here is the button that is supposed to display the chart:

    void MainWindow::on_btnHeading_clicked()
    {
        QLineSeries *series = new QLineSeries();
    
        PGNListCount = PGNList.count();
        qDebug() << PGNListCount;
        for (int i=0;i<PGNListCount;i++) {
            if (PGNList.at(i).contains("127250")) {
                DB21Data << DB2.at(i) + (DB1.at(i));
                //graphTime << timeStamp.at(i);
                }
         }
    
        int DB21DataCount = DB21Data.count();
        for (int i=0; i<DB21DataCount;++i) {
            DB21hex = (((DB21Data.at(i).toInt(&ok, 16))*0.0001)*57.296);
            series->append(i, DB21hex);
            //qDebug() << series << DB21hex;
        }
    
        QChart *chart = new QChart();
            chart->legend()->hide();
            chart->addSeries(series);
            chart->createDefaultAxes();
            chart->setTitle("Heading");
    
        QChartView *chartView = new QChartView(chart);
            chartView->setRenderHint(QPainter::Antialiasing);
    
    //        QMainWindow window;
    //            window.setCentralWidget(chartView);
    //            window.resize(800, 600);
                chartWindow->show();
    
    }
    

    I've tried a few different things - this code errors on the two lines:

    chartWindow->setCentralWidget(&ChartView);
        chartWindow->resize(800,600);
    

    with "chartWindow does not name a type"

    Any hints on how I can approach this would be much appreciated!



  • You don't want your chart in a QMainWindow (an app has one of those), you want it in a QDialog.


  • Moderators

    @MScottM As "Main" in QMainWindow suggests a main window is the main window in an application (so many main :-)). That means - a Qt application can only have one QMainWindow. All other windows can be implemented using QWidget or QDialog (as @Chris-Hennes suggested).



  • Okay! Thanks for the suggestions. I am now able to create and show a qwidget window, now I'm working on how to put the QtChart on there...



  • I typically subclass QDialog for everything. So in that case, if you are doing it by hand (rather than using Designer) you are probably looking for something like this (from the QWidget docs):

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(chartView);
     setLayout(layout);
    


  • YES! Got it to display a chart with the data I want - now on to fine tuning everything.

    Thanks again for the hints that got me on the right track!



  • @Chris-Hennes Thanks again - I'm struggling with how to get it into a separate window that can be closed when done, instead of displaying on top of my existing window.



  • Are you subclassing QWidget or QDialog?


  • Moderators

    @MScottM Just call http://doc.qt.io/qt-5/qwidget.html#show on your dialog instead of exec()



  • Okay, this code:

    QChartView *chartView = new QChartView(chart);
            chartView->setRenderHint(QPainter::Antialiasing);
    
            QWidget * chartWindow = new QWidget(0);
    
            QVBoxLayout *layout = new QVBoxLayout(chartWindow);
            layout->addWidget(chartView);
            setLayout(layout);
            layout->activate();
            
            chartWindow->resize(480,320);
            chartWindow->show();
    

    opens my chart in a new window like I want, but, I get this error message in the debugger:

    QWidget::setLayout: Attempting to set QLayout "" on MainWindow "MainWindow", which already has a layout

    Also, if I close the window then click the button again, I get two charts side by side. I'm thinking there must be some code that properly resets everything when you close the window?

    Thanks again for all the help!



  • You don't want to just call setLayout() in this case, you want to call chartWindow->setLayout() -- otherwise you are calling the member of your current object (your main window).



  • Aaah! I get it! That solved the error message.


Log in to reply