python-3.x - PySide2 / QLayout:无法向 QHBoxLayout 添加空布局
问题描述
我想用 PySide2 创建一个 QHBoxLayout,它有 2 个 QVBoxLayout 作为子级。为了便于阅读,我想将 2 个 QVBoxLayout 分隔为不同的功能(left_panel 和 right_panel)。
请在下面找到脚本示例
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
推荐阅读
- ionic3 - 离子 JWT 生成
- python - 使用一个数据帧输出在另一个数据帧中查找匹配行
- python - 服务器内部远程调试
- powerbi - Power BI 订阅历史
- java - Jersey 2 - 根据请求标头注册响应过滤器
- c# - 实现 AbpIdentity cookie 和 JwtBearer
- deployment - 如何在外部检查 Kubernetes 的部署准备情况?
- python - 如何阻止 apply() 更改列的顺序?
- ms-word - 在给定文件夹中的 word 文档中查找或替换字符串
- mongodb - 将 mongodb 与 spring 一起使用时发出“Found cycle for field”