日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

C/C++/Qt與 Python 混合編程(2):Qt 調(diào)用嵌入python

系統(tǒng) 2251 0

在 Qt 的 Project 中添加一個(gè) py 文件。

C/C++/Qt與 Python 混合編程(2):Qt 調(diào)用嵌入python文件_第1張圖片

?

然后在 test_py.py 中的內(nèi)容如下:

            # This Python file uses the following encoding: utf-8
# if__name__ == "__main__":
# pass
def hello():
 print("hello world!")

          

只有一個(gè) hello()函數(shù),Qt 就是調(diào)用這個(gè) hello 函數(shù),然后執(zhí)行,顯示 hello,world!

在上一節(jié)的主文件中添加如下代碼:

             PyRun_SimpleString("import sys\n");
 PyRun_SimpleString("print(sys.path.append('/Users/wangxinnian/Downloads/qtApp/testQP1'))\n");
 PyObject* pModule = PyImport_ImportModule("test_py");
 if (!pModule){
 printf("不能打開 python file\n");
 Py_Finalize();
 return -1;
 }
 else printf("python文件已經(jīng)打開了!");
 PyObject* pFunHello = PyObject_GetAttrString(pModule, "hello"); // 這里的 hellow 就是 python 文件定義的。
 if (!pFunHello){
 cout << "Get function hello failed !" <
            
          

代碼分析 :

1。 引入了 python 的語(yǔ)句:

import sys

            sys.path.append("/Users/wangxinnian/Downloads/qtApp/testQP1")

          

設(shè)置 test_py 尋找的資源路徑,這個(gè)就是項(xiàng)目的目錄。

            2.使用PyImport_ImportModule

          

使用該函數(shù)打開 py 文件,取得該文件中的模塊函數(shù)

3. 找到 hello 函數(shù)

使用 PyObject_GeAttrString 查找到模塊中指定的函數(shù)

4. 然后執(zhí)行這個(gè)函數(shù)

             PyObject_CallFunction(pFunHello,nullptr);

          

運(yùn)行結(jié)果如下:

今日是: Thu Jul 25 10:04:03 2019

大家好!

None

hello world!

python文件已經(jīng)打開了!hello 模塊已經(jīng)打開了,開始執(zhí)行

完整的 main.cpp 內(nèi)容如下:

            #define PY_SSIZE_T_CLEAN
#include 
            
              
#include 
              
                
#include
                
                  
using namespace std;
int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 wchar_t *program = Py_DecodeLocale(argv[0], nullptr);
 if (program == nullptr) {
 fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
 exit(1);
 }
 Py_SetProgramName(program); /* optional but recommended */
 Py_Initialize();
 PyRun_SimpleString ("from time import time,ctime\n"
 "print('今日是:', ctime(time()))\n"
 "print('大家好!')\n"
 );
 PyRun_SimpleString("import sys\n");
 PyRun_SimpleString("print(sys.path.append('/Users/wangxinnian/Downloads/qtApp/testQP1'))\n");
 PyObject* pModule = PyImport_ImportModule("test_py");
 if (!pModule){
 printf("不能打開 python file\n");
 Py_Finalize();
 return -1;
 }
 else printf("python文件已經(jīng)打開了!");
 PyObject* pFunHello = PyObject_GetAttrString(pModule, "hello"); // 這里的 hellow 就是 python 文件定義的。
 if (!pFunHello){
 cout << "Get function hello failed !" <
                  
                  
                
              
            
          

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 壤塘县| 台湾省| 尼木县| 济源市| 台安县| 大化| 桦川县| 舒城县| 青田县| 瓦房店市| 泰兴市| 昆山市| 建平县| 板桥市| 稻城县| 乐业县| 岳西县| 黄陵县| 个旧市| 黎川县| 进贤县| 双柏县| 聊城市| 行唐县| 崇州市| 辉南县| 四川省| 拉萨市| 孟津县| 获嘉县| 崇州市| 锦屏县| 新竹市| 阜城县| 鄂托克前旗| 奉化市| 松阳县| 平乡县| 临漳县| 罗江县| 兴国县|