Windows下QT调用python脚本

环境 : QT5.8 python34 win64

.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目录中

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值