下面的示例是我根据Pyqt5 网格扩展小部件中的答案修改的,其开头如下:
然后,当您单击“列封面”按钮时,第一个标签“第 0 行,第 0 列”(以红色勾勒出)会在 QGridLayout 的两列上展开 - 但是,我得到了这样的信息:
因此,为了使轮廓标签居中,似乎考虑了两个网格列的宽度,这很好 - 但是,轮廓标签并没有扩展以占据两列的总宽度。
单击“列覆盖”按钮时,如何让标签扩展宽度并占据网格布局中两列的空间?即,我希望在程序启动后第一次单击按钮后获得此信息(在图像编辑器中编辑,框大约是我认为轮廓下方两个标签的边界框的并集)。
请注意,我知道我可以通过操纵来做到这一点.setFixedWidth
-MainUi.label
但是,我正在寻找某种设置,其中标签会自动扩展以填充可用宽度,具体取决于它是放置在 QGridLayout 的一列还是两列中。
代码:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui, QtWidgets
#rom example import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(191, 136)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 2)
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 3, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setObjectName("label_4")
self.gridLayout.addWidget(self.label_4, 2, 1, 1, 1)
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setObjectName("label_5")
self.gridLayout.addWidget(self.label_5, 3, 1, 1, 1)
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setObjectName("label_6")
self.gridLayout.addWidget(self.label_6, 1, 1, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 1, 0, 1, 1)
self.label.setStyleSheet("border: 1px solid; border-color:red;")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setObjectName("label_2")
self.gridLayout.addWidget(self.label_2, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Column Cover"))
self.label_3.setText(_translate("MainWindow", "Row 2, Column 0"))
self.label_4.setText(_translate("MainWindow", "Row 1, Column 1"))
self.label_5.setText(_translate("MainWindow", "Row 2, Column 1"))
self.label_6.setText(_translate("MainWindow", "Row 0, Column 1"))
self.label.setText(_translate("MainWindow", "Row 0, Column 0"))
self.label_2.setText(_translate("MainWindow", "Row 1 ,Column 0 "))
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
QWidget.__init__(self, parent)
self.MainUi = Ui_MainWindow()
self.MainUi.setupUi(self)
self.MainUi.pushButton.setCheckable(True) # +++ True
self.MainUi.pushButton.clicked.connect(self.expand_row)
def expand_row(self, state): # +++ state
if state:
self.MainUi.label_6.hide()
self.MainUi.gridLayout.addWidget(self.MainUi.label, 1, 0, 1, 2, Qt.AlignHCenter) # ...2, 1)
else:
self.MainUi.gridLayout.addWidget(self.MainUi.label, 1, 0, 1, 1) # ...1, 1)
self.MainUi.label_6.show()
# self.MainUi.gridLayout.setRowStretch(0,1)
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.setWindowTitle('Example')
MainWindow.show()
sys.exit(app.exec_())
将对齐标志设置为 ,
QLabel.setAlignment
而不是QLayout.addWidget
。