1

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.

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; although I am not sure how do I call now the function in the python file, which simply print a string.

I want to call the Qt function, and have it call the function in the python file; how do you do that?

testcpp.h file:

#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:

#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:

# -*- 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)
5
  • 1
    read this: pythonqt.sourceforge.net Commented Jul 21, 2017 at 21:40
  • Spot on; so I need a library to use python. It is interesting that when I create a new file, python is one of the choices; although not sure why, since I need an external library to use a python file. Thanks for the hint! Commented Jul 21, 2017 at 21:43
  • I think you mean Qt Creator, that you can create a file in an IDE does not imply that you can run it in your application, that library does what you want. Commented Jul 21, 2017 at 21:45
  • Thanks a lot; indeed the IDE is QT Creator. I did assume wrongly, at this point, that if the IDE create a file type, it also support it :) If you add it as response I can mark it as solution. Commented Jul 25, 2017 at 17:03
  • I already replied, please mark it as correct. Commented Jul 25, 2017 at 17:21

1 Answer 1

1

Any IDE can create any type of file, that does not imply that it can execute, and that is the case of 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.

Sign up to request clarification or add additional context in comments.

2 Comments

BTW is there any gotcha to build PythonQt? I am getting a ton of compile errors on OSX 10.11, in the __locale when running build all.
I have compiled it in linux, not in other OS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.