How to show QChart on the QMainWindow ?
-
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. -
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
-
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,
-
@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.