Python无法使连接信号与类函数一起工作

时间:2013-04-01 23:07:52

标签: python list pyqt

我正在制作这个小项目我需要将一个按钮连接到一个方法,该方法将设置一个特定的框架(存储在列表中)作为QMainWindow的中心小部件,我已经调用了两次方法nuevaEscena ()创建两个场景(创建了2个帧),这是我试过的但是当我点击“Escena#”按钮时,似乎信号没有被发现,放置了一个打印来检查它并没有单击按钮时不打印任何内容,它只在调用nuevaEscena()时打印,我也不确定我处理显示框架的方式是否正确,谢谢这里是代码:

import sys
import sip
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#from blurdev.gui import Dialog
# from Py3dsMax import mxs

myMimeType = 'application/MyWindow'

class MyLabel(QLabel):
    def __init__(self, parent):
        super(MyLabel, self).__init__(parent)
        self.setStyleSheet("background-image: url(selector.png);")
        self.adjustSize()
    def mouseMoveEvent(self, e):
        if e.buttons() != Qt.LeftButton:
            return

        # write the relative cursor position to mime data
        mimeData = QMimeData()
        # simple string with 'x,y'
        mimeData.setText('%d,%d' % (e.x(), e.y()))
        # start the drag operation
        # exec_ will return the accepted action from dropEvent
        if drag.exec_(Qt.MoveAction | Qt.CopyAction) == Qt.MoveAction:
            self.show()



    def mousePressEvent(self, event):
        itemData   = QByteArray()
        dataStream = QDataStream(itemData, QIODevice.WriteOnly)
        dataStream.writeString(self.text())
        dataStream << QPoint(event.pos() - self.rect().topLeft())

        mimeData = QMimeData()
        mimeData.setData(myMimeType, itemData)
        mimeData.setText(self.text())

        drag = QDrag(self)
        drag.setMimeData(mimeData)
        drag.setHotSpot(event.pos() - self.rect().topLeft())

        #self.hide()
        # mxs.select(mxs.getNodeByName('a'))
        if drag.exec_(Qt.MoveAction | Qt.CopyAction) == Qt.MoveAction:
            self.close()
        else:
            self.show()


class MyFrame(QFrame):
    def __init__(self, parent=None):
        super(MyFrame, self).__init__(parent)
        self.setStyleSheet("background-image: url(logo.png);")
        self.setFixedSize(800,600)
        y = 6

        for labelNumber in range(6):
            label = MyLabel(self)
            label.resize(50, 50)
            label.move(6, y)
            label.show()
            y += label.height() + 2

        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        if event.mimeData().hasFormat(myMimeType):
            if event.source() in self.children():
                event.setDropAction(Qt.MoveAction)
                event.accept()
            else:
                event.acceptProposedAction()

    def dropEvent(self, event):
        if event.mimeData().hasFormat(myMimeType):
            mime       = event.mimeData()
            itemData   = mime.data(myMimeType)
            dataStream = QDataStream(itemData, QIODevice.ReadOnly)

            text = QByteArray()
            offset = QPoint()
            dataStream >> text >> offset

            newLabel = MyLabel(self)
            newLabel.setText(event.mimeData().text())
            newLabel.move(event.pos() - offset)
            newLabel.resize(50, 50)
            newLabel.show()

            if event.source() in self.children():
                event.setDropAction(Qt.MoveAction)
                event.accept()

            else:
                event.acceptProposedAction()

        #~ else:
            #~ event.ignore()

class MyWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.move(app.desktop().screen().rect().center() - self.rect().center())
        self.crearPanelEscenas()
        self.crearMenus()
        self.nuevaEscena()
        self.nuevaEscena()

    #GUI Interfaces
    def crearPanelEscenas(self):
        # Lista de escenas
        self.listaEscenas = []
        # Escena actual
        self.escenaActual = -1;
        # Panel para los botones de escenas
        self.listaEscenasGUI = QDockWidget("Escenas")
        self.listaEscenasGUI.setFeatures(QDockWidget.NoDockWidgetFeatures)

        dockWidget = QWidget()
        dockWidgetLayout = QHBoxLayout()
        dockWidget.setLayout(dockWidgetLayout)

        self.listaEscenasGUI.setWidget(dockWidget)
        self.addDockWidget(Qt.TopDockWidgetArea, self.listaEscenasGUI)

    def nuevaEscena(self):
        self.escenaActual = self.escenaActual + 1;
        # Button para seleccionar escena a ver
        myButton = QPushButton("Escena "+str(self.escenaActual))
        self.listaEscenasGUI.widget().layout().addWidget(myButton)
        # Creo la escena nueva
        nuevaEscena = MyFrame() 
        # La guardo             
        self.listaEscenas.append(nuevaEscena)   
        self.connect(myButton, SIGNAL("clicked()"), SLOT(self.mostrarEscena(self.escenaActual)))

    def mostrarEscena(self, index):
        print "Clicked"
        self.escenaActual = index
        self.setCentralWidget(self.listaEscenas[index])

    #GUI Menus
    def crearMenus(self):
        self.menuBar = self.menuBar()
        adicionarAction = QAction("Adicionar",self.menuBar)
        eliminarAction = QAction("Eliminar",self.menuBar)

        self.menuArchivo  = self.menuBar.addMenu("&Archivo")
        self.menuInterfaz = self.menuBar.addMenu("&Interfaz")

        self.menuInterfaz.addAction(adicionarAction)
        self.menuInterfaz.addAction(eliminarAction)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setApplicationName('GUI Picker')
    main = MyWindow()
    main.show()
    sys.exit(app.exec_())

1 个答案:

答案 0 :(得分:0)

这是你应该做的:

MyButton.clicked.connect(lambda: self.mostrarEscena(self.escenaActual))

您需要一个lambda函数,否则您将调用该方法并传递结果对象。

将此绑定与clicked一起使用比使用信号连接

简单得多