Skip to content

Commit 9053196

Browse files
committed
use same instead of foo
1 parent d64cb49 commit 9053196

File tree

5 files changed

+11
-19
lines changed

5 files changed

+11
-19
lines changed

qt-5-pyqt5/README.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
# qt application calling a python script accessing the c++ API of the application
2-
3-
- Create a `.so` module.
4-
- Call the Python script from the c++ code and pass a variable per reference.
5-
- The Python script loads the module and modify the variable.
6-
- The c++ code checks that the values has been modified
1+
# Encapsulate the scripter in a class
72

83
~~~.sh
94
$ mkdir build
@@ -14,6 +9,5 @@ $ ./sample
149

1510
## Notes
1611

17-
- On a mac with Qt from homebrew you need:
18-
`cmake .. -GXcode -DQt5Widgets_DIR=/usr/local/Cellar/qt/5.9.1/lib/cmake/Qt5Widgets`
12+
- running twice `run()`, produces a crash.
1913
- If you prefer you can symlink pybind11 directory and replace the `find_package` with `add_subdirectory` in `CMakeLists.txt`

qt-5-pyqt5/python/set-bar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import sampleapi
22

3-
print(foo_copy.bar)
4-
foo_copy.bar = 5
5-
print(foo_copy.bar)
6-
foo_ref.bar = 5
3+
print(Sample.bar)
4+
Sample.bar = 5
75

qt-5-pyqt5/src/mainwindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void MainWindow::scriptRun()
2929
{
3030
QString fileName = QFileDialog::getOpenFileName(this,
3131
// tr("Open Image"), QStandardPaths::standardLocations(QStandardPaths::HomeLocation).last(), tr("Image Files (*.py *.png *.jpg *.bmp)"));
32-
tr("Open Image"), SCRIPTS_DIRECTORY, tr("Image Files (*.py *.png *.jpg *.bmp)"));
32+
tr("Open Script"), SCRIPTS_DIRECTORY, tr("Python Files (*.py)"));
3333
qDebug() << "fileName" << fileName;
3434
std::cout << "filename: " << fileName.toStdString() << std::endl;
3535

qt-5-pyqt5/src/scripter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ using namespace pybind11::literals; // for the ""_a
55

66
void Scripter::runFile(std::string fileName)
77
{
8-
ScripterAPI foo1, foo2;
8+
ScripterAPI api;
99

1010
auto module = py::module::import("sampleapi");
1111

12-
auto locals = py::dict("foo_copy"_a=foo1, "foo_ref"_a=py::cast(foo2, py::return_value_policy::reference)); // foo1 by value, foo2 by reference
12+
auto locals = py::dict("Sample"_a=py::cast(api, py::return_value_policy::reference));
1313

1414
py::eval_file(fileName, py::globals(), locals);
1515

16-
assert(foo1.bar == 1);
17-
assert(foo2.bar == 5);
16+
assert(api.bar == 5);
1817
}

qt-5-pyqt5/src/scripterAPI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#ifndef SCRIPTERAPI_H
22
#define SCRIPTERAPI_H
3-
struct ScripterAPI
3+
class ScripterAPI
44
{
5-
int bar = 1;
5+
public:
6+
int bar = 1;
67
};
78
#endif

0 commit comments

Comments
 (0)