Houdini

Python Panel 정리

Tedd_Kim 2023. 11. 27. 18:10

[알아두면 좋을 팁부터 정리]

01. 상대경로

from .flowlayout import Flowlayout

- 폴더명 앞에 마침표를 찍으면 상대경로의 의미이다

 

02. 짤팁

workpath = os.path.join(os.path.dirname(__file__))

 

03. Qt Designer Ui 로드

self.loader = QUiLoader()

self.ui = self.loader.load(workpath+"/assets/Tfxassetbrowser_v02.ui")

 

04. 클래스 상속

Python2에서 클래스를 상속하는 방법과  Python3에서 클래스를 상속하는 방법이 다르다

Python2를 기준으로는 

- Class TfxAssetBrowser(QtWidgets.QWidget):

      def __init__(self):

          super(TfxAssetBrowser, self).__init__()

- Class Flowlayout(QLayout):

      def __init__(self, parent=None, margin=0, spacin=-1):

          super(Flowlayout, self).__init__(parent)

 

super의 첫번째 인자는 자식클래스 이름이 들어가고, 두번째인자는 self

그리고 __init__은 자식클래스 생성자에 인자가 있다면 전달해준다

 

05. os 모듈 활용

os.listdir(path) = path에 존재하는 file과 directory 리턴

os.path.isdir(path) = path가 디렉토리인지의 경위를 bool로 리턴 

os.wak(path) = path를 재귀적으로 순회하며 모든 file과 directory리턴 

for(root, directories, files) in os.walk(dir_path):

    for file in files:

        if '.exr' in file:

            file_path = os.path.join(root, file)

os.path.exist(path) = path가 존재하는지를 bool로 리턴

 

06. for문 count

for index, item in enumerate(myList) = myList의 item을 순회하면서 index도 알 수 있음

 

07. connect() 활용

콜백함수는 self.sender()로 어디서 요청됐는지 알 수 있음

 

08. json파일 읽기

with open('/json/path', 'r') as file:

    data = json.load(file)

    return data[key]

 

09. 반복문으로 리스트 만들기

[item.text(0) for item in treeItemSelected]

 

 

[Qt 클래스에서 사용해본 메소드]

참 클래스마다 개성이 강하다는 생각이 든다

 

QLabel

 

QSize

 

QComboBox

-QComboBox.addItem(string)

-QComboBox.setCurrentIndex(int)

-QComboBox.currentIndex()

-QComboBox.currentText()

-QComboBox.currentIndexChagned.connect()

-QComboBox.clear()

 

QPushButton

-QPushButton.setIcon(QtGui.QIcon)

-QPushButton.setProperty('transparent', True)

-QPushButton.setStyleSheet()

-QPushButton.resize(40, 40)

 

QMenu & QAction

-QAction(string)

-QAction.setData()

-QMenu.addAction(QAction)

-QAction.triggered.connect()

-QMenu.exec_(QCursor.pos); pos위치에서 실제 메뉴 창 실행

 

QToolButton

-QToolButton.setToolButtonStyle(Qt.ToolButtonUnderIcon)

-QToolButton.setIcon(QtGui.QIcon)

-QToolButton.setIconSize(QtCore.QSize)

-QToolButton.setFixedSize(QtCore.QSize)

-QToolButton.setText(string)

-QToolButton.setObjectName()

-QToolButton.setStyleSheet()

-QToolButton.setMenu(QMenu)

-QToolButton.setPopupMode(QtWidgets.QToolButton.InstantPopup)

 

QLayout

-QLayout.addWidget(widget)

-QLayoutcount()

-QLayout.takeAt()

난해하니까 예시로 들자면

while QLayout.count():

    item = QLayout.takeAt(0)

    widget = item.widget()

 

    if widget:

        widget.deleteLater()

Layout에 존재하는 모든 widget을 지우는 기능을 해준다

 

QTreeWidget 

-QTreeWidget.setHeaderLabel([string1, string2, ...])

-QTreeWidget.setAlternatingRowColors(True)

-QTreeWidget.setSelectionMode(QtWidgets.QTreeWidget.ExtendedSelection) = 컨트롤이나 Shift누르면서 복수선택

-QTreeWidget.itemSelectionChanged.connect()

-QTreeWidget.addTopLevelItem(QTreeWidgetItem)

-QTreeWidget.topLevelItemCount()

 

QTreeWidgetItem

-QTreeWidget.QTreeWidgetItem(parent); 객체 생성

-QTreeWidgetItem.childCount()

-QTreeWidgetItem.child()

-QTreeWidgetItem.setText(string)

-QTreeWidgetItem.text()

-QTreeWidgetItem.setData(int; column, Qt.userRole, string; value)

-QTreeWidgetItem.data()

for i in range(root.childCount()):

    child = root.child(i)

    pass

TreeWidget 순회

 

QSlider

-QSlider.valueChanged.connect()

 

QCursor

-QCursor.pos