如何在 pyqt 的 table 单元格内垂直居中小部件?
How to vertically center widgets inside a table cell in pyqt?
我向 QFormLayout 添加了一些小部件,然后将此布局添加到 table 中的单元格,但似乎小部件与顶部对齐,我希望它们垂直居中,因此它们位于中心,这是一张图片解释 :
制作布局及其小部件的class:
class Options(QtWidgets.QWidget):
def __init__(self, parent=None,row=[], ops=[]):
super(Options, self).__init__(parent)
self.i = random.randint(1, 100)
self.row = [j.decode("utf-8") if i!=1 else "image" for i,j in enumerate(row)]
self.layout = QFormLayout()
ops = ast.literal_eval(ops[0]) if len(ops[0])>1 else {}
for i,j in ops.items():
#adding widgets here and it is not related!
self.btn = QtWidgets.QPushButton("Submit")
self.btn.clicked.connect(self.do)
self.layout.addWidget(self.btn)
self.setLayout(self.layout)
设置 QFormLayout 的垂直对齐方式:
self.layout.setFormAlignment(QtCore.Qt.AlignVCenter)
通常最好避免覆盖现有实例属性:self.layout()
是所有 QWidget 后代的默认函数;您要么使用局部变量,要么使用不同的名称。
我向 QFormLayout 添加了一些小部件,然后将此布局添加到 table 中的单元格,但似乎小部件与顶部对齐,我希望它们垂直居中,因此它们位于中心,这是一张图片解释 :
制作布局及其小部件的class:
class Options(QtWidgets.QWidget):
def __init__(self, parent=None,row=[], ops=[]):
super(Options, self).__init__(parent)
self.i = random.randint(1, 100)
self.row = [j.decode("utf-8") if i!=1 else "image" for i,j in enumerate(row)]
self.layout = QFormLayout()
ops = ast.literal_eval(ops[0]) if len(ops[0])>1 else {}
for i,j in ops.items():
#adding widgets here and it is not related!
self.btn = QtWidgets.QPushButton("Submit")
self.btn.clicked.connect(self.do)
self.layout.addWidget(self.btn)
self.setLayout(self.layout)
设置 QFormLayout 的垂直对齐方式:
self.layout.setFormAlignment(QtCore.Qt.AlignVCenter)
通常最好避免覆盖现有实例属性:self.layout()
是所有 QWidget 后代的默认函数;您要么使用局部变量,要么使用不同的名称。