1. 程式人生 > >Windows下QT呼叫python指令碼

Windows下QT呼叫python指令碼

.pro檔案

#-------------------------------------------------
#
# Project created by QtCreator 2018-10-13T10:32:41
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = palm-tech
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += main.cpp\
        Widget.cpp \
    PdfReport.cpp \
    ManagerPythonInterface.cpp

HEADERS  += Widget.h \
    PdfReport.h \
    ManagerPythonInterface.h

FORMS    += Widget.ui

INCLUDEPATH += -I  D:\Python34\include
LIBS += -L D:\Python34\libs -lpython34

ManagerPythonInterface.cpp

#include "ManagerPythonInterface.h"
#include <Python.h>
#include <QDebug>

ManagerPythonInterface::ManagerPythonInterface()
{
    //初始化Python直譯器,這是呼叫操作的第一步
     Py_Initialize();
     if( !Py_IsInitialized() ){
     return;
     }

    //執行單句Python語句,用於給出呼叫模組的路徑,否則將無法找到相應的呼叫模組
     PyRun_SimpleString("import sys");
     PyRun_SimpleString("sys.path.append('./')");

    //獲取qt_python_fun.py模組的指標
     PyObject* pModule = PyImport_ImportModule("HtmlHandler");
     if (! pModule){
        qDebug()<<QObject::tr("Can't open python file\n");
        return;
     }

    //獲取hello函式的指標
     PyObject* pFunHandler = PyObject_GetAttrString(pModule,"htmlHandleTest");
     if (!pFunHandler){
        qDebug()<<QObject::tr("Get function hello failed\n");
        return;
     }

    //呼叫函式,傳入引數為NULL
     PyObject_CallFunction(pFunHandler,NULL);
    //銷燬Python直譯器,這是呼叫的最後一步
     Py_Finalize();

}

遇到的問題:

  1. 編譯錯誤 PyType_Slot *slots; object.h(445) : error C2059: 語法錯誤:“;” object.h(445) : error C2238: 意外的標記位於“;”之前

就是關於slots關鍵字的問題。 找到該欄位 PyType_Slot *slots; 修改為 PyType_Slot *_slots;

2.編譯錯誤

In file included from d:/Qt/Qt5.8.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/random:38:0,
                 from d:/Qt/Qt5.8.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/bits/stl_algo.h:66,
                 from d:/Qt/Qt5.8.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/algorithm:62,
                 from D:\Qt\Qt5.8.0\5.8\mingw53_32\include/QtCore/qglobal.h:108,
                 from D:\Qt\Qt5.8.0\5.8\mingw53_32\include/QtCore/qalgorithms.h:43,
                 from D:\Qt\Qt5.8.0\5.8\mingw53_32\include\QtCore/qdebug.h:44,
                 from D:\Qt\Qt5.8.0\5.8\mingw53_32\include\QtCore/QDebug:1,
                 from ..\palm-tech\ManagerPythonInterface.cpp:3:
d:/Qt/Qt5.8.0/Tools/mingw530_32/i686-w64-mingw32/include/c++/cmath:1119:11: error: '::hypot' has not been declared
   using ::hypot;
           ^
Makefile.Debug:862: recipe for target 'debug/ManagerPythonInterface.o' failed
mingw32-make[1]: Leaving directory 'E:/QT/Works/palm-tech/master/build-palm-tech-Desktop_Qt_5_8_0_MinGW_32bit-Debug'
mingw32-make[1]: *** [debug/ManagerPythonInterface.o] Error 1

解決方法,把#include<python.h> 檔案語句放到QT相關包含標頭檔案的後面。

2.執行錯誤 需要把python檔案放到執行目錄中,也就是debug目錄中