【问题标题】:Set Can not setup between 2 QHBoxLayout on 1 QVBoxLayoutSet Can not setup between 2 QHBoxLayout on 1 QVBoxLayout
【发布时间】:2021-06-25 16:16:00
【问题描述】:

目前,我遇到的问题是当我运行它时只显示 QHVerticalRight,也许 QHVerticalLeft 被 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, 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)

`

这张图片描述了 layoutHorizo​​nLeft 和 layoutHorizo​​nRight 的位置定义:

【问题讨论】:

  • 请,以供将来参考,当您要提供示例时,请确保他们的代码是 both minimal and reproducible:您的很多代码对您的问题完全没用,并且必须注释掉许多行(因为它们引用了不存在的函数)才能使其工作。在提交问题之前,请务必在单独的文件中尝试您的代码。

标签: python python-3.x pyqt5 qvboxlayout


【解决方案1】:

您尝试将两个垂直布局设置为主布局,而您没有设置您需要的实际布局,即水平布局。

删除layoutHorizonLeftlayoutHorizonRight末尾的setLayout(),并在__init__末尾添加以下行。

        self.setLayout(self.layoutVer)

或者,或者,只需在构造函数中使用小部件创建该布局:

        self.layoutVer = QHBoxLayout(self)

考虑到您添加了不必要的布局级别:

  • layoutVerLeft 没用,因为你可以将所有小部件添加到 self.layoutHLeft;
  • layoutVerRight 也一样,因为您可以将所有这些小部件添加到 self.layoutHRight

注意:不要混淆水平和垂直布局及其命名。

【讨论】:

  • 谢谢,你是对的。我误解了哪个是主要的并将“自我”放入预期的布局中。
  • @HòaNguyễnLêMinh 不客气。请记住,如果答案解决了您的问题,您应该通过单击其左侧的灰色刻度标记将其标记为已接受。此外,请始终查看您运行的程序的输出(并记住,您还应该尝试在终端或提示符中启动它们,因为 IDE 有时会错过完整的回溯甚至隐藏一些错误):在您的情况下,您会看到来自 Qt 的警报,通知您正在尝试在已经设置了一个布局的小部件上设置布局。
  • 您好,我无法设置按钮高度的配置。请帮我检查这个问题。 self.buttonAdd.setFixedHeight(25) ==> 这个按钮只有默认设置
  • @michaelkenshijpv97 你能试着澄清你的意思吗?如果您可以添加指向您所获得内容的屏幕截图的链接,这可能会很有用。
  • 嗨,很抱歉我的回复晚了。我为此解决了我的问题和想法。非常感谢。
猜你喜欢
  • 2012-09-20
  • 2015-04-12
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2022-12-02
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多