2016-12-08 16 views
0

pyqtを使ってノードエディタを作ろうとしていて、リアルタイムでノードを追加することができません。ノードエディタ、子を追加するQWidget実行時に別のQWidget

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

addHostsTriggerhost.show()を追加します。

あなたのコードで
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) 
+0

おかげEyllanescが、それは簡単だった信じることができません.. :) – Mark

関連する問題