[英]PySide2 / QLayout: Cannot add a null layout to QHBoxLayout
我想用 PySide2 創建一個 QHBoxLayout 有 2 QVBoxLayout 作為孩子。 為了便於閱讀,我想在不同的函數(left_panel 和 right_panel)中分離 2 個 QVBoxLayout。
請在下面找到腳本示例
import sys
from PySide2 import QtCore, QtGui
from PySide2.QtWidgets import (QVBoxLayout, QTableWidget, QWidget, QLabel, QLineEdit, QPushButton, QCheckBox,
QTextEdit, QGridLayout, QApplication, QAbstractItemView, QHBoxLayout)
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
mainLayout = QHBoxLayout()
mainLayout.addLayout(self.left_panel())
mainLayout.addLayout(self.right_panel())
self.setLayout(mainLayout)
def right_panel(self):
rightLayout = QVBoxLayout()
self.tableCert = QTableWidget()
self.tableCert.setColumnCount(3)
self.tableCertColumnLabels = ["First Name","Surname","login"]
self.tableCert.setRowCount(2)
self.tableCert.setHorizontalHeaderLabels(self.tableCertColumnLabels)
self.tableCert.verticalHeader().setVisible(False)
self.tableCert.horizontalHeader().setVisible(True)
rightLayout.addWidget(self.tableCert)
def left_panel(self):
pass #Here we will have the content of the other QVBoxLayout
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
問題是當我執行腳本時出現以下錯誤:
QLayout: Cannot add a null layout to QHBoxLayout
你知道為什么以及如何糾正嗎?
先感謝您。
問題很簡單: right_panel
方法不返回任何內容,或者更確切地說它返回 None,因此:
mainLayout.addLayout(self.right_panel())
等價於 Y:
mainLayout.addLayout(None)
解決方法是返回rightLayout:
def right_panel(self):
rightLayout = QVBoxLayout()
self.tableCert = QTableWidget()
self.tableCert.setColumnCount(3)
self.tableCertColumnLabels = ["First Name", "Surname", "login"]
self.tableCert.setRowCount(2)
self.tableCert.setHorizontalHeaderLabels(self.tableCertColumnLabels)
self.tableCert.verticalHeader().setVisible(False)
self.tableCert.horizontalHeader().setVisible(True)
rightLayout.addWidget(self.tableCert)
return rightLayout
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.