Skip to content

Commit bdb6d86

Browse files
committed
Add support for Qt5
Based on work from the following individual: * Melven Röhrig-Zöllner (https://sourceforge.net/p/pythonqt/discussion/631392/thread/5f20c176/) * Julien Finet (@finetjul): See #38) * Arnaud Barre (@Alzathar):See #15 * Eric Heim (@eric-h):See #36
1 parent 44c9c30 commit bdb6d86

File tree

7 files changed

+303
-23
lines changed

7 files changed

+303
-23
lines changed

CMakeLists.txt

Lines changed: 99 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,33 @@ project(PythonQt)
1414
#----------------------------------------------------------------------------
1515
# Qt version
1616

17+
# Sanity checks
18+
if(DEFINED Qt5_DIR AND DEFINED QT_QMAKE_EXECUTABLE)
19+
message(FATAL_ERROR
20+
"${PROJECT_NAME} shoult NOT be configured setting both Qt5_DIR and QT_QMAKE_EXECUTABLE options.
21+
To build with Qt4, specify QT_QMAKE_EXECUTABLE. To build with Qt5, specify Qt5_DIR.")
22+
endif()
23+
1724
# Set PythonQt_QT_VERSION
18-
set(PythonQt_QT_VERSION 4)
25+
if(DEFINED Qt5_DIR)
26+
message(STATUS "${PROJECT_NAME}: Setting PythonQt_QT_VERSION to 5 because Qt5_DIR is defined.")
27+
set(PythonQt_QT_VERSION 5)
28+
elseif(DEFINED QT_QMAKE_EXECUTABLE)
29+
message(STATUS "${PROJECT_NAME}: Setting PythonQt_QT_VERSION to 4 because QT_QMAKE_EXECUTABLE is defined.")
30+
set(PythonQt_QT_VERSION 4)
31+
else()
32+
set(PythonQt_QT_VERSION 4 CACHE STRING "Pick a version of Qt to use: 4 or 5")
33+
# Set the possible values of Qt version for cmake-gui
34+
set_property(CACHE PythonQt_QT_VERSION PROPERTY STRINGS "4" "5")
35+
endif()
1936

2037
# Requirements
38+
set(minimum_required_qt5_version "5.3.0")
2139
set(minimum_required_qt4_version "4.6.2")
2240
set(minimum_required_qt_version ${minimum_required_qt${PythonQt_QT_VERSION}_version})
2341

2442
# Qt components
43+
set(qt5libs Core Widgets Network OpenGL Sql Svg UiTools WebKitWidgets Xml XmlPatterns)
2544
set(qt4libs core gui network opengl sql svg uitools webkit xml xmlpatterns)
2645
set(qtlibs ${qt${PythonQt_QT_VERSION}libs})
2746

@@ -54,15 +73,32 @@ if(NOT DEFINED PythonQt_INSTALL_INCLUDE_DIR)
5473
set(PythonQt_INSTALL_INCLUDE_DIR include/PythonQt)
5574
endif()
5675

76+
# Since the Qt bindings sources used for both Qt4 and Qt5 are
77+
# grouped using Qt4 naming convention, qt_wrapped_libs variables are the
78+
# same for the two Qt versions.
5779
set(qt4_wrapped_libs ${qt4libs})
80+
set(qt5_wrapped_libs ${qt4libs})
5881
set(qt_wrapped_libs ${qt${PythonQt_QT_VERSION}_wrapped_libs})
5982

83+
set(qt5_wrapped_lib_depends_gui Multimedia)
84+
85+
set(qtlib_to_wraplib_Widgets gui)
86+
set(qtlib_to_wraplib_WebKitWidgets webkit)
87+
6088
# Define PythonQt_Wrap_Qt* options
6189
option(PythonQt_Wrap_QtAll "Make all Qt components available in python" OFF)
6290
foreach(qtlib ${qt_wrapped_libs})
6391
OPTION(PythonQt_Wrap_Qt${qtlib} "Make all of Qt${qtlib} available in python" OFF)
6492
endforeach()
6593

94+
# Set qtlib_to_wraplib_* variables
95+
foreach(qtlib ${qtlibs})
96+
string(TOLOWER ${qtlib} qtlib_lowercase)
97+
if(DEFINED qtlib_to_wraplib_${qtlib})
98+
set(qtlib_lowercase ${qtlib_to_wraplib_${qtlib}})
99+
endif()
100+
set(qtlib_to_wraplib_${qtlib} ${qtlib_lowercase})
101+
endforeach()
66102

67103
# Force option if it applies
68104
if(PythonQt_Wrap_QtAll)
@@ -89,46 +125,86 @@ endif()
89125
#-----------------------------------------------------------------------------
90126
# Setup Qt
91127

128+
if(PythonQt_QT_VERSION VERSION_GREATER "4")
92129

93-
find_package(Qt4)
130+
# Required components
131+
set(qt_required_components Core Widgets)
132+
foreach(qtlib ${qtlibs})
133+
set(qt_wrapped_lib ${qtlib_to_wraplib_${qtlib}})
134+
if(${PythonQt_Wrap_Qt${qt_wrapped_lib}})
135+
list(APPEND qt_required_components ${qtlib} ${qt${PythonQt_QT_VERSION}_wrapped_lib_depends_${qt_wrapped_lib}})
136+
endif()
137+
endforeach()
138+
if(BUILD_TESTING)
139+
list(APPEND qt_required_components Test)
140+
endif()
141+
list(REMOVE_DUPLICATES qt_required_components)
94142

95-
if(QT4_FOUND)
143+
message(STATUS "${PROJECT_NAME}: Required Qt components [${qt_required_components}]")
144+
find_package(Qt5 ${minimum_required_qt_version} COMPONENTS ${qt_required_components} REQUIRED)
96145

97-
set(found_qt_version ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH})
146+
set(QT_LIBRARIES )
147+
foreach(qtlib ${qt_required_components})
148+
include_directories(${Qt5${qtlib}_INCLUDE_DIRS})
149+
add_definitions(${Qt5${qtlib}_DEFINITIONS})
150+
list(APPEND QT_LIBRARIES ${Qt5${qtlib}_LIBRARIES})
151+
endforeach()
98152

99-
if(${found_qt_version} VERSION_LESS ${minimum_required_qt_version})
100-
message(FATAL_ERROR "error: PythonQt requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${found_qt_version}.")
101-
endif()
153+
set(QT_VERSION_MAJOR ${Qt5Core_VERSION_MAJOR})
154+
set(QT_VERSION_MINOR ${Qt5Core_VERSION_MINOR})
155+
156+
macro(pythonqt_wrap_cpp)
157+
qt5_wrap_cpp(${ARGV})
158+
endmacro()
102159

103-
# Enable required qt module
104-
foreach(qtlib ${qt_wrapped_libs})
105-
string(TOUPPER ${qtlib} qtlib_uppercase)
106-
if (NOT ${QT_QT${qtlib_uppercase}_FOUND})
107-
message(FATAL_ERROR "QT_QT${qtlib_uppercase} *not* FOUND - Try to disable PythonQt_Wrap_Qt${qtlib}")
160+
else()
161+
162+
find_package(Qt4)
163+
164+
if(QT4_FOUND)
165+
166+
set(found_qt_version ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}.${QT_VERSION_PATCH})
167+
168+
if(${found_qt_version} VERSION_LESS ${minimum_required_qt_version})
169+
message(FATAL_ERROR "error: PythonQt requires Qt >= ${minimum_required_qt_version} -- you cannot use Qt ${found_qt_version}.")
108170
endif()
109-
set(QT_USE_QT${qtlib_uppercase} ${PythonQt_Wrap_Qt${qtlib}})
110-
endforeach()
111171

112-
set(QT_USE_QTTEST ${BUILD_TESTING})
172+
# Enable required qt module
173+
foreach(qtlib ${qt_wrapped_libs})
174+
string(TOUPPER ${qtlib} qtlib_uppercase)
175+
if (NOT ${QT_QT${qtlib_uppercase}_FOUND})
176+
message(FATAL_ERROR "QT_QT${qtlib_uppercase} *not* FOUND - Try to disable PythonQt_Wrap_Qt${qtlib}")
177+
endif()
178+
set(QT_USE_QT${qtlib_uppercase} ${PythonQt_Wrap_Qt${qtlib}})
179+
endforeach()
113180

114-
include(${QT_USE_FILE})
115-
else()
116-
message(FATAL_ERROR "error: Qt4 was not found on your system. You probably need to set the QT_QMAKE_EXECUTABLE variable")
117-
endif()
181+
# Enable QtTest in Qt4 is the option BUILD_TESTING was activated
182+
set(QT_USE_QTTEST ${BUILD_TESTING})
118183

119-
macro(pythonqt_wrap_cpp)
120-
qt4_wrap_cpp(${ARGV})
121-
endmacro()
184+
include(${QT_USE_FILE})
185+
else()
186+
message(FATAL_ERROR "error: Qt4 was not found on your system. You probably need to set the QT_QMAKE_EXECUTABLE variable")
187+
endif()
188+
189+
macro(pythonqt_wrap_cpp)
190+
qt4_wrap_cpp(${ARGV})
191+
endmacro()
192+
193+
endif()
122194

123195
#-----------------------------------------------------------------------------
124196
# The variable "generated_cpp_suffix" allows to conditionnally compile the generated wrappers
125197
# associated with the Qt version being used.
126198

127199
set(generated_cpp_suffix_46 _47)
200+
set(generated_cpp_suffix_52 _50)
201+
set(generated_cpp_suffix_51 _50)
128202

129203
set(generated_cpp_suffix "_${QT_VERSION_MAJOR}${QT_VERSION_MINOR}")
130204
if(DEFINED generated_cpp_suffix_${QT_VERSION_MAJOR}${QT_VERSION_MINOR})
131205
set(generated_cpp_suffix "${generated_cpp_suffix_${QT_VERSION_MAJOR}${QT_VERSION_MINOR}}")
206+
elseif(${QT_VERSION_MAJOR}.${QT_VERSION_MINOR} VERSION_GREATER "5.4")
207+
set(generated_cpp_suffix "_54")
132208
endif()
133209

134210
#-----------------------------------------------------------------------------
@@ -216,7 +292,7 @@ foreach(qtlib ${qt_wrapped_libs})
216292

217293
set(file_prefix generated_cpp${generated_cpp_suffix}/com_trolltech_qt_${qtlib}/com_trolltech_qt_${qtlib})
218294

219-
foreach(index RANGE 0 11)
295+
foreach(index RANGE 0 12)
220296

221297
# Source files
222298
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file_prefix}${index}.cpp)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include "PythonQt_QtBindings.h"
3+
4+
#include "PythonQt.h"
5+
6+
void PythonQt_init_QtGui(PyObject*);
7+
void PythonQt_init_QtSvg(PyObject*);
8+
void PythonQt_init_QtSql(PyObject*);
9+
void PythonQt_init_QtNetwork(PyObject*);
10+
void PythonQt_init_QtCore(PyObject*);
11+
void PythonQt_init_QtWebKit(PyObject*);
12+
void PythonQt_init_QtOpenGL(PyObject*);
13+
void PythonQt_init_QtXml(PyObject*);
14+
void PythonQt_init_QtXmlPatterns(PyObject*);
15+
void PythonQt_init_QtUiTools(PyObject*);
16+
17+
PYTHONQT_EXPORT void PythonQt_init_QtBindings()
18+
{
19+
#ifdef PYTHONQT_WRAP_Qtcore
20+
PythonQt_init_QtCore(0);
21+
#endif
22+
23+
#ifdef PYTHONQT_WRAP_Qtgui
24+
PythonQt_init_QtGui(0);
25+
#endif
26+
27+
#ifdef PYTHONQT_WRAP_Qtnetwork
28+
PythonQt_init_QtNetwork(0);
29+
#endif
30+
31+
#ifdef PYTHONQT_WRAP_Qtopengl
32+
PythonQt_init_QtOpenGL(0);
33+
#endif
34+
35+
#ifdef PYTHONQT_WRAP_Qtsql
36+
PythonQt_init_QtSql(0);
37+
#endif
38+
39+
#ifdef PYTHONQT_WRAP_Qtsvg
40+
PythonQt_init_QtSvg(0);
41+
#endif
42+
43+
#ifdef PYTHONQT_WRAP_Qtuitools
44+
PythonQt_init_QtUiTools(0);
45+
#endif
46+
47+
#ifdef PYTHONQT_WRAP_Qtwebkit
48+
PythonQt_init_QtWebKit(0);
49+
#endif
50+
51+
#ifdef PYTHONQT_WRAP_Qtxml
52+
PythonQt_init_QtXml(0);
53+
#endif
54+
55+
#ifdef PYTHONQT_WRAP_Qtxmlpatterns
56+
PythonQt_init_QtXmlPatterns(0);
57+
#endif
58+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _PYTHONQT_QTBINDINGS_H
2+
#define _PYTHONQT_QTBINDINGS_H
3+
4+
#include "PythonQtSystem.h"
5+
6+
/// Initialize Qt bindings enabled at configuration time
7+
PYTHONQT_EXPORT void PythonQt_init_QtBindings();
8+
9+
#endif
10+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include "PythonQt_QtBindings.h"
3+
4+
#include "PythonQt.h"
5+
6+
void PythonQt_init_QtGui(PyObject*);
7+
void PythonQt_init_QtSvg(PyObject*);
8+
void PythonQt_init_QtSql(PyObject*);
9+
void PythonQt_init_QtNetwork(PyObject*);
10+
void PythonQt_init_QtCore(PyObject*);
11+
void PythonQt_init_QtWebKit(PyObject*);
12+
void PythonQt_init_QtOpenGL(PyObject*);
13+
void PythonQt_init_QtXml(PyObject*);
14+
void PythonQt_init_QtXmlPatterns(PyObject*);
15+
void PythonQt_init_QtUiTools(PyObject*);
16+
17+
PYTHONQT_EXPORT void PythonQt_init_QtBindings()
18+
{
19+
#ifdef PYTHONQT_WRAP_Qtcore
20+
PythonQt_init_QtCore(0);
21+
#endif
22+
23+
#ifdef PYTHONQT_WRAP_Qtgui
24+
PythonQt_init_QtGui(0);
25+
#endif
26+
27+
#ifdef PYTHONQT_WRAP_Qtnetwork
28+
PythonQt_init_QtNetwork(0);
29+
#endif
30+
31+
#ifdef PYTHONQT_WRAP_Qtopengl
32+
PythonQt_init_QtOpenGL(0);
33+
#endif
34+
35+
#ifdef PYTHONQT_WRAP_Qtsql
36+
PythonQt_init_QtSql(0);
37+
#endif
38+
39+
#ifdef PYTHONQT_WRAP_Qtsvg
40+
PythonQt_init_QtSvg(0);
41+
#endif
42+
43+
#ifdef PYTHONQT_WRAP_Qtuitools
44+
PythonQt_init_QtUiTools(0);
45+
#endif
46+
47+
#ifdef PYTHONQT_WRAP_Qtwebkit
48+
PythonQt_init_QtWebKit(0);
49+
#endif
50+
51+
#ifdef PYTHONQT_WRAP_Qtxml
52+
PythonQt_init_QtXml(0);
53+
#endif
54+
55+
#ifdef PYTHONQT_WRAP_Qtxmlpatterns
56+
PythonQt_init_QtXmlPatterns(0);
57+
#endif
58+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _PYTHONQT_QTBINDINGS_H
2+
#define _PYTHONQT_QTBINDINGS_H
3+
4+
#include "PythonQtSystem.h"
5+
6+
/// Initialize Qt bindings enabled at configuration time
7+
PYTHONQT_EXPORT void PythonQt_init_QtBindings();
8+
9+
#endif
10+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
#include "PythonQt_QtBindings.h"
3+
4+
#include "PythonQt.h"
5+
6+
void PythonQt_init_QtGui(PyObject*);
7+
void PythonQt_init_QtSvg(PyObject*);
8+
void PythonQt_init_QtSql(PyObject*);
9+
void PythonQt_init_QtNetwork(PyObject*);
10+
void PythonQt_init_QtCore(PyObject*);
11+
void PythonQt_init_QtWebKit(PyObject*);
12+
void PythonQt_init_QtOpenGL(PyObject*);
13+
void PythonQt_init_QtXml(PyObject*);
14+
void PythonQt_init_QtXmlPatterns(PyObject*);
15+
void PythonQt_init_QtUiTools(PyObject*);
16+
17+
PYTHONQT_EXPORT void PythonQt_init_QtBindings()
18+
{
19+
#ifdef PYTHONQT_WRAP_Qtcore
20+
PythonQt_init_QtCore(0);
21+
#endif
22+
23+
#ifdef PYTHONQT_WRAP_Qtgui
24+
PythonQt_init_QtGui(0);
25+
#endif
26+
27+
#ifdef PYTHONQT_WRAP_Qtnetwork
28+
PythonQt_init_QtNetwork(0);
29+
#endif
30+
31+
#ifdef PYTHONQT_WRAP_Qtopengl
32+
PythonQt_init_QtOpenGL(0);
33+
#endif
34+
35+
#ifdef PYTHONQT_WRAP_Qtsql
36+
PythonQt_init_QtSql(0);
37+
#endif
38+
39+
#ifdef PYTHONQT_WRAP_Qtsvg
40+
PythonQt_init_QtSvg(0);
41+
#endif
42+
43+
#ifdef PYTHONQT_WRAP_Qtuitools
44+
PythonQt_init_QtUiTools(0);
45+
#endif
46+
47+
#ifdef PYTHONQT_WRAP_Qtwebkit
48+
PythonQt_init_QtWebKit(0);
49+
#endif
50+
51+
#ifdef PYTHONQT_WRAP_Qtxml
52+
PythonQt_init_QtXml(0);
53+
#endif
54+
55+
#ifdef PYTHONQT_WRAP_Qtxmlpatterns
56+
PythonQt_init_QtXmlPatterns(0);
57+
#endif
58+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef _PYTHONQT_QTBINDINGS_H
2+
#define _PYTHONQT_QTBINDINGS_H
3+
4+
#include "PythonQtSystem.h"
5+
6+
/// Initialize Qt bindings enabled at configuration time
7+
PYTHONQT_EXPORT void PythonQt_init_QtBindings();
8+
9+
#endif
10+

0 commit comments

Comments
 (0)