2

I got the following Test-GUI.

There is a left layout and a right layout where i want to put buttons and other things onto. The button on the right should make a QFrame unhide or hide and all the widgets in it. This works.

But after the first two clicks, the layout is different. The TableWidget on the left layout gets resized and the button is a bit more south.

Is there an easy way to fix this?

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class MainWindow(QMainWindow):

    def __init__(self):
        super().__init__()

        self.main_widget = MainWidget()
        self.setCentralWidget(self.main_widget)
        self.show()


class MainWidget(QWidget):
    def __init__(self):
        super().__init__()

        self.layout = QVBoxLayout()

        self.tab_widget = TabWidget()

        self.debugger = Dialog()

        self.layout.addWidget(self.tab_widget)
        self.layout.addWidget(self.debugger)

        self.setLayout(self.layout)


class TabWidget(QTabWidget):
    def __init__(self):
        super().__init__()
        self.tab1 = Tab_1()
        self.addTab(self.tab1, "Tab1")


class Tab_1(QWidget):

    def __init__(self):
        super().__init__()

        # LEFT LAYOUT BEGIN
        self.table = QTableWidget()
        self.table.setRowCount(1)
        self.table.setColumnCount(2)
        self.table.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
        self.table.resizeColumnsToContents()

        left_hlayout = QHBoxLayout()
        left_hlayout.addWidget(self.table)

        #       # LEFT LAYOUT END
        #
        #       # RIGHT LAYOUT BEGIN

        self.button_options = QPushButton('Options')
        self.button_options.setCheckable(True)
        self.button_options.toggled.connect(self.option_pressed)
        right_vlayout = QVBoxLayout()
        right_vlayout.addWidget(self.button_options)

        #       # RIGHT LAYOUT END

        # MAIN LAYOUT BEGING

        self.main_layout = QVBoxLayout()
        self.horizontal_layout = QHBoxLayout()
        self.horizontal_layout.addLayout(left_hlayout)
        self.horizontal_layout.addLayout(right_vlayout)
        self.main_layout.addLayout(self.horizontal_layout)
        self.option = Options()
        self.main_layout.addWidget(self.option)
        self.setLayout(self.main_layout)

    # MAIN LAYOUT END

    def option_pressed(self):
        if self.button_options.isChecked():
            self.option.setVisible(True)
        else:
            self.option.setVisible(False)


class Options(QFrame):
    def __init__(self):
        super().__init__()
        self.hide()
        self.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
        self.options_layout = QFormLayout()
        self.options_label = QLabel('One')
        self.options_lineedit = QLineEdit('Two')
        self.options_layout.addRow(self.options_label, self.options_lineedit)
        self.setLayout(self.options_layout)


class Dialog(QPlainTextEdit):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedHeight(100)
        pal = QPalette()
        bgc = QColor(210, 210, 210)
        pal.setColor(QPalette.Base, bgc)
        self.setPalette(pal)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

0

Browse other questions tagged or ask your own question.