QTreeView Get Selected Text (pyside2)(python3)


# -*- coding: utf-8 -*-
import os
from functools import partial
import time
import imp

"""
PySide2モジュールを探し、ある場合はそちらをインポートします。
"""
try:
    imp.find_module('PySide2')
    from PySide2.QtWidgets import *
    from PySide2.QtGui import *
    from PySide2.QtCore import *

except ImportError:
    from PySide.QtGui import *
    from PySide.QtCore import *


LOGO_IMAGE = r"画像のパスをここに入れてください。"


def get_maya_pointer():
    """
    Mayaのメインウィンドウを取得する関数
    """
    try:
        import maya.cmds as cmds
        from maya import OpenMayaUI

    except ImportError:
        return None

    """
    実は2017ではshibokenも2になっているので、あればshiboken2をインポートします。
    """
    try:
        imp.find_module("shiboken2")
        import shiboken2
        return shiboken2.wrapInstance(int(OpenMayaUI.MQtUtil.mainWindow()), QWidget)

    except ImportError:
        import shiboken
        return shiboken.wrapInstance(int(OpenMayaUI.MQtUtil.mainWindow()), QWidget)





class TreeView_Selected_Text(QMainWindow):


    def __init__(self, parent = None):
        super(TreeView_Selected_Text, self).__init__(parent)
        self.setObjectName("TreeView_Selected_Text")
        self.setWindowTitle("TreeView_Selected_Text")
        self._initUI()
        self.errorDialog = QErrorMessage(self) # QErrorMessageインスタンスの保持


    def _initUI(self):
        wrapper = QWidget()
        self.setCentralWidget(wrapper)

        mainLayout = QVBoxLayout()
        wrapper.setLayout(mainLayout)

        #-----------------------------------------------------------------------

        #-----------------------------------------------------------------------
        # fifth row
        #-----------------------------------------------------------------------
        fifthHorizontalArea = QHBoxLayout()
        fifthHorizontalArea.setSpacing(20)
        mainLayout.addLayout(fifthHorizontalArea)

        fifthHorizontalArea.addWidget(self._makeTreeWidget())

        mainLayout.addWidget(self._makeHorizontalLine())

        #-----------------------------------------------------------------------


    def _makeHorizontalLine(self):
        hline = QFrame()
        hline.setFrameShape(QFrame.HLine)
        hline.setFrameShadow(QFrame.Sunken)
        return hline





    def _makeTreeWidget(self):
        """
        QTreeWidgetを作成する関数
        """
        treeWidget = QTreeWidget()
        headerLabels = ["Name", "Age"]
        treeWidget.setColumnCount(len(headerLabels))
        treeWidget.setHeaderLabels(headerLabels)
        treeWidget.setAlternatingRowColors(True)
        treeData = {
            "Male":[
                {"name":"Taro",     "age":"25"},
                {"name":"Ichiro",   "age":"50"},
                {"name":"Jiro",     "age":"40"}
            ],
            "Female":[
                {"name":"Hanako",   "age":"30"}
            ]
        }

        #for sex, profiles in treeData.iteritems():
        for sex, profiles in treeData.items():
            topItem = QTreeWidgetItem([sex])
            treeWidget.addTopLevelItem(topItem)

            for profile in profiles:
                childItem = QTreeWidgetItem(topItem, [profile.get("name"), profile.get("age")])

        treeWidget.expandAll()
        self.treeWidget=treeWidget
        self.treeWidget.selectionModel().selectionChanged.connect(self.treeWidget_selectionChanged)
        
        return treeWidget
    def treeWidget_selectionChanged(self,selected, deselected):
        print("----treeWidget_selectionChanged-----")
        
        
        for QModelIndex in self.treeWidget.selectedIndexes():
            text = QModelIndex.data(Qt.DisplayRole) # or ix.data()
            print("text= "+str(text))
            
            QModelIndex2=QModelIndex.child(0,0).sibling(0,0)
            text2 = QModelIndex2.data(Qt.DisplayRole)   
            print("text2= "+str(text2))   
            QModelIndex3=QModelIndex.child(0,0).sibling(0,1)
            text3 = QModelIndex3.data(Qt.DisplayRole)   
            print("text3= "+str(text3))   



def start():
    maya_win = get_maya_pointer()
    ui = TreeView_Selected_Text(parent = maya_win)
    ui.show()
    return ui


if __name__ == '__main__':

    app = QApplication.instance()
    if app is None:
        app = QApplication([])
    ui = start()
    app.exec_()

関連記事

コメント

この記事へのコメントはありません。

TOP