Set无法在1 QVBoxLayout上的2 QHBoxLayout之间设置

2024-05-18 18:22:49 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我遇到的问题是,当我运行它时,它只显示QHVerticalRight,可能是QHVerticalRight覆盖了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)

`

此图描述了LayoutAuthorIzonLeft和LayoutAuthorIzonRight的位置定义:
This image describe the position for layoutHorizonLeft and layoutHorizonRight define


Tags: selfconnecttablengnhclickedqpushbuttonaddwidget
1条回答
网友
1楼 · 发布于 2024-05-18 18:22:49

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

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

        self.setLayout(self.layoutVer)

或者,也可以使用构造函数中的小部件创建该布局:

        self.layoutVer = QHBoxLayout(self)

考虑添加一个不必要的布局:

  • layoutVerLeft是无用的,因为您可以将所有小部件添加到self.layoutHLeft
  • 对于layoutVerRight也是如此,因为您可以将所有这些小部件添加到self.layoutHRight

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

相关问题 更多 >

    热门问题