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 !
Development Select File Interface to python call . Here is a global method to open the dialog box
#include <QFileDialog>
using namespace std;
static PyObject *pModule = 0;
// Returns the selected file path
PyObject* OpenDialog(PyObject *self)
{
QString fileName = "";
fileName = QFileDialog::getOpenFileName();
if (fileName.isEmpty())
return PyUnicode_FromString("");
return PyUnicode_FromString(fileName.toStdString().c_str());
}
double-click PyPlayer.ui
file , open Qt The designer , Add a button :
Then add a slot function for this control
stay C++ Of PyPlayer.h Add the definition of slot function in :
#pragma once
#include <QtWidgets/QWidget>
#include "ui_PyPlayer.h"
class PyPlayer : public QWidget
{
Q_OBJECT
public:
PyPlayer(QWidget *parent = Q_NULLPTR);
public slots:
void Open();
private:
Ui::PyPlayerClass ui;
};
Its implementation is to call python Medium opoen function
void PyPlayer::Open() {
cout << "PyPlayer::Open()" << endl;
// call Python Of open function
}
Run it :
You can see that the control binding is successful :
stay pyqt.py Add open Definition of function :
def open():
print("Python open")
filename = OpenDialog()
print(filename)
C++ End pass PyPlayer::Open() Call the above python Of open function :
void PyPlayer::Open() {
cout << "PyPlayer::Open()" << endl;
// call Python Of open function
if (!pModule) return;
PyObject *open = PyObject_GetAttrString(pModule, "open");
if (!open || !PyCallable_Check(open))
{
PyErr_Print();
return;
}
PyObject_CallObject(open, 0);
}
So how to be in python
How to call C++ Write global functions opendialog Well ?
Can be opened in a constructor :
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 the interface for selecting files to python
static PyMethodDef meths[] = {
{
"OpenDialog", (PyCFunction)OpenDialog, METH_NOARGS, 0}
,{
NULL}
};
int re = PyModule_AddFunctions(pModule, meths);
if (!re)
{
PyErr_Print();
}
// Open thread call python Of main function
}
In this case , Run to find , We can print the path of the selected file by clicking the open button
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 !