linuxqtvariablesfunctional-programminggambas

How can I set the initial value of a Switch Button on a Gambas Qt application?


I'm developing an small application written in Gambas with a Qt interface, and as an initial learning test I wanted to write a really simple control panel to start/stop a few services like apache2, mysql, webmin etc.

The application would look something like this:

enter image description here

My problem is, I have no idea how to set the initial position of the Apache button On or Off depending on the state of the service. I can extract the value from the system with sudo service apache2 status and some tr/cut/grep magic to a variable. But the interface only offers me the option of setting the value as True or False. I don't know where to place that code. The only segment I've managed to make work is the start/stop OnClick event, but the initial position should be set before the click. This code works only when the service status matches the button status on starting the panel.

Here's a segment of the code, if it helps:

Public Sub SwitchButton1_Click()
  Dim apachestatus As String
  Shell "sudo service apache2 status | grep Active | tr -s ' ' | cut -d ' ' -f 3" To apachestatus

  If apachestatus = "active\n" Then
    SwitchButton1.Value = False
    Shell "sudo service apache2 stop"
  Else If apachestatus = "inactive\n" Then
    SwitchButton1.Value = True
    Shell "sudo service apache2 start"
  Endif

  Message.Info("apache : " & apachestatus)
End

Any help would be appreciated.


Solution

  • You need to do the status check inside Form_Open():

    Public Sub Form_Open()
        ...
        If apachestatus = "active\n" Then
          SwitchButton1.Value = False
        Else If apachestatus = "inactive\n" Then
          SwitchButton1.Value = True
        Endif
    End