Last active
March 26, 2024 03:51
-
-
Save 44hero/3b53d254293412cd661b05e3b4bad1ea to your computer and use it in GitHub Desktop.
```
- Widget 作ったら、Layout が無ければ見れない!!
- 途中ならば、Layout を Layout することは可能!
- Widget は親、Layout は子
- WidgetとLayoutの親子関係は、 - 通常、Widgetが親であり、その中にLayoutや他のWidgetが配置される形になる
- 最終的には、Layout するためには、Widget が必要
- Layout の無い、Widget も window に配置はできるが、まずもって、
This file contains 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
from PySide2.QtWidgets import (QWidget, QVBoxLayout, QPushButton, | |
QMainWindow, QApplication | |
) | |
from PySide2.QtCore import Qt | |
from maya import OpenMayaUI | |
from shiboken2 import wrapInstance | |
def maya_main_window(): | |
main_window_ptr = OpenMayaUI.MQtUtil.mainWindow() | |
return wrapInstance(int(main_window_ptr), QMainWindow) | |
class Test(QMainWindow): | |
def __init__(self, parent = None, flags = Qt.WindowFlags()): | |
self.size: Tuple[int] = (500, 300, 210, 270) # x, y, width, height | |
self.title = 'MainWindow_Test' | |
self.win = self.title + '_ui' | |
# 当該window を Maya window 画面の前面に ######################################## start | |
if parent is None: | |
parent = maya_main_window() | |
super(Test, self).__init__(parent) | |
# オリジナルメソッド | |
# 重複ウィンドウの回避関数 | |
def _duplicateWindowAvoidFunction(self, winName: str): | |
widgets = QApplication.allWidgets() | |
for w in widgets: | |
if w.objectName() == winName: | |
# w.close() | |
w.deleteLater() | |
# オリジナルメソッド | |
# Window基本設定 | |
def _windowBasicSettings(self): | |
self.setWindowTitle(self.title) # <- window の title名 設定 | |
if not self.isVisible(): | |
self.setGeometry(*self.size) # 完全に新規にUI作成したときの初期値。アンパック(*)は重要 | |
self._duplicateWindowAvoidFunction(self.win) # 重複ウィンドウの回避関数 | |
self.setWindowFlags(Qt.Window | Qt.WindowCloseButtonHint) # windowの右上にx印(close)のみ | |
self.setAttribute(Qt.WA_DeleteOnClose) # <- close 時に delete 設定 | |
self.setObjectName(self.win) # <- window へobjectName 設定 | |
def createUI(self): | |
self._windowBasicSettings() # Window基本設定 | |
central_wid = QWidget(self) # セントラルウィジェット | |
self.setCentralWidget(central_wid) # 中央に配置 | |
main_vbxLay = QVBoxLayout() # メインレイアウト | |
central_wid.setLayout(main_vbxLay) # メインレイアウト | |
# QPushButton 用の pBtnまとまりレイアウト の作成 | |
layout = QVBoxLayout() | |
# QPushButton の作成 | |
pBtnWid = QPushButton('hello') | |
# QPushButton を pBtnまとまりレイアウト に追加 | |
layout.addWidget(pBtnWid) | |
# QPushButton の作成 | |
pBtnWid = QPushButton('hello') | |
# QPushButton を pBtnまとまりレイアウト に追加 | |
layout.addWidget(pBtnWid) | |
# # レイアウト を クラスのベースとなっている ウィジェット に追加 | |
# self.setLayout(layout) | |
# pBtnまとまりレイアウト を クラスのベースとなっている メインレイアウト に追加 | |
main_vbxLay.addLayout(layout) | |
self.show() | |
if __name__ == '__main__': | |
print(u'{}.py: loaded as script file'.format(__name__)) | |
test = Test() | |
test.createUI() | |
else: | |
print(u'{}.py: loaded as module file'.format(__name__)) | |
print('{}'.format(__file__)) # 実行したモジュールフルパスを表示する | |
# pprint.pprint(RT4_UI_PyMel.mro()) # メソッドを呼び出す順番が解ります | |
print(u'モジュール名:{}\n'.format(__name__)) # 実行したモジュール名を表示する |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment