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. QtChart window disappears
QtWS25 Last Chance

QtChart window disappears

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 3.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.
  • M Offline
    M Offline
    MScottM
    wrote on last edited by
    #1

    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!

    jsulmJ 1 Reply Last reply
    0
    • Chris HennesC Offline
      Chris HennesC Offline
      Chris Hennes
      wrote on last edited by
      #2

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

      Chris Hennes, Pioneer Library System

      1 Reply Last reply
      2
      • M MScottM

        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!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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).

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • M Offline
          M Offline
          MScottM
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • Chris HennesC Offline
            Chris HennesC Offline
            Chris Hennes
            wrote on last edited by
            #5

            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);
            

            Chris Hennes, Pioneer Library System

            1 Reply Last reply
            0
            • M Offline
              M Offline
              MScottM
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MScottM
                wrote on last edited by
                #7

                @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.

                jsulmJ 1 Reply Last reply
                0
                • Chris HennesC Offline
                  Chris HennesC Offline
                  Chris Hennes
                  wrote on last edited by
                  #8

                  Are you subclassing QWidget or QDialog?

                  Chris Hennes, Pioneer Library System

                  1 Reply Last reply
                  0
                  • M MScottM

                    @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.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

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

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      MScottM
                      wrote on last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0
                      • Chris HennesC Offline
                        Chris HennesC Offline
                        Chris Hennes
                        wrote on last edited by
                        #11

                        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).

                        Chris Hennes, Pioneer Library System

                        1 Reply Last reply
                        1
                        • M Offline
                          M Offline
                          MScottM
                          wrote on last edited by
                          #12

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

                          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