问题描述
目前,我遇到的问题是当我运行它时只显示 QHVerticalRight,也许 QHVeritcalLeft 被 QHVerticalRight 覆盖了。
这是示例代码: `
class DataEntryForm(QWidget):
def __init__(self):
super().__init__()
self.layoutVerLeft = QVBoxLayout()
self.items = 0
self.flag = 0
self._data = {}
self.lstClear = ['Xóa tất cả','Lựa chọn dòng']
self.table = QTableWidget()
self.labelImage = QLabel()
self.layoutVer = QHBoxLayout()
self.layoutHLeft = QVBoxLayout()
self.layoutHRight = QVBoxLayout()
self.lineEditName = QLineEdit()
self.lineEditBirth = QDateEdit()
self.lineEditPos = QLineEdit()
self.lineEditClub = QLineEdit()
self.lineEditNumber = QLineEdit()
self.comboBoxClear = QComboBox()
self.buttonAdd = QPushButton('Thêm')
self.buttonQuit = QPushButton('Thoát')
self.buttonPlot = QPushButton('Vẽ biểu đồ')
self.buttonEdit = QPushButton('Bật/Tắt Chỉnh Sửa')
self.buttonSaveImg = QPushButton('Lưu Biểu Đồ')
self.buttonSaveFile = QPushButton('Lưu Database')
self.buttonClear = QPushButton('Xóa')
# Layout Horizontal Left
self.layoutHorizonLeft()
# Layout Horizontal Right
self.layoutHorizonRight()
self.layoutVer.addLayout(self.layoutHLeft)
self.layoutVer.addLayout(self.layoutHRight)
self.fill_table()
def layoutHorizonLeft(self):
# Define Widget as you want
self.table.setColumnCount(5)
self.table.setHorizontalHeaderLabels(('Họ và Tên','Ngày Sinh','Vị Trí','Câu Lạc Bộ','Số Áo'))
self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
# Define Vertical Box
layoutVerLeft = QVBoxLayout()
# Image add Widget
layoutVerLeft.addWidget(self.labelImage,alignment=Qt.AlignCenter)
self.labelImage.setpixmap(Qpixmap('football-manager-2021.jpg'))
# Table add Widget
layoutVerLeft.addWidget(self.table)
self.table.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
# Add Layout
self.layoutHLeft.addLayout(layoutVerLeft)
# Set Layout
self.setLayout(self.layoutHLeft)
def layoutHorizonRight(self):
# Define Verical Box
layoutVerRight = QVBoxLayout()
# Tạo combo Box clear
self.comboBoxClear.addItems(self.lstClear)
self.comboBoxClear.setEditable(True)
self.comboBoxClear.setFixedHeight(25)
lineEditComboClear = self.comboBoxClear.lineEdit()
lineEditComboClear.setAlignment(Qt.AlignCenter)
lineEditComboClear.setReadOnly(True)
# Cài đặt độ cao của các button
# self.buttonAdd.setFixedHeight(25)
# self.buttonQuit.setFixedHeight(25)
# self.buttonPlot.setFixedHeight(25)
# self.buttonEdit.setFixedHeight(25)
# self.buttonSaveImg.setFixedHeight(25)
# self.buttonSaveFile.setFixedHeight(25)
# self.buttonClear.setFixedHeight(25)
# Set button Thêm = False,để user nhập đầy đủ thông tin mới cho nhấn vào
self.buttonAdd.setEnabled(False)
# Khoảng cách giữa các khung nhập
layoutVerRight.setSpacing(5)
# Khung nhập thông tin
# Họ Tên
layoutVerRight.addWidget(QLabel('Họ và Tên'))
layoutVerRight.addWidget(self.lineEditName)
self.lineEditName.setMaxLength(25)
# Năm Sinh
layoutVerRight.addWidget(QLabel('Năm Sinh'))
layoutVerRight.addWidget(self.lineEditBirth)
self.lineEditBirth.setdisplayFormat("dd/MM/yyyy")
self.lineEditBirth.setCalendarPopup(True)
self.lineEditBirth.setMinimumDate(QDate(1900,1,1))
self.lineEditBirth.setMaximumDate(QDate(2100,1))
self.lineEditBirth.setDateTime(QtCore.QDateTime.currentDateTime())
# Vị Trí
layoutVerRight.addWidget(QLabel('Vị Trí'))
layoutVerRight.addWidget(self.lineEditPos)
self.lineEditPos.setMaxLength(20)
# Câu Lạc Bộ
layoutVerRight.addWidget(QLabel('Câu Lạc Bộ'))
layoutVerRight.addWidget(self.lineEditClub)
self.lineEditClub.setMaxLength(25)
# Số áo
layoutVerRight.addWidget(QLabel('Số Áo Thi Đấu'))
layoutVerRight.addWidget(self.lineEditNumber)
self.lineEditNumber.setValidator(QIntValidator())
self.lineEditNumber.setMaxLength(2)
# Nút nhấn lựa chọn chức năng
layoutRight_AddEdit = QHBoxLayout()
layoutRight_AddEdit.addWidget(self.buttonAdd)
layoutRight_AddEdit.addWidget(self.buttonEdit)
layoutRight_Clear = QHBoxLayout()
layoutRight_Clear.addWidget(self.comboBoxClear)
layoutRight_Clear.addWidget(self.buttonClear)
layoutRight_PlotQuit = QHBoxLayout()
layoutRight_PlotQuit.addWidget(self.buttonPlot)
layoutRight_PlotQuit.addWidget(self.buttonQuit)
layoutRight_Save = QHBoxLayout()
layoutRight_Save.addWidget(self.buttonSaveImg)
layoutRight_Save.addWidget(self.buttonSaveFile)
# Set layout theo thứ tự từ trên xuống
layoutVerRight.addLayout(layoutRight_AddEdit)
layoutVerRight.addLayout(layoutRight_Clear)
layoutVerRight.addLayout(layoutRight_Save)
layoutVerRight.addLayout(layoutRight_PlotQuit)
# chart widget
chartView = QChartView()
chartView.setRenderHint(QPainter.Antialiasing)
layoutVerRight.addWidget(chartView)
# Add Layout
self.layoutHRight.addLayout(layoutVerRight)
# Set Layout
self.setLayout(self.layoutHRight)
self.buttonQuit.clicked.connect(self.quit_message)
self.buttonPlot.clicked.connect(self.graph_chart)
self.buttonAdd.clicked.connect(self.add_entry)
self.buttonEdit.clicked.connect(self.edit_database)
self.buttonSaveImg.clicked.connect(self.export_img)
self.buttonSaveFile.clicked.connect(self.export_db_file)
self.buttonClear.clicked.connect(self.comboBox_Clear)
self.lineEditName.textChanged[str].connect(self.check_disable)
self.lineEditPos.textChanged[str].connect(self.check_disable)
self.lineEditClub.textChanged[str].connect(self.check_disable)
self.lineEditNumber.textChanged.connect(self.check_disable)
`
此图描述了 layoutHorizonLeft 和 layoutHorizonRight 定义的位置:
解决方法
您正在尝试将两个垂直布局设置为主布局,而您没有设置您需要的实际布局,即水平布局。
删除 setLayout()
和 layoutHorizonLeft
末尾的 layoutHorizonRight
,并在 __init__
末尾添加以下行。
self.setLayout(self.layoutVer)
或者,也可以使用构造函数中的小部件创建该布局:
self.layoutVer = QHBoxLayout(self)
考虑您添加了不必要的布局级别:
-
layoutVerLeft
没有用,因为您可以将所有小部件添加到self.layoutHLeft
; -
layoutVerRight
也是如此,因为您可以将所有这些小部件添加到self.layoutHRight
;
注意:不要混淆水平和垂直布局及其命名。