Percival
Percival

Reputation: 29

How can i set a menusettings checkable, true or false? and if it's true do something

I made a simple ui with pyqt5 autocompiler, everything works but i need to autoexecute a script, the autoexecution works but i will to implement it to an activate/disabled button (QAction set to checkable) located in the settings menu. I want to set to him a state (T or F) and a boolean return if pressed

I'm on Windows

with open("./startup.txt", "r")as f:
    f_contents= f.readline()
    startupis=bool(f_contents)
..some code....
self.actionRun_at_startup.triggered.connect(self.runatstartup)
self.actionRun_at_startup.setCheckable(startupis)

def runatstartup (self, checked):
   if(startupis==True):
        ....
   else:
      ....
   ....some code...

Upvotes: 0

Views: 161

Answers (1)

musicamante
musicamante

Reputation: 48335

It's not very clear if you want the user to trigger the bool state or just keep that value.

In the first case, this will return the new value of the checkbox (if it was False, will return True once it's triggered, and viceversa):

self.actionRun_at_startup.setCheckable(True)
self.actionRun_at_startup.setChecked(startupis)

If you want to keep a variable, use QAction.setData()

    # ...
    self.actionRun_at_startup.setData(startupis)
    # ...

def runatstartup(self):
    if self.actionRun_at_startup.data() == True:
        # ...

Upvotes: 1

Related Questions