Set Can not setup between 2 QHBoxLayout on 1 QVBoxLayout

285 Views Asked by At

Currently, I met the problem is when I run it only shows QHVerticalRight, maybeQHVeritcalLeft was overwritten by QHVerticalRight.

Here is the sample code for that: `

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)

`

This image describe the position for layoutHorizonLeft and layoutHorizonRight define:
This image describe the position for layoutHorizonLeft and layoutHorizonRight define

1

There are 1 best solutions below

6
On

You're trying to set the two vertical layouts as main layouts, and you're not setting the actual layout you need, the horizontal one.

Remove both setLayout() at the end of layoutHorizonLeft and layoutHorizonRight, and add the following line at the end of the __init__.

        self.setLayout(self.layoutVer)

Or, alternatively, just create that layout with the widget in the constructor:

        self.layoutVer = QHBoxLayout(self)

Consider that you're adding an unnecessary level of layouts:

  • layoutVerLeft is useless, as you can add all widgets to self.layoutHLeft;
  • the same for layoutVerRight, since you can add all those widgets to self.layoutHRight;

Note: don't confuse horizontal and vertical layouts and their namings.