2 njh19 njh19 于 2017.08.28 16:03 提问

QT作图问题,第一次运行正常,第二次崩溃

代码如下,画图是单独线程进行的,现在问题是第一次启动线程的时候是没有问题的,第二次程序就崩溃了

 void QTCurvePlotThread(LPVOID pParam)
{
    int argc = 1;
    char *argv[] = { "Qt" };
    static QApplication app(argc, argv);
    app.setQuitOnLastWindowClosed(true);

    QChart *charts[4] = { new QChart(), new QChart(), new QChart(), new QChart() };
    for(int k = 0; k < 8; k++)
    {
        //.. 向窗口中添加曲线
        QChart *chart = charts[k/2];

        QLineSeries *series = new QLineSeries();

        int offset = chart->series().count();
        for(int i = 0; i < 360; i++)
        {
            qreal x = offset * 20 + i;
            series->append(QPointF(i, qSin(2.0 * 3.141592 * x / 360.0)));
        }

        series->setName(QString("line " + QString::number(chart->series().size())));
        chart->addSeries(series);
        //.. 设置属性
        chart->setTitle("Zoom in/out example");
        chart->createDefaultAxes();
        chart->legend()->show();
        chart->legend()->setAlignment(Qt::AlignBottom);
        chart->setAnimationOptions(QChart::SeriesAnimations);
    }


    QSplitter *splitterMain = new QSplitter(Qt::Vertical, NULL/*app->widgetAt(0,0)*/);
    splitterMain->resize(720, 480);
    splitterMain->setOpaqueResize(true);
    splitterMain->setStretchFactor(1, 1);
    QChartView *view1 = new QChartView(charts[0], splitterMain);
    QChartView *view2 = new QChartView(charts[1], splitterMain);
    QChartView *view3 = new QChartView(charts[2], splitterMain);
    QChartView *view4 = new QChartView(charts[3], splitterMain);

    QGridLayout *m_mainLayout = new QGridLayout();
    m_mainLayout->addWidget(splitterMain, 0, 0, 1, 1);

    QWidget *window = new QWidget;
    window->setWindowTitle("CurvePloter");
    window->setLayout(m_mainLayout);
    window->show();


    app.exec();

    return;
}

2个回答

u013704336
u013704336   2017.08.31 14:23

1.从你的代码中没有看出问题来;
2.至于你说的第一次启动线程,没有看到你怎么启动的;
3.建议打几个断电,然后调试,看看程序时在那快奔溃了,这样也好找出问题;

Wussup
Wussup   2017.09.06 17:16

目测是两次线程中使用了静态的app的问题。

Csdn user default icon
上传中...
上传图片
插入图片