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. How to show QChart on the QMainWindow ?
Forum Updated to NodeBB v4.3 + New Features

How to show QChart on the QMainWindow ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 5.4k 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.
  • H Offline
    H Offline
    Hiloshi
    wrote on last edited by
    #1

    Dear Sirs,

    I am learning to use QChart, so I find a good example from tutorials. The original example put codes on:

    int main(int argc, char *argv[])
    {
    }
    

    But I want to use UI Design, so I create a new project from "QT Widgets Application", then put codes inside the:

    using namespace QtCharts;
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        //1
        QSplineSeries* series = new QSplineSeries();
        series->setName("spline");
        //1!
    
        //2
        series->append(0, 6);
        series->append(2, 4);
        series->append(3, 8);
        series->append(7, 4);
        series->append(10, 5);
        *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) << QPointF(20, 2);
        //2
    
        //3
        QChart *chart = new QChart();
        chart->legend()->hide();
        chart->addSeries(series);
        chart->setTitle("Simple spline chart example");
        chart->createDefaultAxes();
        chart->axisY()->setRange(0, 10);
        //3
    
        //4
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
        //4
    
        //5
        QMainWindow window;
        window.setCentralWidget(chartView);
        window.resize(400, 300);
        window.show();
        //5
    }
    

    After running above code, I see nothing on the screen. Why ?
    The original code in the main(int argc, char *argv[]) is working. What is the difference between the
    main(int argc, char *argv[]) and MainWindow(QWidget *parent) ?

    Any suggestions is appreciate,
    Thanks.

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi,

      One way or another you deleted the following line in the constructor:

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);//<----------------This line
      

      This is needed to set up the ui, as it says actually.

      Also you create a new QMainwindow, while you are already in a QMainwindow constructor. In main you need to do that, but here we are using the QT framework which already generated the important parts for us.

          //5
          QMainWindow window;
          window.setCentralWidget(chartView);
          window.resize(400, 300);
          window.show();
          //5
      

      replace the above code with the following:

      setCentralWidget(chartView);
      resize(400, 300);
      show();
      

      And you should be good to go.

      Eddy

      Qt Certified Specialist
      www.edalsolutions.be

      H 1 Reply Last reply
      3
      • EddyE Eddy

        Hi,

        One way or another you deleted the following line in the constructor:

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);//<----------------This line
        

        This is needed to set up the ui, as it says actually.

        Also you create a new QMainwindow, while you are already in a QMainwindow constructor. In main you need to do that, but here we are using the QT framework which already generated the important parts for us.

            //5
            QMainWindow window;
            window.setCentralWidget(chartView);
            window.resize(400, 300);
            window.show();
            //5
        

        replace the above code with the following:

        setCentralWidget(chartView);
        resize(400, 300);
        show();
        

        And you should be good to go.

        Eddy

        H Offline
        H Offline
        Hiloshi
        wrote on last edited by
        #3

        Dear @Eddy ,

        Thanks for the help. Now I understand why it is not working. Thanks again.

        I have another question, what if, instead of showing "chartView" on "QMainwindow", I want it show on certain page of "tabWidget", how should I do ?

        Normally, I will drag and drop a widget from UI design to tabWidget, however there is no such chartView UI widget, so I want to figure our how to create this widget by code and put in on the correct position.

        Appreciate for all the help,

        Thanks,

        jsulmJ 1 Reply Last reply
        1
        • H Hiloshi

          Dear @Eddy ,

          Thanks for the help. Now I understand why it is not working. Thanks again.

          I have another question, what if, instead of showing "chartView" on "QMainwindow", I want it show on certain page of "tabWidget", how should I do ?

          Normally, I will drag and drop a widget from UI design to tabWidget, however there is no such chartView UI widget, so I want to figure our how to create this widget by code and put in on the correct position.

          Appreciate for all the help,

          Thanks,

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

          @Hiloshi It is actually described in the documentation (http://doc.qt.io/qt-5/qtabwidget.html):
          "The normal way to use QTabWidget is to do the following:

          Create a QTabWidget.
          Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
          Insert child widgets into the page widget, using layouts to position them as normal.
          Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.
          

          "

          "Insert child widgets into the page widget" - your child widget would be the chartView.

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

          H 1 Reply Last reply
          3
          • jsulmJ jsulm

            @Hiloshi It is actually described in the documentation (http://doc.qt.io/qt-5/qtabwidget.html):
            "The normal way to use QTabWidget is to do the following:

            Create a QTabWidget.
            Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
            Insert child widgets into the page widget, using layouts to position them as normal.
            Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.
            

            "

            "Insert child widgets into the page widget" - your child widget would be the chartView.

            H Offline
            H Offline
            Hiloshi
            wrote on last edited by
            #5

            Dear @jsulm ,

            Thank you very much. I understand.

            Thanks,

            1 Reply Last reply
            1

            • Login

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