タスクを達成するには、qtnodes(https://github.com/cb109/qtnodes)モジュールとPyqtドラッグドロップコードを組み合わせる必要があります。以下のコードは役に立ちますが、qtnodeの内部コードを読む必要があります。ドラッグドロップオプションを妥協することができれば、qtnodeだけでアプリケーションを構築するのは簡単です。
Tobieと同じツールを構築できますが、pysideまたはpyqtまたはtkinterを使用してすべてのコードを一から書き込む必要があります。コードの下グラフィックシーンに
ドラッグドロップリストを
from PyQt4 import QtCore, QtGui
import sys
class GraphicsScene(QtGui.QGraphicsScene):
def __init__(self, parent = None):
super(GraphicsScene, self).__init__(parent)
def dragEnterEvent(self, event):
event.accept()
def dragMoveEvent(self, event):
event.accept()
def dragLeaveEvent(self, event):
event.accept()
def dropEvent(self, event):
text = QtGui.QGraphicsTextItem(event.mimeData().text())
text.setPos(event.scenePos())
self.addItem(text)
event.accept()
class ListView(QtGui.QListView):
def __init__(self, parent = None):
super(ListView, self).__init__(parent)
self.setDragEnabled(True)
def dragEnterEvent(self, event):
event.setDropAction(QtCore.Qt.MoveAction)
event.accept()
def startDrag(self, event):
index = self.indexAt(event.pos())
if not index.isValid():
return
selected = self.model().data(index, QtCore.Qt.DisplayRole)
mimeData = QtCore.QMimeData()
mimeData.setText(selected.toString())
drag = QtGui.QDrag(self)
drag.setMimeData(mimeData)
result = drag.start(QtCore.Qt.MoveAction)
if result: # == QtCore.Qt.MoveAction:
pass
def mouseMoveEvent(self, event):
self.startDrag(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
self.setGeometry(100, 100, 400, 400)
self.widget = QtGui.QWidget()
self.setCentralWidget(self.widget)
layout = QtGui.QGridLayout(self.widget)
self.ListView = ListView()
data = QtCore.QStringList()
data << "one" << "two" << "three"
self.model = QtGui.QStringListModel(data)
self.ListView.setModel(self.model)
self.GraphicsView = QtGui.QGraphicsView()
self.scene = GraphicsScene()
self.GraphicsView.setScene(self.scene)
#self.GraphicsView.setSceneRect(0, 0, self.GraphicsView.width(), self.GraphicsView.height())
layout.addWidget(self.ListView, 0, 0, 5, 5)
layout.addWidget(self.GraphicsView, 0, 1, 5, 5)
self.show()
self.GraphicsView.setSceneRect(0, 0, self.GraphicsView.width(), self.GraphicsView.height())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
ボタン
from PyQt4 import QtGui, QtCore
class DragButton(QtGui.QPushButton):
def __init__(self, parent):
super(DragButton, self).__init__(parent)
self.allowDrag = True
def setAllowDrag(self, allowDrag):
if type(allowDrag) == bool:
self.allowDrag = allowDrag
else:
raise TypeError("You have to set a boolean type")
def mouseMoveEvent(self, e):
if e.buttons() != QtCore.Qt.RightButton:
return
if self.allowDrag == True:
# write the relative cursor position to mime data
mimeData = QtCore.QMimeData()
# simple string with 'x,y'
mimeData.setText('%d,%d' % (e.x(), e.y()))
print mimeData.text()
# let's make it fancy. we'll show a "ghost" of the button as we drag
# grab the button to a pixmap
pixmap = QtGui.QPixmap.grabWidget(self)
# below makes the pixmap half transparent
painter = QtGui.QPainter(pixmap)
painter.setCompositionMode(painter.CompositionMode_DestinationIn)
painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))
painter.end()
# make a QDrag
drag = QtGui.QDrag(self)
# put our MimeData
drag.setMimeData(mimeData)
# set its Pixmap
drag.setPixmap(pixmap)
# shift the Pixmap so that it coincides with the cursor position
drag.setHotSpot(e.pos())
# start the drag operation
# exec_ will return the accepted action from dropEvent
if drag.exec_(QtCore.Qt.LinkAction | QtCore.Qt.MoveAction) == QtCore.Qt.LinkAction:
print 'linked'
else:
print 'moved'
def mousePressEvent(self, e):
QtGui.QPushButton.mousePressEvent(self, e)
if e.button() == QtCore.Qt.LeftButton:
print 'press'
#AQUI DEBO IMPLEMENTAR EL MENU CONTEXTUAL
def dragEnterEvent(self, e):
e.accept()
def dropEvent(self, e):
# get the relative position from the mime data
mime = e.mimeData().text()
x, y = map(int, mime.split(','))
# move
# so move the dragged button (i.e. event.source())
print e.pos()
#e.source().move(e.pos()-QtCore.QPoint(x, y))
# set the drop action as LinkAction
e.setDropAction(QtCore.Qt.LinkAction)
# tell the QDrag we accepted it
e.accept()
のドラッグ・アンド・ドロップするために、私は、スタックオーバーフローからコピーしたすべてのコードです。詳細は以下のリンクを参照してください。
Drag n Drop inside QgraphicsView doesn't work (PyQt)
Drag n Drop Button and Drop-down menu PyQt/Qt designer
PyQtはやPySideはあなたを助けることができます。 PIP PySideをインストールしてPySideをインストールします。 PySideがインストールされたフォルダ "C:\ Python27 \ Lib \ site-packages \ PySide \ examples"に移動します。例があなたの仕事を手助けするかもしれません。 –
こんにちは、あなたの答えに感謝しますが、私はそれがまさに私が求めていないものだと思います。私はこれらのフレームワークを理解し、これまでに使用してきました。私の質問は、「単純なモデリングツールをドラッグアンドドロップインターフェイスを使って簡単に構築できるフレームワークはありますか?」との関連があります。 – Tooblippe
申し訳ありませんが、あなたの質問を正しく理解できません。私はあなたがQtデザイナーを認識していると思う、フォームを構築するためにドラッグアンドドロップインターフェイスを持っているQtデザイナを使用しています。 –