-
Notifications
You must be signed in to change notification settings - Fork 253
/
baseinstance1.py
56 lines (42 loc) · 1.62 KB
/
baseinstance1.py
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
import sys
import os
# Set preferred binding
os.environ['QT_PREFERRED_BINDING'] = os.pathsep.join(['PySide', 'PyQt4', 'PySide6'])
from Qt import QtWidgets, QtCompat
def setup_ui(uifile, base_instance=None):
"""Load a Qt Designer .ui file and returns an instance of the user interface
Args:
uifile (str): Absolute path to .ui file
base_instance (QWidget): The widget into which UI widgets are loaded
Returns:
QWidget: the base instance
"""
ui = QtCompat.loadUi(uifile) # Qt.py mapped function
if not base_instance:
return ui
else:
for member in dir(ui):
if not member.startswith('__') and \
member is not 'staticMetaObject':
setattr(base_instance, member, getattr(ui, member))
return ui
class MainWindow(QtWidgets.QWidget):
"""Load .ui file example, using setattr/getattr approach"""
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.base_instance = setup_ui('qwidget.ui', self)
def test():
"""Example: QtCompat.loadUi with setup_ui wrapper"""
working_directory = os.path.dirname(__file__)
os.chdir(working_directory)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
# Tests
assert isinstance(window, QtWidgets.QWidget)
assert isinstance(window.parent(), type(None))
assert isinstance(window.base_instance, QtWidgets.QWidget)
assert isinstance(window.lineEdit, QtWidgets.QWidget)
assert window.lineEdit.text() == ''
window.lineEdit.setText('Hello')
assert window.lineEdit.text() == 'Hello'
app.exit()