节点编辑器,在运行时添加子QWidget另一个QWidget

时间:2016-12-08 14:19:28

标签: python python-2.7 pyqt pyqt4

我正在尝试使用pyqt制作节点编辑器,并且无法实时添加节点。

如果我在NodeEditor QWidget的初始化上添加一个节点它工作正常,但如果我使用右键菜单添加一个节点没有任何反应,任何想法我做错了什么?

import sys
from PyQt4 import QtCore, QtGui, uic

class Node(QtGui.QWidget):
    def __init__(self,parent=None):
        QtGui.QWidget.__init__(self,parent)
        widLwayout = QtGui.QVBoxLayout()
        self.setLayout(widLwayout)

        self.frame = QtGui.QFrame()
        self.frameLayout = QtGui.QVBoxLayout()
        self.frame.setLayout(self.frameLayout)
        self.frame.setStyleSheet("background-color: rgb(220, 220, 220)")

        widLwayout.addWidget(self.frame)

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == QtCore.Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()
        super(Node, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            currPos = self.mapToGlobal(self.pos())
            globalPos = event.globalPos()
            diff = globalPos - self.__mouseMovePos
            newPos = self.mapFromGlobal(currPos + diff)
            self.move(newPos)
            self.__mouseMovePos = globalPos
        super(Node, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.__mousePressPos is not None:
            moved = event.globalPos() - self.__mousePressPos 
            if moved.manhattanLength() > 3:
                event.ignore()
                return
        super(Node, self).mouseReleaseEvent(event)

class Hosts(Node):
    def __init__(self, parent=None):
        Node.__init__(self,parent)
        self.resize(150,100)
        title = QtGui.QLabel("Hosts")
        getAssigned = QtGui.QRadioButton("Assigned")
        getHostNames = QtGui.QRadioButton("Host Names")
        getHostNames.setLayoutDirection(QtCore.Qt.RightToLeft)

        self.frameLayout.addWidget(title)
        self.frameLayout.addWidget(getAssigned)
        self.frameLayout.addWidget(getHostNames)


class NodeEditor(QtGui.QWidget):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(640,480)

        self.nodes = []
        self.nodes.append( Hosts(self) )

        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.contextMenuEvent)
        self.buildMenu()

    def buildMenu(self):
        self.menu = QtGui.QMenu(self)

        self.addHosts = QtGui.QAction('Add Hosts', self)
        self.addHosts.triggered.connect(self.addHostsTrigger)
        self.menu.addAction(self.addHosts)

    def contextMenuEvent(self, event):
        self.menu.popup(QtGui.QCursor.pos())

    def addHostsTrigger(self):
        self.nodes.append( Hosts(self) )

if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)
    wnd = NodeEditor()
    wnd.show()
    sys.exit(app.exec_())

任何帮助将不胜感激。

谢谢, 标记

1 个答案:

答案 0 :(得分:1)

host.show()上添加addHostsTrigger

def addHostsTrigger(self):
    host = Hosts(self)
    host.show()
    self.nodes.append(host)

在您的代码中:

import sys
from PyQt4 import QtCore, QtGui


class Node(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        widLwayout = QtGui.QVBoxLayout()
        self.setLayout(widLwayout)

        self.frame = QtGui.QFrame()
        self.frameLayout = QtGui.QVBoxLayout()
        self.frame.setLayout(self.frameLayout)
        self.frame.setStyleSheet("background-color: rgb(220, 220, 220)")

        widLwayout.addWidget(self.frame)

    def mousePressEvent(self, event):
        self.__mousePressPos = None
        self.__mouseMovePos = None
        if event.button() == QtCore.Qt.LeftButton:
            self.__mousePressPos = event.globalPos()
            self.__mouseMovePos = event.globalPos()
        super(Node, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton:
            currPos = self.mapToGlobal(self.pos())
            globalPos = event.globalPos()
            diff = globalPos - self.__mouseMovePos
            newPos = self.mapFromGlobal(currPos + diff)
            self.move(newPos)
            self.__mouseMovePos = globalPos
        super(Node, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if self.__mousePressPos is not None:
            moved = event.globalPos() - self.__mousePressPos
            if moved.manhattanLength() > 3:
                event.ignore()
                return
        super(Node, self).mouseReleaseEvent(event)


class Hosts(Node):
    def __init__(self, parent=None):
        Node.__init__(self, parent)
        self.resize(150, 100)
        title = QtGui.QLabel("Hosts")
        getAssigned = QtGui.QRadioButton("Assigned")
        getHostNames = QtGui.QRadioButton("Host Names")
        getHostNames.setLayoutDirection(QtCore.Qt.RightToLeft)

        self.frameLayout.addWidget(title)
        self.frameLayout.addWidget(getAssigned)
        self.frameLayout.addWidget(getHostNames)


class NodeEditor(QtGui.QWidget):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(640, 480)
        self.buildMenu()
        self.nodes = []
        self.addHostsTrigger()
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.customContextMenuRequested.connect(self.contextMenuEvent)

    def buildMenu(self):
        self.menu = QtGui.QMenu(self)
        self.addHosts = QtGui.QAction('Add Hosts', self)
        self.addHosts.triggered.connect(self.addHostsTrigger)
        self.menu.addAction(self.addHosts)

    def contextMenuEvent(self, event):
        self.menu.popup(QtGui.QCursor.pos())

    def addHostsTrigger(self):
        host = Hosts(self)
        host.show()
        self.nodes.append(host)