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
Forum Updated to NodeBB v4.3 + New Features

QtChart window disappears

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 3.9k 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.
  • M Offline
    M Offline
    MScottM
    wrote on 19 Apr 2017, 14:49 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!

    J 1 Reply Last reply 20 Apr 2017, 04:48
    0
    • C Offline
      C Offline
      Chris Hennes
      wrote on 19 Apr 2017, 20:24 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
        19 Apr 2017, 14:49

        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!

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 20 Apr 2017, 04:48 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 20 Apr 2017, 13:38 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
          • C Offline
            C Offline
            Chris Hennes
            wrote on 20 Apr 2017, 14:03 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 20 Apr 2017, 14:29 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 20 Apr 2017, 17:53 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.

                J 1 Reply Last reply 21 Apr 2017, 04:25
                0
                • C Offline
                  C Offline
                  Chris Hennes
                  wrote on 20 Apr 2017, 20:13 last edited by
                  #8

                  Are you subclassing QWidget or QDialog?

                  Chris Hennes, Pioneer Library System

                  1 Reply Last reply
                  0
                  • M MScottM
                    20 Apr 2017, 17:53

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

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 21 Apr 2017, 04:25 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 21 Apr 2017, 13:35 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
                      • C Offline
                        C Offline
                        Chris Hennes
                        wrote on 21 Apr 2017, 13:38 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 21 Apr 2017, 13:45 last edited by
                          #12

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

                          1 Reply Last reply
                          0

                          1/12

                          19 Apr 2017, 14:49

                          • Login

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