Skip to content

Commit 066fa9c

Browse files
committed
adapted examples to Qt5
removed non-working and old factory example
1 parent 517fbfa commit 066fa9c

File tree

18 files changed

+39
-108
lines changed

18 files changed

+39
-108
lines changed

examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <PythonQt.h>
22
#include <QtGui>
3+
#include <QApplication>
34

45
int main (int argc, char* argv[]) {
56
QApplication app(argc, argv);

examples/CPPPyWrapperExample/CPPPyWrapperExample.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ DESTDIR = ../../lib
99
include ( ../../build/common.prf )
1010
include ( ../../build/PythonQt.prf )
1111

12+
contains(QT_MAJOR_VERSION, 5) {
13+
QT += widgets
14+
}
15+
1216
SOURCES += \
1317
CPPPyWrapperExample.cpp
1418

examples/PyCPPWrapperExample/CustomObjects.h

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CustomObjectWrapper : public QObject {
6464

6565
Q_OBJECT
6666

67-
public slots:
67+
public Q_SLOTS:
6868
// add a constructor
6969
CustomObject* new_CustomObject(const QString& first, const QString& last) { return new CustomObject(first, last); }
7070

@@ -82,68 +82,4 @@ public slots:
8282

8383
};
8484

85-
86-
//------------------------------------------------------------------------------------------------
87-
// alternative: we create a wrapper factory, which creates a wrapper object for each CPP instance:
88-
//------------------------------------------------------------------------------------------------
89-
90-
// declare our own custom object
91-
class CustomObject2 {
92-
public:
93-
CustomObject2() {}
94-
CustomObject2(const QString& first, const QString& last) { _firstName = first; _lastName = last; }
95-
96-
QString _firstName;
97-
QString _lastName;
98-
99-
};
100-
101-
102-
// add a decorator that allows to access the CustomObject from PythonQt
103-
class CustomObject2Wrapper : public QObject {
104-
105-
Q_OBJECT
106-
107-
public:
108-
CustomObject2Wrapper(CustomObject2* obj) { _ptr = obj; }
109-
110-
public slots:
111-
// add access methods
112-
QString firstName() { return _ptr->_firstName; }
113-
114-
QString lastName() { return _ptr->_lastName; }
115-
116-
void setFirstName(const QString& name) { _ptr->_firstName = name; }
117-
118-
void setLastName(const QString& name) { _ptr->_lastName = name; }
119-
120-
private:
121-
CustomObject2* _ptr;
122-
};
123-
124-
// additional constructor/destructor for CustomObject2 (optional)
125-
class CustomObject2Constructor : public QObject {
126-
127-
Q_OBJECT
128-
129-
public slots:
130-
// add a constructor
131-
CustomObject2* new_CustomObject2(const QString& first, const QString& last) { return new CustomObject2(first, last); }
132-
133-
// add a destructor
134-
void delete_CustomObject2(CustomObject2* o) { delete o; }
135-
};
136-
137-
// a factory that can create wrappers for CustomObject2
138-
class CustomFactory : public PythonQtCppWrapperFactory
139-
{
140-
public:
141-
virtual QObject* create(const QByteArray& name, void *ptr) {
142-
if (name == "CustomObject2") {
143-
return new CustomObject2Wrapper((CustomObject2*)ptr);
144-
}
145-
return NULL;
146-
}
147-
};
148-
14985
#endif

examples/PyCPPWrapperExample/PyCPPWrapperExample.pro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ DESTDIR = ../../lib
1212
include ( ../../build/common.prf )
1313
include ( ../../build/PythonQt.prf )
1414

15+
contains(QT_MAJOR_VERSION, 5) {
16+
QT += widgets
17+
}
18+
1519
HEADERS += \
1620
CustomObjects.h
1721

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from PythonQt.example import *
22

3-
print "alternative 1 : CustomObject wrapped by decorators"
3+
print "CustomObject wrapped by decorators"
44

55
# create a new object
66
custom = CustomObject("John","Doe")
@@ -17,22 +17,3 @@
1717

1818
# get the name
1919
print custom.firstName() + " " + custom.lastName();
20-
21-
print
22-
print "alternative 2 : CustomObject2 wrapped by factory"
23-
24-
# create a new object
25-
custom2 = CustomObject2("John","Doe")
26-
27-
# print the object (to see how it is wrapped)
28-
print custom2
29-
30-
# print the methods available
31-
print dir(custom2)
32-
33-
# set a name
34-
custom2.setFirstName("Mike")
35-
custom2.setLastName("Michels")
36-
37-
# get the name
38-
print custom2.firstName() + " " + custom2.lastName();

examples/PyCPPWrapperExample/main.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,9 @@ int main( int argc, char **argv )
5454
PythonQtObjectPtr mainContext = PythonQt::self()->getMainModule();
5555
PythonQtScriptingConsole console(NULL, mainContext);
5656

57-
// -----------------------------------------------------------------
58-
// Alternative 1: make CustomObject known and use decorators for wrapping:
59-
// -----------------------------------------------------------------
60-
6157
// register the new object as a known classname and add it's wrapper object
6258
PythonQt::self()->registerCPPClass("CustomObject", "","example", PythonQtCreateObject<CustomObjectWrapper>);
6359

64-
// -----------------------------------------------------------------
65-
// Alternative 2: make CustomObject2 known and use a wrapper factory for wrapping:
66-
// -----------------------------------------------------------------
67-
68-
// add a factory that can handle pointers to CustomObject2
69-
PythonQt::self()->addWrapperFactory(new CustomFactory());
70-
71-
// the following is optional and only needed if you want a constructor:
72-
// register the new object as a known classname
73-
PythonQt::self()->registerCPPClass("CustomObject2", "", "example");
74-
// add a constructor for CustomObject2
75-
PythonQt::self()->addClassDecorators(new CustomObject2Constructor());
76-
7760
mainContext.evalFile(":example.py");
7861

7962
console.appendCommandPrompt();

examples/PyCustomMetaTypeExample/CustomObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class CustomObjectWrapper : public QObject {
6565

6666
Q_OBJECT
6767

68-
public slots:
68+
public Q_SLOTS:
6969
// add a constructor
7070
CustomObject* new_CustomObject(const QString& first, const QString& last) { return new CustomObject(first, last); }
7171

examples/PyCustomMetaTypeExample/PyCustomMetaTypeExample.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ DESTDIR = ../../lib
1212
include ( ../../build/common.prf )
1313
include ( ../../build/PythonQt.prf )
1414

15+
contains(QT_MAJOR_VERSION, 5) {
16+
QT += widgets
17+
}
1518

1619
HEADERS += \
1720
CustomObject.h

examples/PyDecoratorsExample/PyDecoratorsExample.pro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ DESTDIR = ../../lib
1212
include ( ../../build/common.prf )
1313
include ( ../../build/PythonQt.prf )
1414

15+
contains(QT_MAJOR_VERSION, 5) {
16+
QT += widgets
17+
}
1518

1619
HEADERS += \
1720
PyExampleDecorators.h

examples/PyDecoratorsExample/PyExampleDecorators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class PyExampleDecorators : public QObject
6565
{
6666
Q_OBJECT
6767

68-
public slots:
68+
public Q_SLOTS:
6969
// add a constructor to QSize variant that takes a QPoint
7070
QSize* new_QSize(const QPoint& p) { return new QSize(p.x(), p.y()); }
7171

0 commit comments

Comments
 (0)