开发者

pyqt linux QListWidget with drag and drop of custom widget disappears

I am trying to implement a custom QListWidget to handle with custom data. I already found out how to display custom widgets in a QListWidget.

But when you drag and drop an item, the item disappears. Here is a simple example to show the problem.

import sys
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(QtGui.QMainWindow, self).__init__(parent)
        self.list_test = TestListWidget(self)
        self.setCentralWidget(self.list_test)

class TestListWidget(QtGui.QListWidget):
    def __init__(self, parent=None):
        super(QtGui.QLis开发者_开发知识库tWidget, self).__init__(parent)
        self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
        self.set_model_testdata()

    def set_model_testdata(self):
        for i in range(0, 4):
            item = QtGui.QListWidgetItem(self)
            item_widget = TestListItem("testitem %s" % i, self)
            item.setSizeHint(item_widget.sizeHint())
            self.addItem(item)
            self.setItemWidget(item, item_widget)

class TestListItem(QtGui.QWidget):
    def __init__(self, name, parent=None):
        super(QtGui.QWidget, self).__init__(parent)
        item_name_label = QtGui.QLabel("Name:")
        item_name = QtGui.QLineEdit()
        item_name.setText(name)

        vert = QtGui.QVBoxLayout()
        vert.addWidget(item_name_label)
        vert.addWidget(item_name)
        self.setLayout(vert)

if __name__ == "__main__":
    app  = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    app.exec_()

Can you help me to get the drag and drop right?

I have not understand how to display custom widgets with QTreeView, because you have to deal with a model and delegates. Maybe someone can show a small example of how to deal with QTreeView here?

EDIT: I am working on ubuntu with pyqt version 4.3 and python 2.5.

EDIT: Test on OSX 10.6.8 with pyqt version 4.8 and python 2.6 works and windows version seems to work to.

It looks like it's a linux problem.


You need to invoke super differently...

import sys
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.list_test = TestListWidget(self)
        self.setCentralWidget(self.list_test)

class TestListWidget(QtGui.QListWidget):
    def __init__(self, parent=None):
        super(TestListWidget, self).__init__(parent)
        self.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
        self.set_model_testdata()

    def set_model_testdata(self):
        for i in range(0, 4):
            item = QtGui.QListWidgetItem(self)
            item_widget = TestListItem("testitem %s" % i, self)
            item.setSizeHint(item_widget.sizeHint())
            self.addItem(item)
            self.setItemWidget(item, item_widget)

class TestListItem(QtGui.QWidget):
    def __init__(self, name, parent=None):
        super(TestListItem, self).__init__(parent)
        item_name_label = QtGui.QLabel("Name:")
        item_name = QtGui.QLineEdit()
        item_name.setText(name)

        vert = QtGui.QVBoxLayout()
        vert.addWidget(item_name_label)
        vert.addWidget(item_name)
        self.setLayout(vert)

if __name__ == "__main__":
    app  = QtGui.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    app.exec_()


I think its working fine in linux also, I just tested with ubuntu 12.04 with pyqt 4.3 and python 2.7. Maybe issue with 2.5 but I am not sure about that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜