1
因此,我正在建設一個基於節點的接口使用PyQt爲我正在進行的項目,我有一些問題獲取屬於基地的對象不遵循它在太空中。我想當用戶拖動基節點時,子對象(輸入和輸出框)跟隨它。我有一個可以拖拽的節點,但是子對象沒有正確地執行。有任何想法嗎?PyQt節點接口 - Parrent對ItemIsMovable對象
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
This is the base py file for the GUI
"""
import sys
from PyQt4 import QtGui, QtCore
from array import *
"""
Base class for a node. Contains all the initialization, drawing, and containing inputs and outputs
"""
class node():
width = 100
height = 100
color = 1
x = 90
y = 60
inputs=[]
outputs=[]
def __init__(self, nWidth, nHeight):
self.width = nWidth
self.height = nHeight
self.iniNodeData()
"""
This is where inputs and outputs will be created
"""
def iniNodeData(self):
for j in range(5):
this = self
x = input(this,90, 0+(j*10))
self.inputs.append(x)
"""Draw the node then the input and output objects"""
def draw(self, drawObj):
item = drawObj.addRect(self.x, self.y, self.width, self.height)
item.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
for curInput in self.inputs:
curInput.draw(drawObj)
print("(", self.x, ",", self.y, ")")
"""
Nodes will evaluate from the last node to the first node, therefore inputs are evaluted
"""
class input():
currentConnectedNode = None
currentConnectedOutput = None
parentNode = None
width = 10
height = 10
x = 1
y = 1
color = 1
def __init__(self, pnode, posX, posY):
self.parentNode = pnode
self.x = posX
self.y = posY
self.color = 1
def draw(self, drawObj):
item = drawObj.addRect(self.x+self.parentNode.x, self.y+self.parentNode.y, self.width, self.height)
class output():
parentNode = None
class MainWindow(QtGui.QGraphicsView):
nodes = []
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
def initUI(self):
for j in range(1):
x = node(100,100)
self.nodes.append(x)
self.setScene(QtGui.QGraphicsScene())
self.setWindowTitle('RIS RIB Generator')
self.setGeometry(800, 600, 800, 850)
self.initNodes()
self.show()
def initNodes(self):
for curNode in self.nodes:
curNode.draw(self.scene())
def main():
app = QtGui.QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
app.exec_()
if __name__ == '__main__':
main()
是否需要創建某種類型的事件偵聽器,如此處所述? http://zetcode.com/gui/pyqt4/eventsandsignals/ – jspada 2015-04-01 13:56:56