简体   繁体   中英

The program has unexpectedly finished in QT opencv

I know that this has been posted many times,but I could not find the solution from previous posts. I followed tutorial on How to setup Qt and openCV on Windows from wiki Qt.

My .pro file and mainwindows.cpp are shown below. I wanted to open image following the example. What is wrong here? Checked the opencv version and it is the same as libs included. The PATH is also correct. The cpp file

#include "ui_mainwindow.h"

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    cv::Mat image = cv::imread("C://1.jpg", 1);
    cv::namedWindow("My Image");
    cv::imshow("My Image", image);
}

MainWindow::~MainWindow()
{
    delete ui;
}

and

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = opencvtest
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h
FORMS    += mainwindow.ui

INCLUDEPATH += C:\opencv\build\include
LIBS += C:\opencv-build\bin\libopencv_core451.dll
LIBS += C:\opencv-build\bin\libopencv_highgui451.dll
LIBS += C:\opencv-build\bin\libopencv_imgcodecs451.dll
LIBS += C:\opencv-build\bin\libopencv_imgproc451.dll
LIBS += C:\opencv-build\bin\libopencv_features2d451.dll
LIBS += C:\opencv-build\bin\libopencv_calib3d451.dll
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

When program crashes like that under Qt Creator, and you have extra libraries, the very likely reason is that the extra libraries are missing from the runtime PATH .

In this case, you need to add C:\opencv-build\bin to the PATH. There are at least 3 ways to go about it.

  1. Edit the system environment, so that the relevant directory is always in the system PATH . You need to restart Qt Creator for this change to take effect. This is not the recommended way, unless you actually want these things in there also for other purposes.

  2. You can edit the Build environment of the project under Qt Creator Project view. There's separate configuration for each build type, so you may need to do this to them all separately, which both good and bad. It is good, because then you can have different directory for different builds (for example debug vs relase, MSVC vs MinGW builds). It's bad because it's extra hassle and makes it easier to have something wrong.

  3. You can add it to the run environment in the Qt Creator Project view. Then it will be the same for all build types.

In this case, 3 is probably the way to go.


Qt Creator annoyingly does not display any information about which DLL is missing, it just says the program crashed. This can be solved by instead string the "Qt command prompt" for the correct toolchain from Windows Start menu (search Qt and you should find it). Then go to the built.exe directory and run the.exe. You should now get an error dialog where Windows tells you which DLL it failed to find. Then you can look where that DLL is and add it to the path and try again, until the program starts. After you know the directories using this method, you can then add them to Qt Creator as explained above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM