Created
July 5, 2023 09:24
-
-
Save wention/5c6c7a40cadec36f11a7245a5ca745e5 to your computer and use it in GitHub Desktop.
Custom qlayout base on borderlayout example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| from PyQt5.QtCore import QRect, QSize | |
| from PyQt5.QtWidgets import QMainWindow, QApplication, QLayout, QHBoxLayout, QFrame, QPushButton, QWidget, \ | |
| QWidgetItem | |
| POSITISON_HEADER = 1 | |
| POSITISON_FOOTER = 2 | |
| POSITISON_CONTENT = 3 | |
| POSITISON_LEFT_SIDE = 4 | |
| POSITISON_RIGHT_SIDE = 5 | |
| class MainLayout(QLayout): | |
| def __init__(self): | |
| super().__init__() | |
| self.items = [] | |
| def add(self, item, position): | |
| if isinstance(item, QWidget): | |
| item = QWidgetItem(item) | |
| self.items.append((item, position)) | |
| def count(self): | |
| return len(self.items) | |
| def itemAt(self, index): | |
| if index >= len(self.items): | |
| return | |
| return self.items[index][0] | |
| def _sizeHint(self, minimum=False): | |
| header_size = QSize() | |
| content_size = QSize() | |
| footer_size = QSize() | |
| left_side_size = QSize() | |
| right_side_size = QSize() | |
| for (item, position) in self.items: | |
| item_size = item.sizeHint() if not minimum else item.minimumSize() | |
| if position == POSITISON_HEADER: | |
| header_size = item_size | |
| if position == POSITISON_CONTENT: | |
| content_size = item_size | |
| elif position == POSITISON_FOOTER: | |
| footer_size = item_size | |
| elif position == POSITISON_LEFT_SIDE: | |
| left_side_size = item_size | |
| elif position == POSITISON_RIGHT_SIDE: | |
| right_side_size = item_size | |
| height = max( | |
| content_size.height(), | |
| header_size.height() + max(footer_size.height(), left_side_size.height(), right_side_size.height())) | |
| width = max( | |
| content_size.width(), | |
| header_size.width(), | |
| left_side_size.width() + footer_size.width() + right_side_size.width()) | |
| if header_size.isValid(): | |
| height += self.spacing() | |
| if footer_size.isValid(): | |
| height += self.spacing() | |
| if left_side_size.isValid(): | |
| width += self.spacing() | |
| if right_side_size.isValid(): | |
| width += self.spacing() | |
| return QSize(width, height) | |
| def sizeHint(self) -> QSize: | |
| return self._sizeHint() | |
| def minimumSize(self): | |
| return self._sizeHint(minimum=True) | |
| def setGeometry(self, r): | |
| super().setGeometry(r) | |
| header_height = 0 | |
| footer_height = 0 | |
| left_side_width = 0 | |
| right_side_width = 0 | |
| for (item, position) in self.items: | |
| if position == POSITISON_HEADER: | |
| header_height = item.sizeHint().height() | |
| elif position == POSITISON_FOOTER: | |
| footer_height = item.sizeHint().height() | |
| elif position == POSITISON_LEFT_SIDE: | |
| left_side_width = item.sizeHint().width() | |
| elif position == POSITISON_RIGHT_SIDE: | |
| right_side_width = item.sizeHint().width() | |
| for (item, position) in self.items: | |
| if position == POSITISON_HEADER: | |
| item.setGeometry(QRect(r.left(), r.top(), r.width(), item.sizeHint().height())) | |
| elif position == POSITISON_CONTENT: | |
| item.setGeometry(QRect(r.left(), r.top(), r.width(), r.height())) | |
| elif position == POSITISON_FOOTER: | |
| item.setGeometry(QRect(left_side_width + self.spacing(), | |
| r.bottom() - item.sizeHint().height(), | |
| max(item.minimumSize().width(), r.width() - left_side_width - right_side_width - self.spacing()*2), | |
| item.sizeHint().height())) | |
| elif position == POSITISON_LEFT_SIDE: | |
| item.setGeometry(QRect(r.left(), | |
| header_height + self.spacing(), | |
| item.sizeHint().width(), | |
| r.height() - header_height - self.spacing())) | |
| elif position == POSITISON_RIGHT_SIDE: | |
| item.setGeometry(QRect(r.right() - item.sizeHint().width() + 1, | |
| header_height + self.spacing(), | |
| item.sizeHint().width(), | |
| r.height() - header_height - self.spacing())) | |
| class MainWindow(QMainWindow): | |
| def __init__(self): | |
| super().__init__() | |
| left_side_bar = create_side_bar() | |
| left_side_bar.setStyleSheet("QFrame{background-color: #550000ff; border: 1px solid blue;}") | |
| right_side_bar = create_side_bar() | |
| right_side_bar.setStyleSheet("QFrame{background-color: #550000ff; border: 1px solid blue;}") | |
| top_side_bar = create_side_bar() | |
| top_side_bar.setStyleSheet("QFrame{background-color: #550000ff; border: 1px solid blue;}") | |
| bottom_side_bar = create_side_bar() | |
| bottom_side_bar.setStyleSheet("QFrame{background-color: #550000ff; border: 1px solid blue;}") | |
| center_content = create_side_bar() | |
| center_content.setStyleSheet("QFrame{background-color: #550000ff; border: 1px solid blue;}") | |
| lt = MainLayout() | |
| lt.add(center_content, POSITISON_CONTENT) | |
| lt.add(top_side_bar, POSITISON_HEADER) | |
| lt.add(left_side_bar, POSITISON_LEFT_SIDE) | |
| lt.add(right_side_bar, POSITISON_RIGHT_SIDE) | |
| lt.add(bottom_side_bar, POSITISON_FOOTER) | |
| w = QWidget() | |
| self.setStyleSheet("QMainWindow {background-color: #55ffffff;}") | |
| w.setLayout(lt) | |
| self.setCentralWidget(w) | |
| def create_side_bar(parent=None): | |
| w = QFrame(parent) | |
| lt = QHBoxLayout() | |
| btn = QPushButton("toggle") | |
| lt.addWidget(btn) | |
| w.setLayout(lt) | |
| def toggle(): | |
| if w.minimumWidth() != 100: | |
| w.setMinimumWidth(100) | |
| w.setMaximumWidth(100) | |
| else: | |
| w.setMinimumWidth(200) | |
| w.setMaximumWidth(200) | |
| print("toggle btn clicked") | |
| btn.clicked.connect(toggle) | |
| return w | |
| if __name__ == '__main__': | |
| app = QApplication(sys.argv) | |
| w = MainWindow() | |
| w.show() | |
| sys.exit(app.exec()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment