如何在另一个具有布局的小部件中插入具有自己布局的自定义PyQt5-widget?

时间:2019-08-13 12:27:19

标签: python pyqt5

我用"QHBoxLayout"编写了一个自定义窗口小部件。如果我在带有"QGridLayout"的基本窗口小部件中使用该窗口小部件,我的窗口小部件将无法正确插入。

将我的窗口小部件插入PyQtWindow并不重要-导致布局不匹配的所有可能性。

addLayout的方法addWidget中更改两个语句_insert_mywidgetPyQtWindow的顺序也没有效果。

下面是代码示例:

import sys
from PyQt5.QtWidgets import \
    QWidget, QApplication, \
    QHBoxLayout, QGridLayout, \
    QLineEdit, QLabel


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        layout = QHBoxLayout()
        self.setLayout(layout)

        self.edits = [QLineEdit(self) for _ in range(3)]
        for edit in self.edits:
            layout.addWidget(edit)


class PyQtWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Layouttest")

        # GUI (layout and 3 rows in a grid)

        layout = QGridLayout()
        self.setLayout(layout)

        self._insert_mywidget(layout)
        # insert at first

        self.labels = [QLabel("Label " + str(i+1)) for i in range(5)]
        for i, label in enumerate(self.labels):
            layout.addWidget(label, 1, i)

        # self._insert_mywidget(layout)
        # insert at 2nd

        self.edits = [QLineEdit(self) for _ in range(10)]
        for i, edit in enumerate(self.edits):
            layout.addWidget(edit, 2, i)

        # self._insert_mywidget(layout)
        # insert at last

    def _insert_mywidget(self, layout):
        self.widget = MyWidget(self)

        # add my widget
        layout.addWidget(self.widget, 0, 0, 0, 10)

        # add layout from widget
        layout.addLayout(self.widget.layout(), 0, 0, 0, 10)


# ###
# run app

app = QApplication(sys.argv)

window = PyQtWindow()
window.show()

sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

尝试一下:

import sys
from PyQt5.QtWidgets import ( QWidget, QApplication, QHBoxLayout, 
                              QGridLayout, QLineEdit, QLabel)


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        layout = QHBoxLayout()
        self.setLayout(layout)

        self.edits = [QLineEdit(self) for _ in range(3)]
        for edit in self.edits:
            layout.addWidget(edit)


class PyQtWindow(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("Layouttest")

        self.layout = QGridLayout()                                   # + self.layout
        self.setLayout(self.layout)

        self._insert_mywidget()                                       # - (layout)

        self.labels = [QLabel("Label " + str(i+1)) for i in range(5)]
        for i, label in enumerate(self.labels):
            self.layout.addWidget(label, 1, i)

        self.edits = [QLineEdit(self) for _ in range(10)]
        for i, edit in enumerate(self.edits):
            self.layout.addWidget(edit, 2, i)

    def _insert_mywidget(self):                                        # - , layout):
        self.widget = MyWidget(self)

        # add my widget
#        self.layout.addWidget(self.widget, 0, 0, 0, 10)   
        self.layout.addWidget(self.widget, 0, 0, 1, 10)                # + 1

#        layout.addLayout(self.widget.layout(), 0, 0, 0, 10)


app = QApplication(sys.argv)

window = PyQtWindow()
window.show()

sys.exit(app.exec_())

enter image description here

答案 1 :(得分:0)

好的,要点是在方法apply plugin: 'com.google.gms.google-services' googleServices { disableVersionCheck = true } 中插入一个“ 1”。这样可以获得预期的结果。无需创建layout.addWidget(self.widget, 0, 0, 1, 10)属性(@S。Nick)。

然后方法self.layout现在是:

_insert_mywidget(self, layout):

感谢@ekhumoro和@ S.Nick