I have the following method which establishes a QVBoxLayout with several configuration widgets:
def ConfigureNewGraph(self):
confWnd = QWidget()
confWndLayout = QVBoxLayout()
confWnd.setLayout(confWndLayout)
xAxisLabel = QLineEdit()
xAxisRange = QLineEdit()
yAxisLabel = QLineEdit()
yAxisRange = QLineEdit()
graphTitle = QLineEdit()
#Group box as selector for the input type: via file or via port.
dataStreamBox = QGroupBox("Input type")
serialPortCheck = QCheckBox("Serial port")
textFileCheck = QCheckBox("File")
dataStreamLayout = QFormLayout()
dataStreamLayout.addWidget(serialPortCheck)
dataStreamLayout.addWidget(textFileCheck)
dataStreamBox.setLayout(dataStreamLayout)
confWndLayout.addWidget(dataStreamBox)
#Serial port input handler
serialConfig = SerialConfig()
dataStreamLayout.addWidget(serialConfig.serialBox)
#File input handler
fileConfig = FileConfig()
dataStreamLayout.addWidget(fileConfig.fileBox)
#Widget to configure plot info
plotInfoBox = QGroupBox("Plot information")
plotInfoLayout = QFormLayout()
plotInfoLayout.addRow("Plot title: ",graphTitle)
plotInfoLayout.addRow("x axis label: ",xAxisLabel)
plotInfoLayout.addRow("x axis range:",xAxisRange)
plotInfoLayout.addRow("y axis label: ",yAxisLabel)
plotInfoLayout.addRow("y axis range:",yAxisRange)
plotInfoBox.setLayout(plotInfoLayout)
confWndLayout.addWidget(plotInfoBox)
serialPortCheck.toggled.connect(lambda checked: serialConfig.serialBox.setVisible(checked))#Shows the portBox
serialConfig.connectButton.clicked.connect(lambda: sc.ConnectToPort(serialConfig.baudRateInput.text(),serialConfig.portInput.text(),serialConfig.dataSizeInput.text(),serialConfig.connectionState))
textFileCheck.toggled.connect(lambda checked: fileConfig.fileBox.setVisible(checked))#Shows the fileBox
self.mainLayout.addWidget(confWnd)
And here is the class that implements the behavior for the SerialPort configuration:
class SerialConfig():
def __init__(self):
#Serial port configuration, can be expanded
self.serialBox = QGroupBox()
self.serialBoxLayout = QVBoxLayout()
self.portInput = QLineEdit()
self.baudRateInput = QLineEdit()
self.dataSizeInput = QLineEdit()
self.connectionStateLabel = QLabel("Connection status")
#self.connectionStateWidget
self.connectionState = QTextEdit()
self.connectionState.setReadOnly(True)
self.connectionState.setFixedSize(350,80)
self.serialInfoWidget = QWidget()
self.serialInfoLayout = QFormLayout()
self.serialInfoWidget.setLayout(self.serialInfoLayout)
self.serialInfoLayout.addRow("Port:",self.portInput)
self.serialInfoLayout.addRow("Baud rate:",self.baudRateInput)
self.serialInfoLayout.addRow("Size of data package (in bytes):",self.dataSizeInput)
self.connectButton = QPushButton("Connect")
#Layout order
self.serialBoxLayout.addWidget(self.serialInfoWidget)
self.serialBoxLayout.addWidget(self.connectButton)
self.serialBoxLayout.addWidget(self.connectionStateLabel)
self.serialBoxLayout.addWidget(self.connectionState)
self.serialBox.setLayout(self.serialBoxLayout)
self.serialBox.setVisible(False)
This is how it looks on the default size (QSize(800,600)) after running the application:
And this is how it looks like after maxing the window:
Is there any way to keep the distances within widgets when I resize my window?
I’m quite new to PyQt so any other comment is welcome. Thank you!