简体   繁体   中英

PyQt4: How to reorder child widgets?

I want to implement a GUI program like the blueprint editor in the Unreal game engine with PyQt4. Here is an example of the blueprint editor:

在此输入图像描述

First I create a simple container widget to place all the components(The rectangles). Then I use the QPainterPath widget to draw lines to connect the components. Since the users can place the components wherever they want by drag and drop, I choose absolute positioning in my container widget. Here is the sample code:

class Component(QtGui.QWidget):
    """A simple rectangle."""
    def __init__(self, type_, parent=None):
        super(Component, self).__init__(parent)
        ...

class BezierPath(QtGui.QWidget):

    def __init__(self, start, end, parent=None):
        super(BezierPath, self).__init__(parent)
        self.setMinimumSize(300, 500)
        self._start = start
        self._end = end
        self._path = self._generate_path(self._start, self._end)
        self.setMinimumSize(
            abs(start.x()-end.x()), abs(start.y()-end.y()))

    def _generate_path(self, start, end):
        path = QtGui.QPainterPath()
        path.moveTo(start)
        central_x = (start.x()+end.x())*0.5
        path.cubicTo(
            QtCore.QPointF(central_x, start.y()),
            QtCore.QPointF(central_x, end.y()),
            end)
        return path

    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self)
        pen = QtGui.QPen()
        pen.setWidth(3)
        painter.setPen(pen)
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        painter.drawPath(self._path)
        painter.end()

class Container(QtGui.QWidget):
    def add_component(self, component_type, position):
        component = Component(component_type, self)
        component.move(position)

    def add_connection(self, component_a, component_b):
        bezier_path = BezierPath(component_a.pos(), component_b.pose(), self)

The problem is I want to lines show underneath the components, but the components are created first. Is there a way I can reorder the child widgets of the Container of should I use a better way to organize the components?

I found a solution for reordering the child widgets myself. I'll mark this answer as accepted for now. It's not a perfect solution so if there is a better answer I will accept it then. Anyway here is the solution:

In qt4 the widget has a method raise_() , here I quote:

to raises this widget to the top of the parent widget's stack. After this call the widget will be visually in front of any overlapping sibling widgets.

So if you want to reorder all your widgets, first you keep the references of all the child widgets in you own list or what container you choose. Reorder all the widgets in you own container, then call raise_() for each widget in reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM