c++ - Microsoft Visual Studio 中的 QWT 直方图和立即关闭的 Qt 插件

标签 c++ visual-studio-2010 qt opencv qwt

我在我的 GUI 中使用 Qt add in for Visual Studio C++。

在我的 GUI 上,我有一个名为 plotButton 的按钮,单击它会绘制图像的直方图。我的绘图选项是通过使用 QWT。

但是,它似乎并没有策划任何事情,几乎立即关闭。试过 sleep(),但它似乎也不起作用。问题可能出在我的代码上吗? 这是我的代码供引用:

void qt5test1 ::on_plotButton_clicked()
{
    //Convert to grayscale
    cv::cvtColor(image, image, CV_BGR2GRAY);


    int histSize[1] = {256}; // number of bins
    float hranges[2] = {0.0, 255.0}; // min andax pixel value
    const float* ranges[1] = {hranges};
    int channels[1] = {0}; // only 1 channel used

    cv::MatND hist;
    // Compute histogram
    calcHist(&image, 1, channels, cv::Mat(), hist, 1, histSize, ranges);

    double minVal, maxVal;
    cv::minMaxLoc(hist, &minVal, &maxVal);//Locate max and min values

    QwtPlot plot; //Create plot widget
    plot.setTitle( "Plot Demo" ); //Name the plot
    plot.setCanvasBackground( Qt::black ); //Set the Background colour
    plot.setAxisScale( QwtPlot::yLeft, minVal, maxVal ); //Scale the y-axis
    plot.setAxisScale(QwtPlot::xBottom,0,255); //Scale the x-axis
    plot.insertLegend(new QwtLegend()); //Insert a legend

    QwtPlotCurve *curve = new QwtPlotCurve(); // Create a curve
    curve->setTitle("Count"); //Name the curve
    curve->setPen( Qt::white, 2);//Set colour and thickness for drawing the curve 
    //Use Antialiasing to improve plot render quality
    curve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
    /*Insert the points that should be plotted on the graph in a 
    Vector of QPoints or a QPolgonF */
    QPolygonF points;
    for( int h = 0; h < histSize[0]; ++h) {
        float bin_value = hist.at<float>(h);
        points << QPointF((float)h, bin_value);
    }

    curve->setSamples( points ); //pass points to be drawn on the curve
    curve->attach( &plot ); // Attach curve to the plot 
    plot.resize( 600, 400 ); //Resize the plot
    plot.replot();
    plot.show(); //Show plot
    Sleep(100);
}

加载图像后单击此按钮,将出现一个窗口并立即消失。在输出窗口下,可以找到这些行。

First-chance exception at 0x75d54b32 (KernelBase.dll) in qt5test1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0044939c..
Unhandled exception at 0x75d54b32 (KernelBase.dll) in qt5test1.exe: Microsoft C++ exception: cv::Exception at memory location 0x0044939c..

有人知道我的代码有什么问题吗?谢谢。请再次注意该程序是在 Microsoft Visual Studio C++ 中构建和编写的。谢谢。

最佳答案

问题是你在这里构造了一个栈对象:

QwtPlot plot; //Create plot widget

的确,您试图在方法的末尾显示绘图,但是当您对 QDialog 类使用 exec() 调用时,show() 方法并没有像 QDialog 类那样被事件循环阻塞。

它会被处理,但无论哪种方式,您都将在调用后立即离开范围。

有几种方法可以解决这个问题,但我会争取使用指针时自动删除的 Qt 父/子层次结构。

1) Qt父子关系

QwtPlot *plot = new QwtPlot(this);
                            ^^^^

2)让“情节”成为类(class)成员

plot.show();

并在类构造函数中构造它。

3)使用智能指针

QSharedPointer<QwtPlot> plot = QSharedPointer<QwtPlot>(new QwtPlot());

选择哪种方式取决于您对类(class)的进一步了解,因此请尝试理解这些方法,然后看一看。

关于c++ - Microsoft Visual Studio 中的 QWT 直方图和立即关闭的 Qt 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21119760/

相关文章:

c++ - 模板匹配与模板更新

vb.net - 如何在 Visual Studio 2010 Ultimate 的 VB.NET 中获得 "catch with rethrow" block 的 100% 代码覆盖率?

c++ - VC++ 2010 IntelliSense 自动完成列表消失得太快

c++ - 如何在Qt 5.3中使用QCamera设置图像分辨率?

c++ - 结构 vector 的二进制搜索

c++ - ROOT(cern) FILE reading+TGraph 工作正常,但 TGraphErrors 不想要

c++ - 显示模态对话框时禁用所有顶级窗口(WS_POPUP)

c++ - SDL 不绘制图像

c++ - Visual Studio 中的 Qml

c++ - 添加多个 QDockWidget