nabs
nabs

Reputation: 35

GridLayout position in PyQt

I have the following problem, because i want the frame for my "Enter IP-address" to have the same size as the three other frames below it, which is "Open file/Save file", "parameter" and "hyppighed". And i want my "start log" frame to go all the way to the top level, but this is how it is showed:

Output

And here is the code:

class myWindow(QWidget):
    def __init__(self):
        super(myWindow, self).__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("Unisab 3 Modbuslogger")
        self.setWindowIcon(QtGui.QIcon("logger\johnsoncontrols.png"))
        self.createLayout()
        self.showMaximized()

    def createLayout(self):
        gridLayout = QGridLayout()
        vlayout5 = QHBoxLayout()

        self.frame1_custom = ConfigurationWidget()
        gridLayout.addWidget(self.frame1_custom, 0, 0, 1, 1)

        self.frame2_custom = SaveOpenWidget()
        gridLayout.addWidget(self.frame2_custom, 1, 0, 1, 1)

        self.frame3_custom = ParameterWidget()
        gridLayout.addWidget(self.frame3_custom, 2, 0, 1, 1)

        self.frame4_custom = HyppighedWidget()
        gridLayout.addWidget(self.frame4_custom, 3, 0, 1, 1)

        self.frame5_custom = StartLogWidget()
        gridLayout.addWidget(self.frame5_custom, 0, 1, 4, 4)

        self.setLayout(gridLayout)


class ConfigurationWidget(QFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.frame1 = self
        self.frame1.setFrameShape(self.StyledPanel)
        self.vlayout = QVBoxLayout()

        self.labelIP = QLabel("Enter IP-address:", self)
        self.labelIP.setFont(QFont("Times", 12))
        self.lineeditIP = QLineEdit(self)
        self.lineeditIP.setFont(QFont("Times", 12))
        self.labelIPConfirm = QLabel(self)
        self.labelIPConfirm.setFont(QFont("Times", 12))
        self.lineeditIP.returnPressed.connect(self.labelIPpressed)
        self.vlayout.addWidget(self.labelIP)
        self.vlayout.addWidget(self.lineeditIP)
        self.vlayout.addWidget(self.labelIPConfirm)
        self.frame1.setLayout(self.vlayout)

    def labelIPpressed(self):
        response = os.popen(f"ping {self.lineeditIP.text()}").read()
        if "Received = 4" in response:
           self.labelIPConfirm.setText(f"UP {self.lineeditIP.text()} Ping 
           Successful")
        else:
             self.labelIPConfirm.setText(f"DOWN {self.lineeditIP.text()} 
             Ping Unsuccesful")


class SaveOpenWidget(QFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.frame2 = self
        self.frame2.setFrameShape(self.StyledPanel)
        self.hlayout = QHBoxLayout()
        self.widget = QWidget()

        self.buttonSave = QPushButton("Save file", self)
        self.buttonSave.setFont(QFont("Times", 12))
        self.buttonSave.clicked.connect(self.SaveFileDialog)
        self.buttonOpen = QPushButton("Open file", self)
        self.buttonOpen.setFont(QFont("Times", 12))
        self.buttonOpen.clicked.connect(self.OpenFileDialog)
        self.hlayout.addWidget(self.buttonOpen)
        self.hlayout.addWidget(self.buttonSave)
        self.frame2.setLayout(self.hlayout)

    def SaveFileDialog(self):
        option = QFileDialog.Options()
        file = QFileDialog.getSaveFileName(self.widget, "Save File Window 
        Title", "default.txt", "All Files (*)", options=option)
        print(file[0])

    def OpenFileDialog(self):
        option = QFileDialog.Options()
        file = QFileDialog.getOpenFileName(self.widget, "Open file window 
        title", "Default file", "All Files (*)", options = option)
        print(file)

class ParameterWidget(QFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.frame3 = self
        self.frame3.setFrameShape(self.StyledPanel)
        self.vlayout = QVBoxLayout()

        self.buttonParameter = QPushButton("Parameter", self)
        self.vlayout.addWidget(self.buttonParameter)
        self.frame3.setLayout(self.vlayout)

class HyppighedWidget(QFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.frame4 = self
        self.frame4.setFrameShape(self.StyledPanel)
        self.vlayout = QVBoxLayout()

        self.buttonHyppighed = QPushButton("Hyppighed", self)
        self.vlayout.addWidget(self.buttonHyppighed)
        self.frame4.setLayout(self.vlayout)

class StartLogWidget(QFrame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.frame5 = self
        self.frame5.setFrameShape(self.StyledPanel)
        self.vlayout = QVBoxLayout()

        self.buttonLog = QPushButton("Start log", self)
        self.vlayout.addWidget(self.buttonLog)
        self.frame5.setLayout(self.vlayout)


def window():
app = QApplication(sys.argv)
win = myWindow()
win.show()
sys.exit(app.exec_())


window()

And i thought it would make sense to give the first frame, which is ConfigurationWidget() the coordinates(0, 0, 1, 1), but then the GUI is showed like this, which is wrong, because i want the frames to left to have the size as they had on the first picture:

Output2

Upvotes: 1

Views: 983

Answers (1)

eyllanesc
eyllanesc

Reputation: 243907

You have to set the stretch factor of the second column to 1:

gridLayout.setColumnStretch(1, 1)

Upvotes: 1

Related Questions