Created
February 3, 2019 16:26
-
-
Save kanzwataru/d8a1256f17590f74f8a47c252b682ba3 to your computer and use it in GitHub Desktop.
Load Qt .ui file and add methods・uiファイルを読み込んでメソッドを追加
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <ui version="4.0"> | |
| <class>Form</class> | |
| <widget class="QWidget" name="Form"> | |
| <property name="geometry"> | |
| <rect> | |
| <x>0</x> | |
| <y>0</y> | |
| <width>350</width> | |
| <height>301</height> | |
| </rect> | |
| </property> | |
| <property name="windowTitle"> | |
| <string>Form</string> | |
| </property> | |
| <layout class="QGridLayout" name="gridLayout"> | |
| <item row="0" column="0"> | |
| <layout class="QVBoxLayout" name="verticalLayout"> | |
| <item> | |
| <widget class="QLabel" name="label"> | |
| <property name="text"> | |
| <string>TextLabel</string> | |
| </property> | |
| </widget> | |
| </item> | |
| <item> | |
| <widget class="QPushButton" name="pushButton"> | |
| <property name="text"> | |
| <string>PushButton</string> | |
| </property> | |
| </widget> | |
| </item> | |
| </layout> | |
| </item> | |
| </layout> | |
| </widget> | |
| <resources/> | |
| <connections/> | |
| </ui> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import sys | |
| import types | |
| from Qt import QtCore, QtGui, QtCompat, QtWidgets | |
| def create_ui(classtype, uifile, *args): | |
| """ | |
| uiファイルからウィジェットを読み込んで特定のクラスから関数をすべて移植する | |
| Args: | |
| classtype (class): このクラスから関数を移植する | |
| uifile (str): このパスのファイルを読み込む | |
| *args: setup関数に渡される | |
| Returns: | |
| uiファイルのルート | |
| """ | |
| assert(hasattr(classtype, 'setup')) | |
| ui = QtCompat.loadUi(uifile) | |
| for member in dir(classtype): | |
| if hasattr(getattr(classtype, member), '__func__'): | |
| func = getattr(classtype, member).__func__ | |
| setattr(ui, member, types.MethodType(func, ui)) | |
| ui.setup(*args) | |
| return ui | |
| class TestWidget(object): | |
| """ | |
| このクラスを直接作成しないで、 | |
| uiファイルのルートウィジェットに関数が追加されます | |
| """ | |
| def setup(self, parent=None): | |
| self.setParent(parent) | |
| self.pushButton.clicked.connect(lambda: self.do()) | |
| self.times = 0 | |
| def do(self): | |
| self.times += 1 | |
| self.label.setText('Button has been pressed: {}'.format(self.times)) | |
| if __name__ == '__main__': | |
| app = QtWidgets.QApplication(sys.argv) | |
| # uiファイルを読み込んでTestWidgetから関数を移植します | |
| widget = create_ui(TestWidget, 'test.ui') | |
| widget.show() | |
| # この場合はuiファイルにはウィンドウじゃなくてレイアウトがあって | |
| # こっちでウィンドウを作ってウィジェットを追加します。 | |
| # uiファイル側でウィンドウを作っても構わない | |
| win = QtWidgets.QMainWindow() | |
| win.setCentralWidget(widget) | |
| win.show() | |
| sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment