FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cefpython/examples/qt.py at cefpython55 · packetlost/cefpython · GitHub
packetlost
cefpython
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cefpython
/
examples
/
qt.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
337 lines (281 loc) · 11 KB
Breadcrumbs
cefpython
/
examples
/
qt.py
Copy path
File metadata and controls
337 lines (281 loc) · 11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# Example of embedding CEF Python browser using PyQt/PySide libraries.
# This example has two widgets: a navigation bar and a browser.
#
# Tested with PyQt 4.10.4 (4.8.6), PySide 1.2.1 (4.8.6)
# and CEF Python v55.3+, only on Linux.
import
os
import
sys
import
platform
from
cefpython3
import
cefpython
as
cef
# PyQt imports
if
"pyqt"
in
sys
.
argv
:
from
PyQt4
.
QtGui
import
*
from
PyQt4
.
QtCore
import
*
# PySide imports
elif
"pyside"
in
sys
.
argv
:
import
PySide
from
PySide
import
QtCore
# noinspection PyUnresolvedReferences
from
PySide
.
QtGui
import
*
# noinspection PyUnresolvedReferences
from
PySide
.
QtCore
import
*
else
:
print
(
"USAGE:"
)
print
(
" qt.py pyqt"
)
print
(
" qt.py pyside"
)
sys
.
exit
(
1
)
# Constants
LINUX
=
(
platform
.
system
()
==
"Linux"
)
WINDOWS
=
(
platform
.
system
()
==
"Windows"
)
WIDTH
=
800
HEIGHT
=
600
# OS differences
CefWidgetParent
=
QWidget
if
LINUX
:
# noinspection PyUnresolvedReferences
CefWidgetParent
=
QX11EmbedContainer
def
main
():
check_versions
()
sys
.
excepthook
=
cef
.
ExceptHook
# To shutdown all CEF processes on error
cef
.
Initialize
()
app
=
CefApplication
(
sys
.
argv
)
main_window
=
MainWindow
()
main_window
.
show
()
app
.
exec_
()
app
.
stopTimer
()
del
main_window
# Just to be safe, see below
del
app
# Must destroy before calling Shutdown
cef
.
Shutdown
()
def
check_versions
():
print
(
"[qt.py] CEF Python {ver}"
.
format
(
ver
=
cef
.
__version__
))
print
(
"[qt.py] Python {ver}"
.
format
(
ver
=
sys
.
version
[:
6
]))
# PyQt version
if
"pyqt"
in
sys
.
argv
:
print
(
"[qt.py] PyQt {v1} ({v2})"
.
format
(
v1
=
PYQT_VERSION_STR
,
v2
=
qVersion
()))
# PySide version
elif
"pyside"
in
sys
.
argv
:
print
(
"[qt.py] PySide {v1} ({v2})"
.
format
(
v1
=
PySide
.
__version__
,
v2
=
QtCore
.
__version__
))
# CEF Python version requirement
assert
cef
.
__version__
>=
"55.3"
,
"CEF Python v55.3+ required to run this"
class
MainWindow
(
QMainWindow
):
def
__init__
(
self
):
super
(
MainWindow
,
self
).
__init__
(
None
)
self
.
cef_widget
=
None
self
.
navigation_bar
=
None
if
"pyqt"
in
sys
.
argv
:
self
.
setWindowTitle
(
"PyQt example"
)
elif
"pyside"
in
sys
.
argv
:
self
.
setWindowTitle
(
"PySide example"
)
self
.
setFocusPolicy
(
Qt
.
StrongFocus
)
self
.
setupLayout
()
def
setupLayout
(
self
):
self
.
resize
(
WIDTH
,
HEIGHT
)
self
.
cef_widget
=
CefWidget
(
self
)
self
.
navigation_bar
=
NavigationBar
(
self
.
cef_widget
)
layout
=
QGridLayout
()
layout
.
addWidget
(
self
.
navigation_bar
,
0
,
0
)
layout
.
addWidget
(
self
.
cef_widget
,
1
,
0
)
layout
.
setContentsMargins
(
0
,
0
,
0
,
0
)
layout
.
setSpacing
(
0
)
frame
=
QFrame
()
frame
.
setLayout
(
layout
)
self
.
setCentralWidget
(
frame
)
# Browser can be embedded only after layout was set up
self
.
cef_widget
.
embedBrowser
()
def
focusInEvent
(
self
,
event
):
# This event seems to never get called, as CEF is stealing all
# focus due to Issue #284.
if
WINDOWS
:
# noinspection PyUnresolvedReferences
cef
.
WindowUtils
.
OnSetFocus
(
int
(
self
.
centralWidget
().
winId
()),
0
,
0
,
0
)
print
(
"[qt.py] focusInEvent"
)
if
self
.
cef_widget
.
browser
:
self
.
cef_widget
.
browser
.
SetFocus
(
True
)
def
focusOutEvent
(
self
,
event
):
# This event seems to never get called, as CEF is stealing all
# focus due to Issue #284.
print
(
"[qt.py] focusOutEvent"
)
def
closeEvent
(
self
,
event
):
# Close browser (force=True) and free CEF reference
if
self
.
cef_widget
.
browser
:
self
.
cef_widget
.
browser
.
CloseBrowser
(
True
)
self
.
cef_widget
.
browser
=
None
# free ref
class
NavigationBar
(
QFrame
):
def
__init__
(
self
,
cef_widget
):
super
(
NavigationBar
,
self
).
__init__
()
self
.
cef_widget
=
cef_widget
# Init layout
layout
=
QGridLayout
()
layout
.
setContentsMargins
(
0
,
0
,
0
,
0
)
layout
.
setSpacing
(
0
)
# Back button
self
.
back
=
self
.
createButton
(
"back"
)
# noinspection PyUnresolvedReferences
self
.
back
.
clicked
.
connect
(
self
.
onBack
)
layout
.
addWidget
(
self
.
back
,
0
,
0
)
# Forward button
self
.
forward
=
self
.
createButton
(
"forward"
)
# noinspection PyUnresolvedReferences
self
.
forward
.
clicked
.
connect
(
self
.
onForward
)
layout
.
addWidget
(
self
.
forward
,
0
,
1
)
# Reload button
self
.
reload
=
self
.
createButton
(
"reload"
)
# noinspection PyUnresolvedReferences
self
.
reload
.
clicked
.
connect
(
self
.
onReload
)
layout
.
addWidget
(
self
.
reload
,
0
,
2
)
# Url input
self
.
url
=
QLineEdit
(
""
)
# noinspection PyUnresolvedReferences
self
.
url
.
returnPressed
.
connect
(
self
.
onGoUrl
)
layout
.
addWidget
(
self
.
url
,
0
,
3
)
# Layout
self
.
setLayout
(
layout
)
self
.
updateState
()
def
onBack
(
self
):
if
self
.
cef_widget
.
browser
:
self
.
cef_widget
.
browser
.
GoBack
()
def
onForward
(
self
):
if
self
.
cef_widget
.
browser
:
self
.
cef_widget
.
browser
.
GoForward
()
def
onReload
(
self
):
if
self
.
cef_widget
.
browser
:
self
.
cef_widget
.
browser
.
Reload
()
def
onGoUrl
(
self
):
if
self
.
cef_widget
.
browser
:
self
.
cef_widget
.
browser
.
LoadUrl
(
self
.
url
.
text
())
def
updateState
(
self
):
browser
=
self
.
cef_widget
.
browser
if
not
browser
:
self
.
back
.
setEnabled
(
False
)
self
.
forward
.
setEnabled
(
False
)
self
.
reload
.
setEnabled
(
False
)
self
.
url
.
setEnabled
(
False
)
return
self
.
back
.
setEnabled
(
browser
.
CanGoBack
())
self
.
forward
.
setEnabled
(
browser
.
CanGoForward
())
self
.
reload
.
setEnabled
(
True
)
self
.
url
.
setEnabled
(
True
)
self
.
url
.
setText
(
browser
.
GetUrl
())
def
createButton
(
self
,
name
):
resources
=
os
.
path
.
join
(
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
)),
"resources"
)
pixmap
=
QPixmap
(
os
.
path
.
join
(
resources
,
"{0}.png"
.
format
(
name
)))
icon
=
QIcon
(
pixmap
)
button
=
QPushButton
()
button
.
setIcon
(
icon
)
button
.
setIconSize
(
pixmap
.
rect
().
size
())
return
button
class
CefWidget
(
CefWidgetParent
):
def
__init__
(
self
,
parent
=
None
):
super
(
CefWidget
,
self
).
__init__
(
parent
)
self
.
parent
=
parent
self
.
browser
=
None
self
.
show
()
def
embedBrowser
(
self
):
self
.
width
=
0
self
.
height
=
0
window_info
=
cef
.
WindowInfo
()
window_info
.
SetAsChild
(
int
(
self
.
winId
()))
self
.
browser
=
cef
.
CreateBrowserSync
(
window_info
,
url
=
"https://www.google.com/"
)
self
.
browser
.
SetClientHandler
(
LoadHandler
(
self
.
parent
.
navigation_bar
))
self
.
browser
.
SetClientHandler
(
FocusHandler
())
def
moveEvent
(
self
,
_
):
# pos = event.pos()
# self.x = pos.x()
# self.y = pos.y()
self
.
x
=
0
self
.
y
=
0
if
self
.
browser
:
if
WINDOWS
:
# noinspection PyUnresolvedReferences
cef
.
WindowUtils
.
OnSize
(
int
(
self
.
winId
()),
0
,
0
,
0
)
elif
LINUX
:
# noinspection PyUnresolvedReferences
self
.
browser
.
SetBounds
(
self
.
x
,
self
.
y
,
self
.
width
,
self
.
height
)
def
resizeEvent
(
self
,
event
):
size
=
event
.
size
()
self
.
width
=
size
.
width
()
self
.
height
=
size
.
height
()
if
self
.
browser
:
if
WINDOWS
:
# noinspection PyUnresolvedReferences
cef
.
WindowUtils
.
OnSize
(
int
(
self
.
winId
()),
0
,
0
,
0
)
elif
LINUX
:
# noinspection PyUnresolvedReferences
self
.
browser
.
SetBounds
(
self
.
x
,
self
.
y
,
self
.
width
,
self
.
height
)
class
CefApplication
(
QApplication
):
def
__init__
(
self
,
args
):
super
(
CefApplication
,
self
).
__init__
(
args
)
self
.
timer
=
self
.
createTimer
()
self
.
setupIcon
()
def
createTimer
(
self
):
timer
=
QTimer
()
# noinspection PyUnresolvedReferences
timer
.
timeout
.
connect
(
self
.
onTimer
)
timer
.
start
(
10
)
return
timer
def
onTimer
(
self
):
# For best performance, a proper way of doing message loop should
# probably be:
# 1. In createTimer() call self.timer.start(0)
# 2. In onTimer() call MessageLoopWork() only when
# QtGui.QApplication.instance()->hasPendingEvents() returns False.
# But... there is a bug in Qt, hasPendingEvents() always returns true.
# TODO: The bug above was noticed in Qt 4.8 on Windows. Other versions
# and/or other OSes may not be affected, so check it.
cef
.
MessageLoopWork
()
def
stopTimer
(
self
):
# Stop the timer after Qt's message loop has ended
self
.
timer
.
stop
()
def
setupIcon
(
self
):
icon_file
=
os
.
path
.
join
(
os
.
path
.
abspath
(
os
.
path
.
dirname
(
__file__
)),
"resources"
,
"{0}.png"
.
format
(
sys
.
argv
[
1
]))
if
os
.
path
.
exists
(
icon_file
):
self
.
setWindowIcon
(
QIcon
(
icon_file
))
class
LoadHandler
(
object
):
def
__init__
(
self
,
navigation_bar
):
self
.
initial_app_loading
=
True
self
.
navigation_bar
=
navigation_bar
def
OnLoadingStateChange
(
self
,
**
_
):
self
.
navigation_bar
.
updateState
()
def
OnLoadStart
(
self
,
browser
,
**
_
):
self
.
navigation_bar
.
url
.
setText
(
browser
.
GetUrl
())
if
self
.
initial_app_loading
:
# Temporary fix no. 2 for focus issue during initial loading
# on Linux (Issue #284). If this is not applied then
# sometimes during initial loading, keyboard focus may
# break and it is not possible to type anything, even
# though a type cursor blinks in web view.
print
(
"[qt.py] LoadHandler.OnLoadStart:"
" keyboard focus fix no. 2 (#284)"
)
browser
.
SetFocus
(
True
)
self
.
initial_app_loading
=
False
class
FocusHandler
(
object
):
def
__init__
(
self
):
pass
def
OnTakeFocus
(
self
,
**
kwargs
):
# print("[qt.py] FocusHandler.OnTakeFocus, next={next}"
# .format(next=kwargs["next_component"]]))
pass
def
OnSetFocus
(
self
,
**
kwargs
):
# source_enum = {cef.FOCUS_SOURCE_NAVIGATION: "navigation",
# cef.FOCUS_SOURCE_SYSTEM: "system"}
# print("[qt.py] FocusHandler.OnSetFocus, source={source}"
# .format(source=source_enum[kwargs["source"]]))
# return False
pass
def
OnGotFocus
(
self
,
browser
,
**
_
):
# Temporary fix no. 1 for focus issues on Linux (Issue #284).
# If this is not applied then when switching to another
# window (alt+tab) and then back to this example, keyboard
# focus becomes broken, you can't type anything, even
# though a type cursor blinks in web view.
print
(
"[qt.py] FocusHandler.OnGotFocus:"
" keyboard focus fix no. 1 (#284)"
)
browser
.
SetFocus
(
True
)
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL