Isotropix Forums

PYQt singleton prob

Clarisse Scripting related topics

PYQt singleton prob

Unread postby guydubost » Thu Jan 02, 2020 7:12 pm

Hi There
I m building a PyQt app to submit renders on our farm.
I was able to start my PyQt app from a custom menu item.
menu.add_command("RFX>Farm submit", "path to python script")
now if i run my script a second time , a second app Gui pops up and i dont want that
I thought first that i could store a global var and store my GUI in it so second time this var would nt be None .
but seems like python scripts run in a different python session so that global var are just global to the current python script but not shared among different python scripts.

I found a workaround with the linux command wmctrl where i can retrieve the pid of my tool window and check if it match current pid of clarisse , so i know the tool is already running

Do u have soamthing in mind that could avoid the above workaround ?
guydubost
 
Posts: 4
Joined: Fri Aug 09, 2019 10:37 pm

Re: PYQt singleton prob

Unread postby dcourtois » Fri Jan 03, 2020 10:01 am

Hi,

If I understand correctly you just want to prevent your menu item to create the same window multiple times ? In this case, you can get the top level widgets from Qt and check if you have one matching your window. Something like this:

python code

# check for your window by title (or anything else)
found = False
for widget in QApplication.topLevelWidgets():
if widget.windowTitle() == "farm_submitter":
# show the window if it was hidden
if widget.isHidden():
widget.show()
found = True

if found is False:
# create the window


Full reference: https://doc.qt.io/qtforpython/PySide2/Q ... velWidgets
User avatar
dcourtois
 
Posts: 83
Joined: Tue Jul 25, 2017 3:15 pm

Re: PYQt singleton prob

Unread postby guydubost » Fri Jan 03, 2020 6:12 pm

good find. works as well
guydubost
 
Posts: 4
Joined: Fri Aug 09, 2019 10:37 pm

Re: PYQt singleton prob

Unread postby mati » Tue Jan 07, 2020 4:51 am

If your global variable is located in another python module found in PYTHONPATH, it should work. It looks like an imported module stays in memory and is not reloaded. Here's an example of a shelf button or menu item script:

Code: Select all
import test_module

test_module.called_count += 1
print("This shelf button has been clicked {} times".format(test_module.called_count))


test_module just contains:
Code: Select all
called_count = 0

I can click on the shelf button or menu item multiple times in a Clarisse session and see that the counter increases.

For Qt, I keep track of the instance on the window class itself and clean it up before launching a new window.

Code: Select all
class MyWindow(QMainWindow):
    _instance = None
    def __init__(self, parent, **kwargs):
        if MyWindow._instance is not None:
            MyWindow._instance.close()
            MyWindow._instance = None
        super(MyWindow, self).__init__(parent, **kwargs)
mati
 
Posts: 53
Joined: Fri Apr 19, 2019 8:35 pm


Return to Scripting