0

嗨,我正在制作这个小项目,我需要将一个按钮连接到一个将特定框架(存储在列表中)设置为 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_())
4

1 回答 1

0

这是你应该做的:

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

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

使用这个绑定clicked比使用连接信号简单得多

于 2013-04-01T23:18:42.533 回答