将小部件放在选项卡中导致禁用控件的bug - python

时间:2015-12-09 21:16:22

标签: python pyside

我注意到当将一个小部件放在一个标签控件中时,它会禁用拖放的所有功能?为什么会这样?您可以在TreeWidget中清楚地看到我启用了拖放功能......?我很困惑。我再也无法拖放我的qtreewidget项目了。

# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore


class TreeNodeItem( QtGui.QTreeWidgetItem ):
    def __init__( self, parent, name="" ):
        super( TreeNodeItem, self ).__init__( parent )
        self.setText( 0, name )
        self.stuff = "Custom Names - " + str(name)

class TreeWidget(QtGui.QTreeWidget):
    def __init__(self, parent=None):
        QtGui.QTreeWidget.__init__(self, parent)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.setItemsExpandable(True)
        self.setAnimated(True)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
        self.setAlternatingRowColors(True)

# Drag-n-Drop Widget
# ------------------------------------------------------------------------------
class ExampleWidget(QtGui.QWidget):

    def __init__(self,):
        super(ExampleWidget, self).__init__()

        self.initUI()

    def initUI(self):

        # formatting
        self.resize(250, 400)
        self.setWindowTitle("Example")

        # widget - passes treewidget
        self.itemList = QtGui.QTreeWidget()
        self.itemList = TreeWidget()

        headers = [ "Items" ]
        self.itemList.setColumnCount( len(headers) )
        self.itemList.setHeaderLabels( headers )            

        # layout Grid - row/column/verticalpan/horizontalspan
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.setContentsMargins(5,5,5,5)
        self.mainLayout.addWidget(self.itemList, 0,0,1,1)

        # display
        self.show()

    # Functions
    # --------------------------------------------------------------------------   
    def showEvent(self, event):
        print "open"
        for i in xrange(20):
            TreeNodeItem( parent=self.itemList , name=str(i) )



class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      
        # formatting
        self.resize(550, 400)

        # widgets
        tab_panel = QtGui.QTabWidget()

        _tab = QtGui.QWidget()
        _tab_layout = QtGui.QHBoxLayout(_tab)
        _tab_layout.addWidget(ExampleWidget())

        tab_panel.addTab(_tab, "Testing")


        # signals
        tab_panel.currentChanged.connect(self.Update)

        # main layout
        main_layout = QtGui.QVBoxLayout()
        main_layout.addWidget(tab_panel)
        self.setLayout(main_layout)

        self.show()

    def Update(self):
        print "update"
        # self.adjustSize()  


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

修复了不正确的窗口小部件嵌套以及show事件。

# Imports
# ------------------------------------------------------------------------------
import sys
from PySide import QtGui, QtCore


class TreeNodeItem( QtGui.QTreeWidgetItem ):
    def __init__( self, parent, name="" ):
        super( TreeNodeItem, self ).__init__( parent )
        self.setText( 0, name )
        self.stuff = "Custom Names - " + str(name)

class TreeWidget(QtGui.QTreeWidget):
    def __init__(self, parent=None):
        QtGui.QTreeWidget.__init__(self, parent)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.setItemsExpandable(True)
        self.setAnimated(True)
        self.setDragEnabled(True)
        self.setDropIndicatorShown(True)
        self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
        self.setAlternatingRowColors(True)

# Drag-n-Drop Widget
# ------------------------------------------------------------------------------
class ExampleWidget(QtGui.QWidget):

    def __init__(self,):
        super(ExampleWidget, self).__init__()

        self.initUI()

    def initUI(self):

        # formatting
        self.resize(250, 400)
        self.setWindowTitle("Example")

        # widget - passes treewidget
        self.itemList = QtGui.QTreeWidget()
        self.itemList = TreeWidget()

        headers = [ "Items" ]
        self.itemList.setColumnCount( len(headers) )
        self.itemList.setHeaderLabels( headers )            

        # layout Grid - row/column/verticalpan/horizontalspan
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.setContentsMargins(5,5,5,5)
        self.mainLayout.addWidget(self.itemList, 0,0,1,1)


    # Functions
    # --------------------------------------------------------------------------   
    def showEvent(self, event):
        print "open"
        for i in xrange(20):
            TreeNodeItem( parent=self.itemList , name=str(i) )



class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      
        # formatting
        self.resize(550, 400)

        tab_panel = QtGui.QTabWidget()
        tab_panel.addTab(ExampleWidget(), "Test Tab")

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(tab_panel)
        self.setLayout(mainLayout)

    def Update(self):
        print "update"
        # self.adjustSize()  


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()



# I think it was a table widget in another widget that then got placed in a tab widget.
# Maybe another level too.

# You were also initializing something in the args part of a method.