pyqt QTreeWidget setItemWidget 拖放后消失

发布于 2024-08-23 16:18:19 字数 1876 浏览 11 评论 0原文

我试图在使用 QTreeWidget.setItemWidget() 重新父级(拖放)后将小部件放入 QTreeWidgetItem

但结果是,如果您编译以下代码 - QTreeWidgetItem 内的小部件消失。 知道为什么吗?什么代码可以解决这个问题(用我猜的小部件重新填充 QTreeWidgetItem?)

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class InlineEditor (QWidget):
    _MUTE = 'MUTE'

    def __init__ (self, parent):
        QWidget.__init__ (self, parent)

        self.setAutoFillBackground (True)
        lo = QHBoxLayout()
        lo.setSpacing(4)

        self._cbFoo = QComboBox()
        for x in ["ABC", "DEF", "GHI", "JKL"]:
            self._cbFoo.addItem(x)

        self._leBar = QLineEdit('', self)
        lo.addWidget (self._cbFoo, 3)
        lo.addSpacing (5)
        lo.addWidget (QLabel ( 'Bar:'))
        lo.addWidget (self._leBar, 3)
        lo.addStretch (5)
        self.setLayout (lo)

class Form (QDialog):
    def __init__(self,parent=None):
        QDialog.__init__(self, parent)

        grid = QGridLayout ()
        tree = QTreeWidget ()

        # Here is the issue?
        tree.setDragDropMode(QAbstractItemView.InternalMove)

        tree.setColumnCount(3)

        for n in range (2):
            i = QTreeWidgetItem (tree) # create QTreeWidget the sub i
            i.setText (0, "first" + str (n)) # set the text of the first 0
            i.setText (1, "second")
            for m in range (2):
                j = QTreeWidgetItem(i)
                j.setText (0, "child first" + str (m))

        #b1 = QCheckBox("push me 0", tree) # this wont work w/ drag by itself either
        #tree.setItemWidget (tree.topLevelItem(0).child(1), 1, b1)

        item = InlineEditor(tree) # deal with a combination of multiple controls
        tree.setItemWidget(tree.topLevelItem(0).child(1), 1, item)

        grid.addWidget (tree)
        self.setLayout (grid)

app = QApplication ([])
form = Form ()
form.show ()
app.exec_ ()

I'm trying to keep a widget put into a QTreeWidgetItem after a reparent (drag and drop) using QTreeWidget.setItemWidget()

But the result, if you compile the following code - is that the widget inside the QTreeWidgetItem disappears.
Any idea why? What code would fix this (repopulate the QTreeWidgetItem with the widget I guess?)

from PyQt4.QtCore import *
from PyQt4.QtGui import *


class InlineEditor (QWidget):
    _MUTE = 'MUTE'

    def __init__ (self, parent):
        QWidget.__init__ (self, parent)

        self.setAutoFillBackground (True)
        lo = QHBoxLayout()
        lo.setSpacing(4)

        self._cbFoo = QComboBox()
        for x in ["ABC", "DEF", "GHI", "JKL"]:
            self._cbFoo.addItem(x)

        self._leBar = QLineEdit('', self)
        lo.addWidget (self._cbFoo, 3)
        lo.addSpacing (5)
        lo.addWidget (QLabel ( 'Bar:'))
        lo.addWidget (self._leBar, 3)
        lo.addStretch (5)
        self.setLayout (lo)

class Form (QDialog):
    def __init__(self,parent=None):
        QDialog.__init__(self, parent)

        grid = QGridLayout ()
        tree = QTreeWidget ()

        # Here is the issue?
        tree.setDragDropMode(QAbstractItemView.InternalMove)

        tree.setColumnCount(3)

        for n in range (2):
            i = QTreeWidgetItem (tree) # create QTreeWidget the sub i
            i.setText (0, "first" + str (n)) # set the text of the first 0
            i.setText (1, "second")
            for m in range (2):
                j = QTreeWidgetItem(i)
                j.setText (0, "child first" + str (m))

        #b1 = QCheckBox("push me 0", tree) # this wont work w/ drag by itself either
        #tree.setItemWidget (tree.topLevelItem(0).child(1), 1, b1)

        item = InlineEditor(tree) # deal with a combination of multiple controls
        tree.setItemWidget(tree.topLevelItem(0).child(1), 1, item)

        grid.addWidget (tree)
        self.setLayout (grid)

app = QApplication ([])
form = Form ()
form.show ()
app.exec_ ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走野 2024-08-30 16:18:19

通过编写我自己的 treeDropEvent 设法获得相对“有效”的修复...但是,如果有人有更优雅的解决方案,请随时分享。下面的代码将解决其他人在树中使用 setItemWidgets 进行拖放的麻烦,欢呼。

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class InlineEditor (QWidget):
    _MUTE = 'MUTE'

    def __init__ (self, parent):
        QWidget.__init__ (self, parent)

        self.setAutoFillBackground (True)
        lo = QHBoxLayout()
        lo.setSpacing(4)

        self._cbFoo = QComboBox()
        for x in ["ABC", "DEF", "GHI", "JKL"]:
            self._cbFoo.addItem(x)

        self._leBar = QLineEdit('', self)
        lo.addWidget (self._cbFoo, 3)
        lo.addSpacing (5)
        lo.addWidget (QLabel ( 'Bar:'))
        lo.addWidget (self._leBar, 3)
        lo.addStretch (5)
        self.setLayout (lo)

class Tree(QTreeWidget):
    def __init__(self, parent=None):
        QTreeWidget.__init__(self, parent)

        # Here is the issue?
        self.setDragDropMode(QAbstractItemView.InternalMove)
        self.installEventFilter(self)
        self.setColumnCount(3)
        self.dropEvent = self.treeDropEvent

        for n in range (2):
            i = QTreeWidgetItem (self) # create QTreeWidget the sub i
            i.setText (0, "first" + str (n)) # set the text of the first 0
            i.setText (1, "second")
            for m in range (2):
                j = QTreeWidgetItem(i)
                j.setText (0, "child first" + str (m))

        self.item = InlineEditor(self) # deal with a combination of multiple controls
        self.setItemWidget(self.topLevelItem(0).child(1), 1, self.item)

    def treeDropEvent(self, event):
        dragItem = self.currentItem()

        QTreeWidget.dropEvent(self, event)
        # rebuild widget (grabbing it doesnt seem to work from self.itemWidget?)
        self.item = InlineEditor(self) 
        self.setItemWidget(dragItem, 1, self.item)

class Form (QDialog):
    def __init__(self,parent=None):
        QDialog.__init__(self, parent)
    grid = QGridLayout ()
    tree = Tree ()
    grid.addWidget (tree)
    self.setLayout (grid)

app = QApplication ([])
form = Form ()
form.show ()
app.exec_ ()

managed to get a relatively "working" fix in by writing my own treeDropEvent... however if someone has a more elegant solution, please feel free to share. the code below will solve anyone else's headaches for drag/drop with setItemWidgets in a tree, cheers.

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class InlineEditor (QWidget):
    _MUTE = 'MUTE'

    def __init__ (self, parent):
        QWidget.__init__ (self, parent)

        self.setAutoFillBackground (True)
        lo = QHBoxLayout()
        lo.setSpacing(4)

        self._cbFoo = QComboBox()
        for x in ["ABC", "DEF", "GHI", "JKL"]:
            self._cbFoo.addItem(x)

        self._leBar = QLineEdit('', self)
        lo.addWidget (self._cbFoo, 3)
        lo.addSpacing (5)
        lo.addWidget (QLabel ( 'Bar:'))
        lo.addWidget (self._leBar, 3)
        lo.addStretch (5)
        self.setLayout (lo)

class Tree(QTreeWidget):
    def __init__(self, parent=None):
        QTreeWidget.__init__(self, parent)

        # Here is the issue?
        self.setDragDropMode(QAbstractItemView.InternalMove)
        self.installEventFilter(self)
        self.setColumnCount(3)
        self.dropEvent = self.treeDropEvent

        for n in range (2):
            i = QTreeWidgetItem (self) # create QTreeWidget the sub i
            i.setText (0, "first" + str (n)) # set the text of the first 0
            i.setText (1, "second")
            for m in range (2):
                j = QTreeWidgetItem(i)
                j.setText (0, "child first" + str (m))

        self.item = InlineEditor(self) # deal with a combination of multiple controls
        self.setItemWidget(self.topLevelItem(0).child(1), 1, self.item)

    def treeDropEvent(self, event):
        dragItem = self.currentItem()

        QTreeWidget.dropEvent(self, event)
        # rebuild widget (grabbing it doesnt seem to work from self.itemWidget?)
        self.item = InlineEditor(self) 
        self.setItemWidget(dragItem, 1, self.item)

class Form (QDialog):
    def __init__(self,parent=None):
        QDialog.__init__(self, parent)
    grid = QGridLayout ()
    tree = Tree ()
    grid.addWidget (tree)
    self.setLayout (grid)

app = QApplication ([])
form = Form ()
form.show ()
app.exec_ ()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文