我想實現一個簡單的節點接口,其中節點執行其輸入連接的總和,並輸出結果。如何在PyQt中編寫非常基本的基於節點的編輯器?
實施例:http://i.stack.imgur.com/h0l6N.png
只是一段簡單的功能的代碼。
我想實現一個簡單的節點接口,其中節點執行其輸入連接的總和,並輸出結果。如何在PyQt中編寫非常基本的基於節點的編輯器?
實施例:http://i.stack.imgur.com/h0l6N.png
只是一段簡單的功能的代碼。
這應該是完全可能的。
我建議你爲盒子生成新的QLineEdits(你可以給他們一個使用Qt樣式表的盒子)。然後,您將能夠解析通過Python中的輸入。我會建議你閱讀this question on draggable buttons。你應該能夠適應qlineedit。我也會建議你一般對鼠標事件非常熟悉。
連接器可能是QWidgets,您可以在小部件上定義自定義paintEvent
以繪製直線和圓。
編輯:這實際上是我感興趣的東西,所以我想我會掀起一個例子。
EDIT3:嗯,我有一些樣本可活動塊:
import sys, time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
class Entry(QLineEdit):
def __init__(self,parent=None):
QLineEdit.__init__(self,parent)
self.resize(75,50)
def paintEvent(self,event):
painter=QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.HighQualityAntialiasing)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.drawEllipse(54,25,20,20)
painter.drawRect(QRect(0,0,self.width()-22,self.height()-1))
font=QFont('Arial',13)
painter.setFont(font)
painter.drawText(QPoint(22,22),self.text())
painter.end()
#movements from https://stackoverflow.com/questions/12219727/dragging-moving-a-qpushbutton-in-pyqt
def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == Qt.LeftButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
super(Entry, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
# adjust offset from clicked point to origin of widget
currPos = self.mapToGlobal(self.pos())
globalPos = event.globalPos()
diff = globalPos - self.__mouseMovePos
newPos = self.mapFromGlobal(currPos + diff)
self.move(newPos)
self.__mouseMovePos = globalPos
super(Entry, 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(Entry, self).mouseReleaseEvent(event)
class Add(QWidget):
def __init__(self,parent=None):
QWidget.__init__(self,parent)
self.resize(75,50)
def paintEvent(self, event):
#use QPainter for a custom look
painter=QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.HighQualityAntialiasing)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.drawEllipse(0,25,20,20)
painter.drawEllipse(0,0,20,20)
painter.drawRect(QRect(20,0,34,49))
painter.drawEllipse(53,24,20,20)
font=QFont('Arial',13)
painter.setFont(font)
painter.drawText(QPoint(22,22),"Add")
painter.end()
#movements from https://stackoverflow.com/questions/12219727/dragging-moving-a-qpushbutton-in-pyqt
def mousePressEvent(self, event):
self.__mousePressPos = None
self.__mouseMovePos = None
if event.button() == Qt.LeftButton:
self.__mousePressPos = event.globalPos()
self.__mouseMovePos = event.globalPos()
super(Add, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() == Qt.LeftButton:
# adjust offset from clicked point to origin of widget
currPos = self.mapToGlobal(self.pos())
globalPos = event.globalPos()
diff = globalPos - self.__mouseMovePos
newPos = self.mapFromGlobal(currPos + diff)
self.move(newPos)
self.__mouseMovePos = globalPos
super(Add, 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(Add, self).mouseReleaseEvent(event)
class Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.resize(640,480)
self.add=Add(self)
self.entry1=Entry(self)
self.entry2=Entry(self)
if __name__=="__main__":
app=QApplication(sys.argv)
win=Window()
win.show()
sys.exit(app.exec_())
注意,沒有連接部件。我會根據鼠標按鍵繪製自定義小部件(例如,某人按下輸入終端,然後添加)。另請注意,Entry塊沒有明確的方式表示有條目。你可以連接一個Qtimer並在文本閃爍的下方有一行。
你最終也製作了連接部分嗎?我希望看到你如何做到這一點。 – rataplan
你可以證明你到目前爲止所嘗試過的嗎? –
@Ben,我花了8個多小時試圖找到問題的解決方案,類似的問題之前已經發布,但沒有人給出明確的答案,我只需要一些東西開始 –
@JaneSmith你能告訴我們什麼你看過嗎? (什麼類似的問題?等等) – IronManMark20