Jump to content

PyQt Single Instance


csp

Recommended Posts

Hi,

 

I have python script using PyQt ui based on SESI's recommended example. As is, I will have unlimited instances when I hit shelf tool. How is possible to keep a single instance open. Either deleting previous instance and opening a fresh one or blocking a new instance if there is already one open.

 

My last question is how to quit script/ui once using a button. Let's say "Open Hip", first command to open a new hip and then quit the tool.

 

cheers,

Link to comment
Share on other sites

import hou
from PySide import QtCore as qtc
from PySide import QtGui as qtg

#----------------------------------------------------------------------------------------

try:        app = qtg.QApplication(['houdini'])
except:     app = qtg.QApplication.instance()

event_loop = qtc.QEventLoop()

#----------------------------------------------------------------------------------------

def qt_callback():
    if not ctl.isVisible():
        try:    hou.ui.removeEventLoopCallback( qt_callback )
        except: pass
    event_loop.processEvents()
    app.sendPostedEvents(None, 0)

#----------------------------------------------------------------------------------------

ctl = None
def show( ctl_ ):
    global ctl
    ctl = ctl_
    ctl.show()
    hou.ui.addEventLoopCallback( qt_callback )

#----------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------

button_widget = qtg.QPushButton('Hello World !!!')
button_widget.clicked.connect( button_widget.hide )
button_widget.setWindowFlags( qtc.Qt.FramelessWindowHint | qtc.Qt.WindowStaysOnTopHint )
show(button_widget)

1. This simple one-widget solution use one QApplication instance.

2. You can connect clicked button signal to hide widget. Hide widget with no parents leads to finish script

Edited by Alexey Vanzhula
Link to comment
Share on other sites

Thanks for your replies Alexey. I have hard time to make your code work inside my script. Here is my working script.

I am bringing in the ui with loadUi. The hide function will not work, probably I am using it wrong. The Qt.WindowStaysOnTopHint is working in my mac but I am using PySide there. At work's linux I can't get it stay on top. And most important the single instance.

import hou, os
import pyqt_houdini
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *

app = QApplication.instance()
if app is None:
        app = QApplication(['houdini'])
        
projectsDir = '/jobs'
pwd = os.path.dirname(__file__)


class StartShot(QWidget):
    def __init__(self, parent=None):
        super(StartShot, self).__init__(parent)

        # ui loader
        self.ui = loadUi(pwd+'/ui/open.ui', self)

        # get parameters
        self.filter = self.ui.findChild(QLineEdit, 'filter')
        self.project = self.ui.findChild(QComboBox, 'project')
        self.shot = self.ui.findChild(QComboBox, 'shot')
        self.hip = self.ui.findChild(QComboBox, 'hip')
        self.open = self.ui.findChild(QPushButton, 'load')
        
        # initial function
        self.getProject()
        
        # set connections
        self.filter.textChanged.connect(self.getProject)
        self.project.currentIndexChanged.connect(self.getShot)
        self.shot.currentIndexChanged.connect(self.getHip)
        self.open.clicked.connect(self.loadHip)
        
        self.ui.setWindowFlags(Qt.WindowStaysOnTopHint) #will not work in Linux
        self.ui.show()
        
    def getProject(self):
        #code
        
    def getShot(self):
        #code

    def getHip(self):
        #code

    def loadHip(self):
        project = str(self.project.currentText())
        shot = str(self.shot.currentText())
        job = os.path.join(projectsDir, project, shot)
        hip = job+'/hip/'+str(self.hip.currentText())
        if os.path.isfile(hip):
            hou.hipFile.load(hip)
            hou.hscript('setenv JOB = %s'%job)
            self.hide #will not work but no error

def runApp():
    dialog = StartShot()
    pyqt_houdini.exec_(app, dialog)
Link to comment
Share on other sites

1. I this case there is no difference between PySide and PyQt4.

2. For linux there is some situations where you need to setup your window manager for WindowStaysOnTopHint.

3. I never used uic and Designer for build ui -  just manually layouting controls. It is my firm position :)

4. This solution works well for me and my linux desktop. But you can also try this http://paulwinex.blogspot.com/2014/06/pyside-helper-for-houdini.html

Edited by Alexey Vanzhula
Link to comment
Share on other sites

  • 2 weeks later...

hello stevegh, there is nothing wrong with the script as is. I just wanted to have some functionalities, as single instance and close window on button clicked, which I can live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...