I forgot to mention. This approach is also platform independent, and you
don't have to mess with window handles outside of the qt event loop.


On Mon, Jan 6, 2014 at 10:11 PM, Justin Israel <[email protected]>wrote:

> Check out this older article. It may give you some ideas on how to rethink
> the solution to the problem:
>
> http://developer.nokia.com/Community/Wiki/Run_only_one_instance_of_a_Qt_application
>
> An interesting modification that you might try to the socket approach is
> to actually accept a "raise_()" signal over the socket to tell the existing
> one to show and raise to the top:
>
>
>    1. First instance of the application starts up and creates its local
>    socket. It wait on this socket (using signal/slot) for a message
>    2. Second instance tries to start, and checks if it can create the
>    local socket. It will fail because the socket exists already
>    3. Before the second instance quits, it can connect to the second (as
>    opposed to trying to bind it) and send the signal to raise the existing one
>
>
>
>
> On Mon, Jan 6, 2014 at 9:42 PM, Marcus Ottosson <[email protected]>wrote:
>
>> Good idea.
>>
>> The act of showing the window doesn't seem to produce any events, but
>> there then is a QEnterEvent being triggered when entering it with the
>> cursor after being shown via win32 (no leave event), along with events
>> hovering the native part of the ui, such as the titlebar, as well as
>> QMouseEvents when clicking within the static area.
>>
>> I tried putting a call to show() within event() when isinstance(event,
>> QEnterEvent) == True and the UI came back to life when entering. Getting
>> closer!
>>
>> Is there a way to trigger such a signal via win32 you think?
>>
>> In Nathan's post <http://nathanhorne.com/?p=485> about getting a QWidget
>> instance via a pointer, it seems I should be able to manipulate the window
>> as though it were a local python object, doing stuff like calling show()
>> myself. However I can't figure out how to get a useful pointer for that
>> method. Could that be something to persue?
>>
>> I did see mention of creating a singleton for an application somewhere on
>> Google, so perhaps the problem is bigger than it seems.
>>
>>
>>
>>
>> On 6 January 2014 07:59, Justin Israel <[email protected]> wrote:
>>
>>> No I suppose you are right. That probably is a platform specific
>>> solution to create a singleton application. Can you re-implement the
>>> event() method on your window, to print and examine if it is receiving any
>>> kind of events when you manipulate the visibility from win32?
>>>
>>>
>>> On Mon, Jan 6, 2014 at 8:46 PM, Marcus Ottosson 
>>> <[email protected]>wrote:
>>>
>>>> Right, I completely forgot a use case.
>>>>
>>>> I'm looking to bring up an existing application if one exists when the
>>>> application is being run twice, such as a launcher type application. If the
>>>> launcher is already up, I'd like to restore it, rather than create a new
>>>> instance.
>>>>
>>>> There might be a better way?
>>>>
>>>>
>>>> On 5 January 2014 23:03, Justin Israel <[email protected]> wrote:
>>>>
>>>>> Maybe something to do with widget events not being generated (stab in
>>>>> the dark) when you go through the window system instead of qt?
>>>>> Out of curiosity, what is the use case for controlling the windows
>>>>> this way?
>>>>>
>>>>>
>>>>> On Mon, Jan 6, 2014 at 11:28 AM, Marcus Ottosson <
>>>>> [email protected]> wrote:
>>>>>
>>>>>> This might very well be a too specific question for this group and is
>>>>>> outside of Maya, and I suspect Justin you are more on the Linux side of
>>>>>> Python, and its more than reasonable to assume the problem is not unique 
>>>>>> to
>>>>>> Qt, but I figure asking couldn't hurt.
>>>>>>
>>>>>> In Maya, I would fetch a handle to the child window by traversing the
>>>>>> existing handles of the parent window and finally run .show(), but how 
>>>>>> does
>>>>>> this work on top-level windows?
>>>>>>
>>>>>> So I'm hiding a PyQt main window and showing it using the pywin32
>>>>>> library with its method win32gui.ShowWindow
>>>>>> win32gui.ShowWindow(hsnd, win32con.SW_SHOW)
>>>>>>
>>>>>> Which I presume is the equivalent of the ctypes (for those who would
>>>>>> rather use it over pywin32)
>>>>>> ctypes.windll.user32.ShowWindow(hsnd, win32con.SW_SHOW)
>>>>>>
>>>>>> The window shows up, but I suspect the event loop is still on hold as
>>>>>> there is no interacting with the window, its a complete zombie.
>>>>>>
>>>>>> How would I return control to the QApplication event loop (if that
>>>>>> indeed is the problem) after showing it via the Win32 API?
>>>>>>
>>>>>> Here is some sample code (you need 
>>>>>> pywin32<http://sourceforge.net/projects/pywin32/> installed
>>>>>> and you need to be on Windows). Minimize and re-run the code to have the
>>>>>> window pop back up. Uncomment closeEvent to make it hide when you try and
>>>>>> close it. Re-running the code this time will bring up a static window 
>>>>>> with
>>>>>> no ability to interact.
>>>>>>
>>>>>> import win32gui, win32con
>>>>>>
>>>>>> from PyQt5.QtWidgets import *
>>>>>> from PyQt5.QtCore import *
>>>>>> from PyQt5.QtGui import *
>>>>>>
>>>>>> class Window(QWidget):
>>>>>>     def __init__(self, parent=None):
>>>>>>         super(Window, self).__init__(parent)
>>>>>>         self.setWindowTitle('Test Window')
>>>>>>         self.resize(300, 200)
>>>>>>
>>>>>>         QPushButton('Test Button', self)
>>>>>>
>>>>>>     # def closeEvent(self, event):
>>>>>>     #     self.hide()
>>>>>>     #     event.ignore()
>>>>>>
>>>>>>
>>>>>> def new_window():
>>>>>>     import sys
>>>>>>     app = QApplication(sys.argv)
>>>>>>     win = Window()
>>>>>>     win.show()
>>>>>>     sys.exit(app.exec_())
>>>>>>
>>>>>>
>>>>>> HWND = None
>>>>>> def foreach_window(hwnd, lParam):
>>>>>>     if 'Test Window' == win32gui.GetWindowText(hwnd):
>>>>>>         global HWND
>>>>>>         HWND = hwnd
>>>>>>     return True
>>>>>>
>>>>>>
>>>>>> win32gui.EnumWindows(foreach_window, 0)
>>>>>>
>>>>>>
>>>>>> if HWND:
>>>>>>     print "Restoring existing Window"
>>>>>>     win32gui.ShowWindow(HWND, win32con.SW_RESTORE)
>>>>>> else:
>>>>>>     print "No existing Window found"
>>>>>>     new_window()
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>  --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msgid/python_inside_maya/f094bfcc-a222-4173-b1f0-62b2780ef7db%40googlegroups.com
>>>>>> .
>>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>>
>>>>>
>>>>>  --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Python Programming for Autodesk Maya" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>>  To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA03e3NL3KrWOfhPV0EPkbw8q5_LDO6qoJnE5JJEOzceLg%40mail.gmail.com
>>>>> .
>>>>>
>>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> *Marcus Ottosson*
>>>> [email protected]
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Python Programming for Autodesk Maya" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>>  To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCBy1bmH1vJrGbP%3DHRK1YpH9V_6g0UeqtiTA75_u0Hh-A%40mail.gmail.com
>>>> .
>>>>
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>  To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3h2-QiSdCzoEDAnSbq%2BrC_0_OOVBB4gzatKUfn9TbnmA%40mail.gmail.com
>>> .
>>>
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>>
>>
>> --
>> *Marcus Ottosson*
>> [email protected]
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>>  To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBXTnQ9O8bTO9YKSmYKFNowuTUyP87QkwzGFeV1pHTD0A%40mail.gmail.com
>> .
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1qFeeBCt%3DoL9_rwYzMjHz2sTKGOuX1yA%2BLR1OfF8PjqA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to