You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Qt C++应用中嵌入PySide/PyQt Widget并获取返回值?

在Qt C中调用PySide/PyQt Widget并获取C兼容字符串结果

可行方案分为两种,根据场景选择:


方案一:C++嵌入Python解释器直接调用

这种方式适合需要在C++进程内直接交互的场景,能灵活传递数据。

步骤1:编写PySide/PyQt自定义Widget

实现一个模态对话框,用户操作后返回字符串结果,通过__call__方法让实例可以像函数一样调用:

# custom_widget.py
from PySide6.QtWidgets import QDialog, QLineEdit, QPushButton, QVBoxLayout

class CustomPySideWidget(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("输入内容")
        self.result_str = ""
        
        layout = QVBoxLayout()
        self.input_edit = QLineEdit()
        self.ok_btn = QPushButton("确认")
        self.ok_btn.clicked.connect(self.on_ok_clicked)
        
        layout.addWidget(self.input_edit)
        layout.addWidget(self.ok_btn)
        self.setLayout(layout)
    
    def on_ok_clicked(self):
        self.result_str = self.input_edit.text()
        self.accept()
    
    def __call__(self):
        # 调用实例时自动弹出对话框,返回结果
        self.exec()
        return self.result_str

步骤2:C++中嵌入Python解释器调用Widget

在C++代码中初始化Python环境,导入自定义模块,实例化Widget并获取结果:

#include <QApplication>
#include <QString>
#include <Python.h>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 初始化Python解释器
    Py_Initialize();
    if (!Py_IsInitialized()) {
        qFatal("Python解释器初始化失败");
        return 1;
    }

    // 添加脚本路径到Python搜索路径
    PyObject* sys_path = PySys_GetObject((char*)"path");
    PyObject* script_dir = PyUnicode_FromString("."); // 脚本在当前目录
    PyList_Append(sys_path, script_dir);
    Py_DECREF(script_dir);

    // 导入自定义模块
    PyObject* module = PyImport_ImportModule("custom_widget");
    if (!module) {
        PyErr_Print();
        Py_Finalize();
        return 1;
    }

    // 获取Widget类
    PyObject* widget_class = PyObject_GetAttrString(module, "CustomPySideWidget");
    if (!widget_class || !PyCallable_Check(widget_class)) {
        PyErr_Print();
        Py_DECREF(module);
        Py_Finalize();
        return 1;
    }

    // 创建Widget实例
    PyObject* widget_instance = PyObject_CallObject(widget_class, nullptr);
    if (!widget_instance) {
        PyErr_Print();
        Py_DECREF(widget_class);
        Py_DECREF(module);
        Py_Finalize();
        return 1;
    }

    // 调用实例获取结果
    PyObject* result_obj = PyObject_CallObject(widget_instance, nullptr);
    QString result_str;
    if (result_obj && PyUnicode_Check(result_obj)) {
        const char* c_str = PyUnicode_AsUTF8(result_obj);
        result_str = QString::fromUtf8(c_str);
    } else {
        PyErr_Print();
        result_str = "获取结果失败";
    }

    // 释放Python对象
    Py_XDECREF(result_obj);
    Py_DECREF(widget_instance);
    Py_DECREF(widget_class);
    Py_DECREF(module);
    Py_Finalize();

    // 使用结果
    qDebug() << "获取到的结果:" << result_str;

    return app.exec();
}

注意事项

  • 确保C++项目链接对应版本的Python库(如python310.lib),并包含Python头文件路径。
  • 保证PySide6/PyQt6安装在C++使用的Python环境中,版本匹配。

方案二:通过QProcess调用Python脚本

这种方式配置简单,无需嵌入Python解释器,适合仅需传递字符串结果的场景。

步骤1:编写可独立运行的Python脚本

脚本运行Widget后,将结果输出到标准输出:

# custom_widget_cli.py
from PySide6.QtWidgets import QApplication, QDialog, QLineEdit, QPushButton, QVBoxLayout
import sys

class CustomPySideWidget(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("输入内容")
        self.result_str = ""
        
        layout = QVBoxLayout()
        self.input_edit = QLineEdit()
        self.ok_btn = QPushButton("确认")
        self.ok_btn.clicked.connect(self.on_ok_clicked)
        
        layout.addWidget(self.input_edit)
        layout.addWidget(self.ok_btn)
        self.setLayout(layout)
    
    def on_ok_clicked(self):
        self.result_str = self.input_edit.text()
        self.accept()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = CustomPySideWidget()
    if widget.exec() == QDialog.Accepted:
        print(widget.result_str)
    sys.exit(0)

步骤2:C++中用QProcess调用脚本并读取结果

#include <QApplication>
#include <QProcess>
#include <QString>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QProcess process;
    // 替换为你的Python可执行文件路径,如"python3"或完整路径
    process.start("python", QStringList() << "custom_widget_cli.py");
    process.waitForFinished();

    QString result_str = process.readAllStandardOutput().trimmed();
    qDebug() << "获取到的结果:" << result_str;

    return app.exec();
}

两种方案对比

方案优点缺点
嵌入Python解释器进程内交互,灵活传递复杂对象编译配置复杂,依赖Python环境
QProcess调用配置简单,无需链接Python库仅能通过标准IO传递数据,交互能力有限

内容的提问来源于stack exchange,提问作者NejoFS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 19:45:48