Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
devwiki:python_qt [2019/06/13 02:04]
ying [Tree Widget]
devwiki:python_qt [2019/07/17 21:07] (current)
ying [Tab Widget]
Line 493: Line 493:
 </​code>​ </​code>​
  
 +**Pass User Defined Function to Class Function variable holder**
 +
 +  * code <​python>​
 +wip
 +</​code>​
 ===== Hotkey and Shortcut ===== ===== Hotkey and Shortcut =====
  
Line 589: Line 594:
 | ... | QtCore.Qt.LinkAction | Link the source to the target | | ... | QtCore.Qt.LinkAction | Link the source to the target |
 | ... | QtCore.Qt.IgnoreAction | do nothing | | ... | QtCore.Qt.IgnoreAction | do nothing |
-| source() | return where the drag starts, can be a widget or zero for outside |+| source() | return where the drag starts, can be a widget or zero for outside ​| Note, not all event type has source, so check the target object first |
 | mimeData() |  | mimeData() | 
 | pos() | drop position | | pos() | drop position |
Line 643: Line 648:
  
 </​code>​ </​code>​
 +
 +  * For QTreeWidget,​ you can use 1) re-implement method (the startDrag function), 2) listener event method
 +    - the startDrag re-implement method <code python>
 +class MyTree(QtWidgets.QTreeWidget):​
 +    def __init__(self):​
 +        QtWidgets.QTreeWidget.__init__(self)
 +        self.setDragEnabled(1)
 +        # setupUI
 +        node_list = [ '​file',​ '​place2dTexture',​ '​reverse'​]
 +        [self.invisibleRootItem().addChild( QtWidgets.QTreeWidgetItem([x]) ) for x in node_list]
 +        ​
 +    def startDrag(self,​ dropActions ):
 +        # item drag activate this
 +        print('​start drag')
 +        item = self.currentItem()
 +        mimeData = QtCore.QMimeData()
 +        mimeData.setText(item.text(0)) # work with Qt object, itself or other application
 +        #​mimeData.setData('​text/​plain',​ unicode(item.text(0))) # not work as text like above
 +        print(item.text(0))
 +        ​
 +        drag = QtGui.QDrag(self)
 +        drag.setMimeData(mimeData)
 +        drag.setHotSpot(QtCore.QPoint(12,​12))
 +        ​
 +        # this make it work, return the result action
 +        # if drag.start is old method, exec conflict in old code, use exec_
 +        if drag.exec_(QtCore.Qt.CopyAction) == QtCore.Qt.CopyAction: ​
 +            print('​Drag dropped ok.')
 +    # for demo purpose as below
 +    def dragMoveEvent(self):​
 +        print('​Move'​)
 +        currentNode = self.currentItem()
 +        if currentNode:​
 +            print(currentNode.text(0))
 +    def event(self, event):
 +        if event.type() == QtCore.QEvent.KeyPress and event.key() == QtCore.Qt.Key_Tab:​
 +            print('​Tab key now')
 +            return 1
 +        return 0
 +</​code>​
 +    - the event listener method for tree <code python>
 +    cur_tree = self.uiList['​main_browse'​].uiList['​file_tree'​]
 +    cur_tree.setDragEnabled(1)
 +    cur_tree.installEventFilter(self)
 +
 +    def eventFilter(self,​ object, event):
 +        cur_tree = self.uiList['​main_browse'​].uiList['​file_tree'​]
 +        if object is cur_tree and event.type()== QtCore.QEvent.ChildRemoved:​ # the tree drag event name
 +            #​print(event.type())
 +            currentNode = cur_tree.currentItem()
 +            if currentNode:​
 +                # for multi selection case
 +                path_text = '​\n'​.join([unicode(node.text(0)) for node in cur_tree.selectedItems()])
 +                # for single selection case
 +                #cur_path = unicode(currentNode.text(0))
 +                #​print(cur_path)
 +                mimeData = QtCore.QMimeData()
 +                mimeData.setText(path_text)
 +                drag = QtGui.QDrag(self)
 +                drag.setMimeData(mimeData)
 +                drag.setHotSpot(QtCore.QPoint(12,​12))
 +                if drag.exec_(QtCore.Qt.CopyAction) == QtCore.Qt.CopyAction: ​
 +                    print('​Drag dropped ok.')
 +                return 1
 +        return 0
 +        ​
 +</​code>​
 +
 +==== QDrag and QMimeData ====
 +
 +  * mimedata is a object that contains the dragging information,​ all applications support it and pass information in that object format. like Explorer, Nuke, etc.
 +  * Drag is the object handle the drag Action (the whole process of dragging), it contains information about dragging and that mimedata.
 +
 +^ QDrag'​s method ^
 +| setMimeData() | store the mimedata |
 +| setPixmap() |
 +| setHotSpot() |
 +| exec_() | start/​init/​exec the drag action |
 ===== QThread and Threading ===== ===== QThread and Threading =====
  
Line 1191: Line 1274:
 self.uiList['​my_tree'​].setHeaderLabels(["​Name",​ "​URL"​]) self.uiList['​my_tree'​].setHeaderLabels(["​Name",​ "​URL"​])
 self.uiList['​item_tree'​].setHeaderHidden(1) self.uiList['​item_tree'​].setHeaderHidden(1)
 +# set on one column for its header label
 +cur_tree.headerItem().setText(0,​cur_version)
  
 # hide column 1 # hide column 1
Line 1201: Line 1286:
         cur_tree.setColumnHidden(col,​1-cur_tree.isColumnHidden(col))         cur_tree.setColumnHidden(col,​1-cur_tree.isColumnHidden(col))
  
-# column width+# column width (not working)
 self.uiList['​file_tree'​].setColumnWidth(0,​200) self.uiList['​file_tree'​].setColumnWidth(0,​200)
 self.uiList['​file_tree'​].setColumnWidth(1,​50) self.uiList['​file_tree'​].setColumnWidth(1,​50)
 +
 +# auto fix width to content for request column index
 +self.uiList['​element_tree'​].resizeColumnToContents(0)
 +# prevent above auto fit cause too small column look for empty column
 +self.uiList['​element_tree'​].header().setMinimumSectionSize(100) ​
  
 # auto column width, and no super long last column, 2 col in this example # auto column width, and no super long last column, 2 col in this example
Line 1446: Line 1536:
 self.uiList['​main_tab'​].count() # return total tabs self.uiList['​main_tab'​].count() # return total tabs
 </​code>​ </​code>​
-  * put tab on bottom <code python>​self.uiList['​main_tab'​].setTabPosition(QtGui.QTabWidget.South)</​code>​+  * put tab on bottom <code python>​self.uiList['​main_tab'​].setTabPosition(QtWidgets.QTabWidget.South)</​code>​
   * link ctrl+1,​2,​3,​4 to the tab switch <code python>   * link ctrl+1,​2,​3,​4 to the tab switch <code python>
 cur_tab = self.uiList['​main_tab'​] cur_tab = self.uiList['​main_tab'​]