0

在过去的几天里,我试图解决小部件移动的问题。在某些时候,我尝试用鼠标信号重写 QComboBox 类,但没有奏效。作为一种解决方法,我决定将我的小部件作为 QGraphicsWidget 的父母,但是一旦我尝试添加另一个项目,它就不再显示,我不知道该怎么做。这是完整的测试脚本:

from PyQt4 import QtGui,  QtCore
from PyQt4.QtGui import QApplication,QGraphicsItem, QGraphicsView, QGraphicsScene, QDesktopWidget, QCheckBox, QGroupBox, QPushButton, QGridLayout, QLabel, QLineEdit, QComboBox, QFont, QRadioButton, QButtonGroup, QWidget, QShortcut, QKeySequence, QIcon, QListView, QStandardItemModel, QStandardItem, QAction, QIntValidator, QListWidget, QProgressBar, QSpacerItem
from PyQt4.QtCore import QRect
from functools import partial
import sys

class node_GUI(QtGui.QWidget):
    def __init__(self):
        super(node_GUI, self).__init__()



class Main(QtGui.QMainWindow):
    def __init__(self, *args):
        super(Main, self).__init__(*args)#QtGui.QMainWindow.__init__(self)
        self.init_defaults()
    def init_defaults(self):
        self.setGeometry(800,800,500,200)
        self.lay_main = QGridLayout()
        self.centralwidget = QtGui.QWidget()
        self.centralwidget.setLayout(self.lay_main)
        self.setCentralWidget(self.centralwidget)

        btn_create_node = QPushButton("Create Node View")
        btn_create_node.clicked.connect(self.create_node_view)
        self.lay_main.addWidget(btn_create_node)

    def showWindow(self,window):
        window.show()

    def printTest(self):
        print "Start"
        box = QGroupBox("subWidget")
        box_btn = QPushButton("Test")
        box_btn.clicked.connect(self.printTest)
        le_edit = QLineEdit()
        lay = QGridLayout()
        box.setLayout(lay)
        lay.addWidget(box_btn)
        lay.addWidget(le_edit)

        area = QtGui.QGraphicsWidget()
        area.setMinimumSize(QtCore.QSizeF(400,300))
        area.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        area.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)

        proxy = self.scene.addWidget(box)
        proxy.setParentItem(area)

        print "END"
    def create_node_view(self):
        print "creting node view"
        window = node_GUI()
        window.setGeometry(QRect(100, 100, 400, 200))
        window.setWindowTitle("node ")
        window.setObjectName("node")
        show_window = QPushButton("Show Node Editor")
        show_window.setObjectName("btn")
        show_window.clicked.connect(partial(self.showWindow,window))
        self.lay_main.addWidget(show_window)

        box = QGroupBox("Widgets")
        box_btn = QPushButton("Test")
        box_btn.clicked.connect(self.printTest)
        le_edit = QLineEdit()
        lay = QGridLayout()
        box.setLayout(lay)
        lay.addWidget(box_btn)
        lay.addWidget(le_edit)

        area = QtGui.QGraphicsWidget()
        area.setMinimumSize(QtCore.QSizeF(300,300))
        area.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        area.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)
        area.setAutoFillBackground(True)

        ecs = QtGui.QGraphicsEllipseItem()
        ecs.setRect(QtCore.QRectF(79,79,79,79))
        ecs.setFlag(QtGui.QGraphicsItem.ItemIsMovable, True)
        ecs.setFlag(QtGui.QGraphicsItem.ItemIsSelectable, True)

        view = QGraphicsView()
        self.scene = QGraphicsScene()
        self.scene.addItem(area)
        proxy = self.scene.addWidget(box)
        proxy.setParentItem(area)

        self.scene.addItem(ecs)
        view.setScene(self.scene)

        lay_window = QGridLayout()
        window.setLayout(lay_window)
        lay_window.addWidget(view)


def main():
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

当您单击“创建节点视图”>“显示节点编辑器”>“测试”按钮时,应该会出现一个新的 GroupBox,但这不起作用。不知道为什么。

4

1 回答 1

0

是的,所以我停止使用 QGraphicsWidget() 而是只使用 QGraphicsRectItem(例如,ecs),一旦我这样做了,一切都开始按预期工作。

于 2016-09-17T22:14:12.640 回答