简体   繁体   English

Qt5在Python脚本中调用函数

[英]Qt5 call a function in a Python script

I have a simple Qt5 project with a button and a text field, I did create also a py file in the project to check how to call functions in python files, from Qt. 我有一个带有按钮和文本字段的简单Qt5项目,我也确实在该项目中创建了一个py文件,以检查如何从Qt调用python文件中的函数。

Although now I am stuck; 虽然现在我被困住了; I have my testcpp.h and testcpp.cpp, where I define a string and a function to print that string; 我有testcpp.h和testcpp.cpp,其中定义了一个字符串和一个用于打印该字符串的函数; although I am not sure how do I call now the function in the python file, which simply print a string. 尽管我不确定现在如何在python文件中调用该函数,该函数只是打印一个字符串。

I want to call the Qt function, and have it call the function in the python file; 我想调用Qt函数,并让它在python文件中调用该函数; how do you do that? 你是怎样做的?

testcpp.h file: testcpp.h文件:

#ifndef TESTCPP_H
#define TESTCPP_H

#include <QObject>
#include <QString>

class testcpp : public QObject
{
    Q_OBJECT
public:
    explicit testcpp(QObject *parent = nullptr);

    QString getTest_to_print() const;
    void setTest_to_print(const QString &value);

signals:

public slots:

private:
    QString test_to_print;

};

#endif // TESTCPP_H

testcpp.cpp file: testcpp.cpp文件:

#include "testcpp.h"

testcpp::testcpp(QObject *parent) : QObject(parent)
{

}

QString testcpp::getTest_to_print() const
{
    return test_to_print;
}

void testcpp::setTest_to_print(const QString &value)
{
    test_to_print = value;
}

void testcpp::PrintViaPython(QString &test_to_print)
{



}

Python file: Python档案:

# -*- coding: utf-8 -*-

try:
    from PySide import QtWidgets
except:
    from PyQt5 import QtWidgets


class test_python:
    def __init__(self):


    def testme(self, string_to_print):
        print(string_to_print)

Any IDE can create any type of file, that does not imply that it can execute, and that is the case of QtCreator. 任何IDE都可以创建任何类型的文件,这并不意味着它可以执行,而QtCreator就是这种情况。

Answering the background of the problem I recommend using the PythonQt library, you could do it from scratch through SIP and the python.h library but it is unnecessary if there is a library I suggest. 回答问题的背景(我建议使用PythonQt库),您可以从头开始通过SIP和python.h库完成,但是如果我建议使用一个库,则不需要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM