如何从扩展的QListWidget类dropEvent中调用QMainWindow类方法?

时间:2014-11-12 23:14:54

标签: python pyqt

这是一段源代码。 当您运行它时,只需将项目从第一个listWidget拖放到第二个listWidget。

我在dropEvent中有一个print语句,我想调用名为“special”的父QMainWindow方法。

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt,QString
import sys, os

class ThumbListWidget(QtGui.QListWidget):
    _rows_to_del=[]

    def __init__(self, type, parent=None):
        super(ThumbListWidget, self).__init__(parent)
        self.setIconSize(QtCore.QSize(124, 124))
        self.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
        self.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        self.setAcceptDrops(True)
        self._dropping = False

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.accept()
        else:
            super(ThumbListWidget, self).dragEnterEvent(event)

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            super(ThumbListWidget, self).dragMoveEvent(event)

    def dropEvent(self, event):

        print "How can I call the main window class method called 'special' here?"

        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            event.setDropAction(QtCore.Qt.CopyAction)
            self._dropping = True
            super(ThumbListWidget, self).dropEvent(event)
            self._dropping = False

    def rowsInserted(self, parent, start, end):
        if self._dropping:
            self.emit(QtCore.SIGNAL("dropped"), (start, end))
        super(ThumbListWidget, self).rowsInserted(parent, start, end)

    def dataChanged(self,start,end):
        if self._dropping:
            for row in range(start.row(),end.row()+1):
                index = self.indexFromItem(self.item(row))
                shot = index.data().toString()
                #print len(self.findItems(shot,Qt.MatchExactly))
                if len(self.findItems(shot,Qt.MatchExactly))>1:
                    self._rows_to_del.append(row)

            self._rows_to_del.reverse()

            for row in self._rows_to_del:
                self.takeItem(row)

            self._rows_to_del=[]

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()
        self.listItems={}

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()
        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)

        self.listWidgetA = ThumbListWidget(self)
        for i in range(12): 
            QtGui.QListWidgetItem( 'Item '+str(i), self.listWidgetA )

        all_items =  self.listWidgetA.findItems(QString('*'), Qt.MatchWrap | Qt.MatchWildcard)
        for item in all_items:
            item.setFlags(item.flags() | Qt.ItemIsUserCheckable )
            # item.setCheckState(Qt.Checked)

        myBoxLayout.addWidget(self.listWidgetA)

        self.listWidgetB = ThumbListWidget(self)

        self.listWidgetA.setAcceptDrops(False)

        myBoxLayout.addWidget(self.listWidgetB)   

        self.connect(self.listWidgetA, QtCore.SIGNAL("dropped"), self.items_dropped)
        self.listWidgetA.currentItemChanged.connect(self.item_clicked)

        self.connect(self.listWidgetB, QtCore.SIGNAL("dropped"), self.items_dropped)
        self.listWidgetB.currentItemChanged.connect(self.item_clicked)

    def items_dropped(self, arg):
        print 'items_dropped', arg
        start,end = arg
        #print range(start,end+1)
        for row in range(start,end+1):
            item = self.listWidgetB.item(row)
            item.setFlags(item.flags() | Qt.ItemIsUserCheckable )
            item.setCheckState(Qt.Checked)

    def special(self):
        print "special called"

    def item_clicked(self, arg):
        print "arg=",arg

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:1)

在这种情况下,您将窗口传递给自定义窗口小部件的构造函数self,它将成为自定义窗口小部件的父窗口。所以你应该能够做到

window = self.parent()
window.special()

您还可以使用Qt事件系统发出主窗口可以连接的事件,并调用special方法本身。

class ThumbListWidget(QtGui.QListWidget):
    my_signal = pyqtSignal()

    def dropEvent(self, event):
        self.my_signal.emit()


class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        ...
        self.listWidgetB = ThumbListWidget(self)
        self.listWidgetB.my_signal.connect(self.special)