Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to embed python interpreter that can use PySide2 to interact with widgets created in C++?
Forum Updated to NodeBB v4.3 + New Features

How to embed python interpreter that can use PySide2 to interact with widgets created in C++?

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 3 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    veracioux
    wrote on 2 Apr 2022, 13:29 last edited by
    #1

    Hello,
    I am creating a python plugin system for my Qt5 C++ application. I'm using pybind11 to embed the python interpreter into my app. I would like plugins to be able to add custom PySide2 widgets to already existing C++ widgets. While doing that, I have to respect the following requirements:

    • No dependencies other than PySide2 or PyQt5 (prefer the former) on the python side, and no linking to additional libraries besides pybind11 and the libpython library
    • No binding generation using shiboken, though I'm willing to accept shiboken as a dependency if need be
    • No converting parts of my application into a standalone library

    I have managed to create a QApplication in C++ and it is available inside python, so I can add top-level widgets to my application just fine. But I don't know how my python module can interact with widgets created in C++.

    Here's an example application to illustrate what I want:

    • In C++ I create a window, set a vbox layout and add a button to the layout.
    • I import a custom module called mymodule using the embedded interpreter, thereby executing the module
    • mymodule creates a PySide2 QPushButton and adds it to the layout that was created on the C++ side

    C++ code (main.cpp):

    #include <pybind11/embed.h>
    #include <QApplication>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QWidget>
    
    namespace py = pybind11;
    
    int main(int argc, char* argv[]) {
        py::scoped_interpreter guard{};
    
        QApplication app(argc, argv);
        auto window = new QWidget();
        auto cppButton = new QPushButton("C++ button");
    
        auto layout = new QVBoxLayout(window);
        layout->addWidget(cppButton);
    
        window->show();
    
        // Executes mymodule in the current directory
        auto module = py::module_::import("mymodule");
        return app.exec();
    }
    

    Python code in ./mymodule.py:

    from PySide2.QtWidgets import QPushButton
    pythonButton = QPushButton('Python button')
    # How can I obtain a reference to 'layout' that was created in C++, so the following is possible:
    layout.addWidget(pythonButton)
    

    Is what I'm asking even possible?

    1 Reply Last reply
    1
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Apr 2022, 13:40 last edited by
      #2

      Hi,

      I think you will be interested by the scriptable application example in PySide's sources.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • J Offline
        J Offline
        J.K.Who
        wrote on 26 Apr 2022, 06:19 last edited by
        #3

        Struggling in the same matter... Have you solved it?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved