0% found this document useful (0 votes)
237 views

pyside6[801-809]

Uploaded by

chaconyadixa123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views

pyside6[801-809]

Uploaded by

chaconyadixa123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

enums = [

(QtCore.Qt, 'Alignment'),
(QtCore.Qt, 'ApplicationAttribute'),
(QtCore.Qt, 'CheckState'),
(QtCore.Qt, 'CursorShape'),
(QtWidgets.QSizePolicy, 'Policy'),
]

# Look up using the long name (e.g. QtCore.Qt.CheckState.Checked, used


# in PyQt6) and store under the short name (e.g. QtCore.Checked, used
# in PyQt5, PySide2 & accepted by PySide6).
for module, enum_name in enums:
for entry in getattr(module, enum_name):
setattr(module, entry.name, entry)

Alternatively, you can define a custom function to handle the namespace lookup.

def _enum(obj, name):


parent, child = name.split('.')
result = getattr(obj, child, False)
if result: # Found using short name only.
return result

obj = getattr(obj, parent) # Get parent, then child.


return getattr(obj, child)

When passed an object and a PyQt6 compatible long-form name, this function
will return the correct enum or flag on both PyQt6 and PySide6.

>>> _enum(PySide6.QtCore.Qt, 'Alignment.AlignLeft')


PySide6.QtCore.Qt.AlignmentFlag.AlignLeft
>>> _enum(PyQt6.QtCore.Qt, 'Alignment.AlignLeft')
<Alignment.AlignLeft: 1>

If you’re doing this in multiple files it can get a bit cumbersome. A nice solution
to this is to move the import logic and custom shim methods to their own file, e.g.
named qt.py in your project root. This module imports the Qt modules (QtCore,

797
QtGui, QtWidgets, etc.) from one of the two libraries, and then you import into
your application from there.

The contents of the qt.py are the same as we used earlier —

import sys

if 'PyQt6' in sys.modules:
# PyQt6
from PyQt6 import QtGui, QtWidgets, QtCore
from PyQt6.QtCore import pyqtSignal as Signal, pyqtSlot as Slot

else:
# PySide6
from PySide6 import QtGui, QtWidgets, QtCore
from PySide6.QtCore import Signal, Slot

def _enum(obj, name):


parent, child = name.split('.')
result = getattr(obj, child, False)
if result: # Found using short name only.
return result

obj = getattr(obj, parent) # Get parent, then child.


return getattr(obj, child)

You must remember to add any other PyQt6 modules you use (browser,
multimedia, etc.) in both branches of the if block. You can then import Qt6 into
your own application as follows —

from .qt import QtGui, QtWidgets, QtCore, _enum

…and it will work seamlessly across either library.

798
That’s really it
There’s not much more to say — the two libraries really are that similar.
However, if you do stumble across any other PyQt6/PySide6 examples or features
which you can’t easily convert, drop me a note.

799
Appendix D: What next?
This book covers the key things you need to know to start creating GUI
applications with Python. If you’ve made it here you should be well on your way
to create your own apps!

But there is still a lot to discover while you build your applications. To help with
this I post regular tips, tutorials and code snippets on the accompanying website.
Like this book all samples are MIT licensed and free to mix into your own apps.
You may also be interested in joining my Python GUI Academy where I have
video tutorials covering the topics in this book & beyond!

Thanks for reading, and if you have any feedback or suggestions please let me
know!

Get access to updates


If you bought this book direct from me, you will receive automatic updates to this
book. If you bought this book elsewhere, you can email your receipt to me to get
access future updates.

Documentation
Resource

Qt6 Documentation

PyQt6 Library documentation

PySide "Qt for Python" Library documentation

Copyright
This book is ©2022 Martin Fitzpatrick. All code examples in this book are free to
use in your own programming projects without license.

800
45. Thank you
This book continues to be expanded and updated in response to reader feedback.
Thankyou to the following readers for their contributions, which helped make
this edition what it is!

• James Battat

• Alex Bender

• Andries Broekema

• Juan Cabanela

• Max Fritzler

• Olivier Girard

• Richard Hohlfield

• Cody Jackson

• John E Kadwell

• Jeffrey R Kennedy

• Gajendra Khanna

• Bing Xiao Liu

• Alex Lombardi

• Juan Pablo Donayre Quintana

• Rodrigo de Salvo Braz

• Guido Tognan

If you have feedback or suggestions for future editions, please get in touch.

801
Index
A database, 347
Debian, 712
accelerator keys, 112
DecorationRole, 315
accept, 121
dialog box, 125
actions, 108
dialog buttons, 122
arguments, 11
dialogs, 118
B dismissed, 118
basedir, 42 document mode, 92
bitmap graphics, 379 drawLine, 384
bitmap image, 380 drawPoint, 385
bitwise, 653 drawRect, 391
drawRects, 391
C
drawRoundedRect, 391
C++, 5, 774
Cancel, 121 E

checkable, 100, 98 editing strategy, 352


checkbox, 105, 45 event, 12, 180
checked, 21 event filter, 180
class, 96 event handler, 12
click, 12 event loop, 11, 12, 121
clicked, 21 event queue, 12
color, 67 events, 180
columns, 79
F
command line, 11
file, 42
command-line arguments, 658
filter, 357
currentItemChanged, 51
Free Desktop Specification, 243
currentTextChanged, 51
Fugue icons, 102
D Fusion, 227, 236
dark mode, 236

802
G MySQL, 378

GIL, 462
N

H numpy, 340

horizontally, 79
O

I OK, 121

icons, 238, 94
P
installer, 704
palettes, 229
InstallForge, 696
Pandas, 343
intercept events, 180
paths, 630

K PEP8, 5
PNG, 241
key sequence, 114
PostgreSQL, 378
keyboard shortcut, 114
processes, 462
L processEvents, 459
layouts, 66 pseudo-selectors, 282
Linux package, 712 PyInstaller, 667, 696, 712
PyQtGraph, 489, 571
M
pyside6-uic, 224, 224
main window, 14
matplotlib, 593 Q

menubar, 636 QAbstractSlider, 412


menus, 94 QAbstractTableModel, 326
modal, 118 QApplication, 459, 8
Model View, 302 QBrush, 393
mouse movement, 12 QCheckBox, 44
mouseDoubleClickEvent, 180 QColor, 406
mouseMoveEvent, 180, 405 QComboBox, 47, 51
mousePressEvent, 180, 183 QContextMenuEvent, 185
mouseReleaseEvent, 180 QDataWidgetMapper, 372
MVC, 302, 322 QDial, 414, 438, 63

803
QDialogButtonBox, 125 QSS, 246
QDoubleSpinBox, 57 QStackedLayout, 66
QEvent, 180 QStandardItemModel, 322
QFont, 398 QStyle, 240
QGridLayout, 66 Qt Creator, 191, 197, 220
QHBoxLayout, 66 Qt Designer, 191, 196, 219, 243, 66
QIcon, 238 Qt namespace, 104, 114, 184, 37, 647
QLabel, 35, 39 QTableView, 322
QLineEdit, 54 QTabWidget, 90
QListItem, 51 QThreadPool, 476
QListView, 304 QVBoxLayout, 66
QListWidget, 51 QWidget, 414, 65
QMouseEvent, 180
R
QPainter, 380, 412
QPalette, 229 reject, 121

QPen, 389 relative paths, 42

QPixmap, 380, 39 remove a toolbar, 95

QProcess, 560 ribbon, 94

QPushButton, 406 roles, 331

QRC, 216
S
QRect, 391
setSizePolicy, 417
QRectF, 391
signals, 19, 29, 619, 621
QResource, 215
slots, 19, 29, 621
QRunnable, 476
SQL, 347
QSlider, 60
SQLite, 348
QSpinBox, 57
stacked, 79
QSqlQuery, 367
standardIcon, 239
QSqlQueryModel, 365
status tip, 97
QSqlRelation, 362
style sheets, 246
QSqlRelationalDelegate, 363
subclass, 121
QSqlRelationalTableModel, 361
subprocess, 560
QSqlTableModel, 350

804
SVG, 241
sys.argv, 658
system tray, 636

tabbed, 85
threads, 462
timer, 458
toggleable, 100
toggled, 102
toolbar, 103
toolbars, 94
triggered action, 96

Ubuntu, 712

vertically, 79

widget, 94
widgets, 33
window, 10
window decorations, 10
windowTitleChanged, 27

805

You might also like