簡體   English   中英

Microsoft Visual Studio中帶有QWT的直方圖和Qt插件可立即關閉

[英]Histogram with QWT in Microsoft Visual Studio and Qt addin which closes immediately

我正在為我的GUI使用Qt add in 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

確實要在方法末尾顯示圖,但是當對它們使用exec()調用時,show()方法不會像QDialog類那樣受事件循環的阻礙。

它將被處理,但是無論哪種方式調用之后,您都將保留作用域。

有幾種方法可以解決此問題,但是我將努力實現Qt父/子層次結構,該結構中使用指針時會自動刪除。

1)Qt父母/子女關系

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

2)將“情節”設置為班級成員

plot.show();

並在類構造函數中進行構造。

3)使用智能指針

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

這取決於您在班級中的進一步學習背景,因此請嘗試理解這些方法並進行偷看。

plot應該使用new創建。 現在,您在堆棧上創建了它,因此on_plotButton_clicked函數完成后將立即刪除它。 在這里不應該使用Sleep ,它不會帶來任何好處。

QwtPlot* plot = new QwtPlot();
plot->setTitle( "Plot Demo" ); //Name the plot
//...
plot->show(); //Show plot

問題可能是您的QwtPlot只是一個局部變量,即使因為休眠也在主線程中,您也無法在函數返回之前及時繪制它。 然后,當它完成睡眠時,它將破壞您的本地QwtPlot對象並返回,因此,如果您看到這樣的窗口眨眼,則您很幸運。

要使其正常工作,您將必須這樣稱呼它:

QwtPlot* plot = new QwtPlot(this);

是將托管您的繪圖的父窗口(如果有)。 這樣,您的小部件將保持活動狀態,直到在程序執行結束時將其關閉或將其父級銷毀為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM