I have a c++ QMainWindow, and wants to develop more window designs qwidget with python for it.
Perhaps somehow expose QMainwindow's add widget function via CPython?
Could someone point to a example or tell me how to do so? Much appreciated!
I have a c++ QMainWindow, and wants to develop more window designs qwidget with python for it.
Perhaps somehow expose QMainwindow's add widget function via CPython?
Could someone point to a example or tell me how to do so? Much appreciated!
Last edited by eue; 19th February 2025 at 10:36.
I think you mean QMainWindow::setCentralWidget(). Since that is not a C++ virtual method, you can't override it in a class derived from QMainWindow. If what you are interested in doing is to use a central widget created in Python, then I think you could probably write a method in your own MainWindow class, call it something like "setMainWidget" and expose it via CPython.Perhaps somehow expose QMainWindow's add widget function via CPython?
I do not know what a QWidget created in Python "looks like" on the C++ side. If there is a way to convert it into a QWidget pointer, then you can pass that pointer into QMainWindow::setCentralWidget().
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
exactly my man.
I would like a c++ pointer to the created python QWidget to be add to my c++ mainwindow.
If you are using PySide, then the PySide QWidget has a method winId() which returns a wId handle to the underlying window system window handle (like an HWnd in native Windows code).
On the C++ side, the static method QWidget::find() takes a wId as argument and returns a QWidget pointer. So this could be the solution for you.
Last edited by d_stranz; 20th February 2025 at 17:56.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
You can embed a Python-based QWidget into a C++ QMainWindow by exposing the QMainWindow's addWidget function using Python bindings. This is typically done with PySide6 or PyQt6.
Steps:
1. Expose C++ functions to Python: Use Shiboken (for PySide6) or SIP (for PyQt6) to create bindings for QMainWindow’s addWidget function.
2. Embed Python in C++: Use CPython API (Py_Initialize(), PyImport_ImportModule(), etc.) to run Python code inside C++.
3. Create Python UI Components: Design QWidget in Python and expose it to C++.
4. Load Python Widgets in C++: Use the exposed functions to add Python-based QWidget dynamically.
Bookmarks