Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QApplication>
- #include <QWidget>
- #include <QVBoxLayout>
- #include <QPushButton>
- #include <QMainWindow>
- #pragma push_macro("slots")
- #undef slots
- #include "Python.h"
- #pragma pop_macro("slots")
- int main(int argc, char *argv[])
- {
- PyObject *pName, *pModule, *pDict, *pFunc;
- // Initialize QApplication and setup basic widgets.
- QApplication app (argc, argv);
- QMainWindow mainWindow (nullptr);
- QPushButton button ("test", &mainWindow);
- QWidget * widget = new QWidget(&mainWindow);
- mainWindow.setCentralWidget(widget);
- auto l = new QVBoxLayout ();
- widget->setLayout(l);
- l->addWidget(&button);
- // Initialize the Python Interpreter
- Py_Initialize();
- // Build the name object
- pName = PyUnicode_FromString("runPyQt");
- // Load the module object
- pModule = PyImport_Import(pName);
- if (!pModule){
- Py_Finalize();
- return 1;
- }
- pDict = PyModule_GetDict(pModule);
- pFunc = PyDict_GetItemString(pDict, "main");
- if (PyCallable_Check(pFunc))
- {
- PyObject_CallObject(pFunc, nullptr);
- } else
- {
- PyErr_Print();
- }
- mainWindow.show();
- int r = 0;
- r = QApplication::exec();
- // Clean up
- Py_DECREF(pModule);
- Py_DECREF(pName);
- // Finish the Python Interpreter
- Py_Finalize();
- return r;
- }
- import sys
- if sys.platform.startswith( 'linux' ) :
- from OpenGL import GL
- from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow
- def main():
- app = QApplication.instance()
- widgets = app.topLevelWidgets()
- for widget in widgets:
- if type(widget) is QMainWindow:
- break
- widget = widget.centralWidget()
- widget.layout().addWidget(QPushButton('Hello from PyQt'))
Add Comment
Please, Sign In to add comment