繁体   English   中英

替换QFormLayout中的行时PySide崩溃

[英]PySide crashing when replacing rows in QFormLayout

使用以下代码示例PySide片段会在按下“添加”,“添加”,“删除”,“添加”时以及由于其他一些交互序列而出现段错误。

  • 的Python:2.7.6
  • PySide:1.2.1
  • QtCore的:4.8.5

码:

from PySide.QtGui import *
from PySide.QtCore import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setObjectName('MainWindow')
        self.baseLayout = QWidget(self)
        self.v_layout = QVBoxLayout(self.baseLayout)
        self.setCentralWidget(self.baseLayout)
        self.form_layout = QFormLayout(self.baseLayout)
        self.v_layout.addLayout(self.form_layout)
        self.button_add = QPushButton(self.baseLayout)
        self.button_add.setText("Add")
        self.v_layout.addWidget(self.button_add)
        self.button_del = QPushButton(self.baseLayout)
        self.button_del.setText("Remove")
        self.v_layout.addWidget(self.button_del)
        self.button_add.clicked.connect(self.add)
        self.button_del.clicked.connect(self.remove)

        self.fields = []

    def add_item(self):
        layout = QHBoxLayout(self.parent())
        line = QLineEdit(self.parent())
        slider = QSlider(self.parent())
        layout.addWidget(line)
        layout.addWidget(slider)
        self.fields.append((layout, line, slider))
        self.form_layout.addRow("Test", layout)

    def add(self):
        for i in range(15):
            self.add_item()

    def remove(self):
        for (layout, line, slider) in self.fields:
            line.deleteLater()
            slider.deleteLater()
        while self.form_layout.itemAt(0):
            child = self.form_layout.takeAt(0)
            if child.widget():
                child.widget().deleteLater()
        self.form_layout.update()
        self.fields = []


def main():
    import sys
    app = QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    app.exec_()

if __name__ == '__main__':
    main()

这是将复合窗口小部件(在本例中是QSliderQLineEditQVBoxLayout )添加到表单布局的正确方法吗? 我究竟做错了什么?

QFormLayout添加“复合控件”的正确方法是创建一个QWidget ,该QFormLayout将成为该布局的父级。

add_item()应该看起来像这样:

def add_item(self):
    widget = QWidget(self.parent())
    layout = QHBoxLayout(widget)
    line = QLineEdit(widget)
    slider = QSlider(widget)
    layout.addWidget(line)
    layout.addWidget(slider)
    self.fields.append((layout, widget, line, slider))
    self.form_layout.addRow("Test", widget)

(并且删除字段时,也必须删除该小部件)。

我认为您没有以正确的方式创建布局,例如,您尝试两次设置base_layout的布局。 您也可以在QLayout上检查count() ,以查看是否有子级:

from PySide.QtGui import *
from PySide.QtCore import *

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.baseLayout = QWidget(self)
        self.v_layout = QVBoxLayout(self.baseLayout)
        self.setCentralWidget(self.baseLayout)

        self.form_layout = QFormLayout()
        self.v_layout.addLayout(self.form_layout)

        self.button_add = QPushButton()
        self.button_add.setText("Add")
        self.v_layout.addWidget(self.button_add)

        self.button_del = QPushButton()
        self.button_del.setText("Remove")
        self.v_layout.addWidget(self.button_del)

        self.button_add.clicked.connect(self.add)
        self.button_del.clicked.connect(self.remove)

        self.fields = []

    def add_item(self):
        layout = QHBoxLayout()
        line = QLineEdit()
        slider = QSlider()
        layout.addWidget(line)
        layout.addWidget(slider)
        self.fields.append((layout, line, slider))
        self.form_layout.addRow("Test", layout)

    def add(self):
        for i in range(15):
            self.add_item()

    def remove(self):

        while self.form_layout.count() > 0:
            child = self.form_layout.takeAt(0)
            widget = child.widget()
            if widget:
                widget.deleteLater()

        self.form_layout.update()
        self.fields = []


def main():
    import sys
    app = QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    app.exec_()

if __name__ == '__main__':
    main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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