qt调用python代码,python的输入输出都为中文的情况@TOC
qt ui页面
textEdit为输入窗口,textEdit_out_2为输出窗口。
python代码(pythonproject.py)
def input_information(disease_information):
return disease_information
仅为输入输出。
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#undef slots
#include<Python.h>
#define slots Q_SLOTS
#include
#include
#include <string.h>
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
private slots:
void on_pushButton_2_clicked();
void py_call2(string disease_information);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#pragma execution_character_set(“utf-8”)
#include “mainwindow.h”
#include “ui_mainwindow.h”
#include
#include
#include
#include <windows.h>
using namespace std;
PyObject* StringToPyByWin(std::string str)
{
int wlen = ::MultiByteToWideChar(CP_ACP, NULL, str.c_str(), int(str.size()), NULL, 0);
wchar_t* wszString = new wchar_t[wlen + 1];
::MultiByteToWideChar(CP_ACP, NULL, str.c_str(), int(str.size()), wszString, wlen);
wszString[wlen] = ‘\0’;
PyObject* pobj = PyUnicode_FromUnicode((const Py_UNICODE*)wszString, wlen);
delete wszString;
return pobj;
}
void init()
{
QString path = QCoreApplication::applicationDirPath() + “/pythonEnv/”;
Py_SetPythonHome((wchar_t *)(reinterpret_cast<const wchar_t *>(path.utf16())));
Py_Initialize();
if( !Py_IsInitialized() )
{
qDebug()<<“初始化失败”;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./')");
}
string FromUnicode(const QString& qstr)
{
QTextCodec* pCodec = QTextCodec::codecForName(“gb2312”);
if(!pCodec)
return “”;
QByteArray arr = pCodec->fromUnicode(qstr);
string cstr = arr.data();
return cstr;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName(“gbk”);
#else
QTextCodec *codec = QTextCodec::codecForName(“utf-8”);
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName(“utf-8”);
QTextCodec::setCodecForLocale(codec);
#endif
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
QString qstr=ui->textEdit->toPlainText().trimmed();
qDebug()<<“qstr”<<qstr;
string tout1=FromUnicode(qstr);
//string tout1=qstr.toStdString();
py_call2(tout1);
}
void MainWindow::py_call2(string disease_information)
{
QString dis=QString::fromStdString(disease_information);
//qDebug()<<“dis”<<dis;
//qDebug()<<“111”;
//创建模块指针
PyObject* pModule = PyImport_ImportModule(“fastapi_client_hanshu”);//脚本名
//qDebug()<<“222”;
if (!pModule)
qDebug()<<“获取模块指针失败”;
qDebug()<<“获取模块指针成功”;
//创建函数指针
PyObject* pFunc= PyObject_GetAttrString(pModule,“input_information”);//获取函数input_information
if(!pFunc)
qDebug()<<“获取函数指针1失败”;
qDebug()<<“获取函数指针1成功”;
PyObject* pKeyWord =StringToPyByWin(disease_information);
PyObject* args = PyTuple_New(1);
//PyTuple_SetItem(args, 0, Py_BuildValue(“s”,disease_information));
PyTuple_SetItem(args, 0, pKeyWord);
if(!args)
qDebug()<<"参数错误";
else
{
qDebug()<<"参数正常";
}
qDebug()<<"333";
PyObject *FuncBack = PyObject_CallObject(pFunc, args);// 调用函数 input_information 并传入参数
if(FuncBack==NULL)
{
qDebug()<<"失败";
}
else
{
char* result;
PyArg_Parse(FuncBack, "s", &result);
QString qstr=QString::fromStdString(result);
qDebug()<<"qstr"<<qstr;
ui->textEdit_out_2->setPlainText(qstr);
}
Py_DECREF(pFunc);
Py_DECREF(pModule);
Py_Finalize();
}