I am trying to write a Qt C++ code which should make a plot in separate windows by clicking Push Button.
I did by creating two forms 1: mainwindow , 2: dialog. Once I click on Pushbutton, immediately plot appears in different windows and next another window of dialog form is posed up covering my plot window.
The summary is that clicking on Pushbutton generates two windows which is not desirable.
I did the followings:
Created new project with Qt Widgets Applications which generates mainwindow.cpp and mainwindow.h files along with main.cpp file
Created Push button on mainwindow.cpp file using form
Created new dialog form using "add new" by right clicking on project
Then written code to generate plot ( using Chart ) inside on_pushButton_clicked() in mainwindow.cpp file
Then run the code . Eventually PushButton Window appears. Then I click on Push button, Then it generates two separate windows 1: Plot windows, 2: Blank dialog Window
There are total three *.cpp files , two *.h files
Here is mainwindow.cpp code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"dialog.h"
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
QT_CHARTS_USE_NAMESPACE
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
Dialog aa;
aa.setDisabled(false);
//aa.display();
//![1]
QLineSeries *series = new QLineSeries();
//![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->createDefaultAxes();
chart->setTitle("Simple line chart example");
//![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]
aa.exec();
}
Actual result: Clicking on PushButton generates two separate windows 1: Plot window, 2: blank window
Expected Results: Clicking on PushButton should generate only PlotWindow