qt uses external libraries (python/opencv as an example)

If it is helpful to you, you can leave a like, follow, bookmark for the humble blogger (not) 

(Cheat about the data, maybe the interview will pass in the future, thank you)

In addition to the official library files that come with qt, sometimes you may want to use some third-party library files, or call python and other behaviors, so you need to introduce external libraries, the reason is very simple, and it is not difficult to implement, but there are some small things to pay attention to question.

The environment used in this article is qt 5.12.6 python3.7 64bit opencv 3.4.14 

One: qt tune python script

Create a qwidgets application, not much to say, you know how to create it

The next step is to import external libraries

 Naturally choose an external library and then pay attention to it next

The python library is a dynamic library, you need to find your own python storage directory, the name of the library file to be imported is libpythonXX.a

(XX is the version, and my python3.7 is 37). After finding it, confirm it. The next step is the include path. When you select the library file, the include path will be generated, but the default path is wrong and needs to be changed manually.

There is an include folder in the upper directory of the library file we just selected, this is the correct path

This is how it looks after the final selection. Let’s remove the suffix added to the debug version. The python I installed doesn’t seem to have a debug version, so I don’t choose it. It shouldn’t matter if I choose it. Then click Next, and then click Finish to complete the addition

 After adding, there will be a few more lines of code

So far we have successfully added the library 

have a test

Introduce the py header file in the cpp file that needs to use python, (Python.h)

Note: Due to the naming conflict, you need to cancel the original definition of qt first, then import the header file, and finally restore the original definition of qt

#测试用的py代码 py_demo.py

def show():
    print("hello!")
//main.cpp


#include "mainwindow.h"
#include<QDebug>
#include <QApplication>

#undef slots
#include <Python.h>
#define slots

int main(int argc, char *argv[])
{
    Py_Initialize();  //初始化python

    if(!Py_IsInitialized())   //如果初始化失败
    {
        qDebug()<<"Python init fail!";
        return -1;
    }
    //加载模块,模块名称为py_demo  (py_demo.py)
    PyObject *pModule = PyImport_ImportModule("py_demo");

    if(!pModule)   //加载失败
    {
        qDebug()<<"load pModule fail!";
        return -1;
    }

    PyObject* show = PyObject_GetAttrString(pModule, "show");   //加载show函数

    if(!show)   //加载函数失败
    {
        qDebug() << "load func show fail";
    }

    //PyEval_CallObject(show,NULL);   //调用函数show  旧式写法

    PyObject_CallFunction(show,nullptr,nullptr);  //新写法

    Py_Finalize();   //退出

    return 0;
}

Note: py_demo.py needs to be placed in the output directory (qt is compiled first before the output directory)

For example, my directory is as follows

The py file is placed here (in the debug of the output directory, if you use release, it is in the release)

 

 result:

Two: qt uses opencv

Create a project first, pure C++ is fine, there should be no way

Then there is another problem. The compiled version of opencv we downloaded is suitable for the compilation chain of VS, and there are two types of qt compilation, msvc and mingw. If we use mingw, we need to manually compile the source code of opencv

You can refer to this video for what you need here. The video of teacher Jia Zhigang at station b was also used in the environment of this tutorial, but my qt version is relatively low, and the opencv4 compilation failed. qt5.12 supports opencv3 at most. If you use a higher version, you can Watch the video directly, and only watch the compiled source code part of the video if it is relatively low

https://www.bilibili.com/video/BV1Za4y1v7ra/?spm_id_from=333.1007.top_right_bar_window_custom_collection.content.click&vd_source=49f5c9b51a6873d5fc74ea6b061d9259

Considering that the download of opencv on the official website is a bit slow, I uploaded the source code and the compiled library by the way. It should be fine.

The homepage you need is free self-pickup

The following introduces the use of mingw to compile opencv under the chain

This is my compiled library, other files are source code and opencv official compiled files, the demonstration only uses the library compiled by myself

Configure the environment variables first

The path is mingw_build/install/x64/mingw/bin

The file looks like this

 Then add the library, same as before

 Next, select the dynamic library, the library file path is as follows

The include directory is as follows

 Add d to the debug version suffix to uncheck it, click Next to complete

No error is reported in the test header file, it should be completed

Guess you like

Origin blog.csdn.net/ass133755/article/details/127073905