1
votes

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:

  1. Created new project with Qt Widgets Applications which generates mainwindow.cpp and mainwindow.h files along with main.cpp file

  2. Created Push button on mainwindow.cpp file using form

  3. Created new dialog form using "add new" by right clicking on project

  4. Then written code to generate plot ( using Chart ) inside on_pushButton_clicked() in mainwindow.cpp file

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

1

1 Answers

0
votes

You create and show 2 windows: the aa as a modal dialog (exec() on a QDialog shows the dialog as modal) and the window.

To fix it you can show the plot inside the dialog, you'll need to create a layout for the dialog and add the chartView into that layout:

    //...
    //comment the other main window creation, we will setup the plot inside aa
    //![5]
    //QMainWindow window;
    //window.setCentralWidget(chartView);
    //window.resize(400, 300);
    //window.show();
    //
    //create a layout for the dialog (most likely you'll need to #include <QVBoxLayout>)
    aa.setLayout(new QVBoxLayout());
    //add chartView into the layout
    aa.layout()->addWidget(chartView);
    //...

Note: in case you want a non-modal dialog (or window), you will need to allocate the widget on heap (if allocated on the stack the window will just show and immediately close, because the destructor de-allocates the widget).