author : Empty bad uncle
Blog :https://xuhss.com
The breakfast shop won't be open till night , The people who want to eat have already come !
python Medium pyqt.py
Module definition configuration item
print("Python PyPlayer")
conf = {
"width" : 1280,
"height" : 720,
"title" : "PyPlaye player "
}
# The main function Called in subthread , Thread is c++ establish
def main():
print("Python main")
To prevent coding problems , use vscode Open editor
stay C++ Code PyPlyer.cpp
In order to get python Configuration item
#include <Python.h>
#include "PyPlayer.h"
#include <iostream>
using namespace std;
static PyObject *pModule = 0;
PyPlayer::PyPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
Py_SetPythonHome(L"./");
Py_Initialize();
// Loading module
pModule = PyImport_ImportModule("pyqt");
if (!pModule)
{
printf("PyImport import error");
PyErr_Print();
return;
}
// obtain python Configuration item Change the size and title of the window
PyObject *conf = PyObject_GetAttrString(pModule, "conf");
if (!conf)
{
cout << "Please set the conf" << endl;
PyErr_Print();
return;
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
if (width > 0 && height > 0)
{
resize(width, height);
}
Py_XDECREF(conf);
// Open thread call python Of main function
}
function :
You can see that the window size has been successfully modified
c++
End add python
Title Reading :
#include <Python.h>
#include "PyPlayer.h"
#include <iostream>
using namespace std;
static PyObject *pModule = 0;
PyPlayer::PyPlayer(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
Py_SetPythonHome(L"./");
Py_Initialize();
// Loading module
pModule = PyImport_ImportModule("pyqt");
if (!pModule)
{
printf("PyImport import error");
PyErr_Print();
return;
}
// obtain python Configuration item Change the size and title of the window
PyObject *conf = PyObject_GetAttrString(pModule, "conf");
if (!conf)
{
cout << "Please set the conf" << endl;
PyErr_Print();
return;
}
PyObject *key = PyUnicode_FromString("width");
int width = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
key = PyUnicode_FromString("height");
int height = PyLong_AsLong(PyDict_GetItem(conf, key));
Py_XDECREF(key);
// Change window title
key = PyUnicode_FromString("title");
wchar_t title[1024] = { 0 };
PyUnicode_AsWideChar(PyDict_GetItem(conf, key), title, 1023);
this->setWindowTitle(QString::fromUtf16((char16_t*)title));
Py_XDECREF(key);
if (width > 0 && height > 0)
{
resize(width, height);
}
Py_XDECREF(conf);
// Open thread call python Of main function
}
You can see that the changes have been made successfully qt Dialog box
The title of the
give the thumbs-up
Collection
forward
A wave , Bloggers also support making exclusive dynamic wallpapers for iron fans ~Follow the card below to get more programming knowledge immediately , Including various language learning materials , Thousands of sets PPT Templates and various game source materials and so on . More information can be viewed by yourself !