【问题标题】:Clicking on Push button should show the plot in different window using Qt C++单击 Push 按钮应使用 Qt C++ 在不同窗口中显示绘图
【发布时间】:2023-03-13 18:04:01
【问题描述】:

我正在尝试编写一个 Qt C++ 代码,该代码应该通过单击按钮在单独的窗口中进行绘图。

我创建了两个表单 1:主窗口,2:对话框。一旦我点击按钮,绘图会立即出现在不同的窗口中,然后另一个对话框窗体窗口会覆盖我的绘图窗口。

总结是点击Pushbutton会产生两个不可取的窗口。

我做了以下事情:

  1. 使用 Qt Widgets Applications 创建了新项目,该项目生成 mainwindow.cpp 和 mainwindow.h 文件以及 main.cpp 文件

  2. 使用表单在 mainwindow.cpp 文件上创建按钮

  3. 通过右键单击项目使用“添加新”创建新的对话框表单

  4. 然后编写代码在 mainwindow.cpp 文件的 on_pushButton_clicked() 中生成绘图(使用 Chart)

  5. 然后运行代码。最终出现按钮窗口。然后我点击 Push 按钮,然后它会生成两个单独的窗口 1:绘图窗口,2:空白对话框窗口

一共有三个*.cpp文件,两个*.h文件

这里是 mainwindow.cpp 代码:

#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();
}

实际结果:点击PushButton会产生两个独立的窗口1:绘图窗口,2:空白窗口

预期结果:单击 PushButton 应该只生成 PlotWindow

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    您创建并显示 2 个窗口:aa 作为模式对话框(QDialog 上的exec() 将对话框显示为模式)和window

    要修复它,您可以在对话框中显示绘图,您需要为对话框创建布局并将chartView 添加到该布局中:

        //...
        //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);
        //...
    

    注意:如果您想要一个非模态对话框(或窗口),则需要在堆上分配小部件(如果在堆栈上分配,窗口将只显示并立即关闭,因为析构函数会取消分配小部件)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 1970-01-01
      • 2011-10-30
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多