PyQt app in seperate thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anders

    PyQt app in seperate thread

    I am writing a plugin for a piece of software in python, and I want to
    start up a PyQt GUI in the plugin, without stalling the main thread
    while the gui is running (later i will want to pass messages between
    the main thread and the gui thread).

    I'm new to pyqt, so I'm probably doing something very silly, but for
    the moment I'm just trying to get the first pyqt tutorial example
    running in a seperate thread:

    ----8<--------

    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore

    class MyThread( QtCore.QThread ):
    def __init__( self ):
    QtCore.QThread. __init__( self )

    def run( self ):
    app = QtGui.QApplicat ion( sys.argv )
    hello = QtGui.QPushButt on( 'Hello world!' )
    hello.resize( 500, 500 )
    hello.show()
    app.exec_()
    QtCore.QThread. terminate( )



    mt = MyThread()
    mt.start()
    print 'Main thread continuing...'
    mt.wait()
    print 'GUI thread finished.'

    ----8<--------

    The app starts up (with a warning WARNING: QApplication was not created
    in the main() thread. ). I get a window, but no button, and clicking on
    the close button does nothing and I have to force the program to quit.

    There's got to be a way to get this working, right? Can anyone help me
    along the right path?

    Cheers,

    Anders

  • Phil Thompson

    #2
    Re: PyQt app in seperate thread

    On Wednesday 22 November 2006 12:37 pm, anders wrote:
    I am writing a plugin for a piece of software in python, and I want to
    start up a PyQt GUI in the plugin, without stalling the main thread
    while the gui is running (later i will want to pass messages between
    the main thread and the gui thread).
    >
    I'm new to pyqt, so I'm probably doing something very silly, but for
    the moment I'm just trying to get the first pyqt tutorial example
    running in a seperate thread:
    >
    ----8<--------
    >
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    >
    class MyThread( QtCore.QThread ):
    def __init__( self ):
    QtCore.QThread. __init__( self )
    >
    def run( self ):
    app = QtGui.QApplicat ion( sys.argv )
    hello = QtGui.QPushButt on( 'Hello world!' )
    hello.resize( 500, 500 )
    hello.show()
    app.exec_()
    QtCore.QThread. terminate( )
    >
    >
    >
    mt = MyThread()
    mt.start()
    print 'Main thread continuing...'
    mt.wait()
    print 'GUI thread finished.'
    >
    ----8<--------
    >
    The app starts up (with a warning WARNING: QApplication was not created
    in the main() thread. ). I get a window, but no button, and clicking on
    the close button does nothing and I have to force the program to quit.
    >
    There's got to be a way to get this working, right? Can anyone help me
    along the right path?
    Read http://doc.trolltech.com/4.2/threads.html

    In particular the bit that says that exec_() must be called from the main
    thread and not from a QThread.

    Phil

    Comment

    • anders

      #3
      Re: PyQt app in seperate thread


      Phil Thompson wrote:
      On Wednesday 22 November 2006 12:37 pm, anders wrote:
      I am writing a plugin for a piece of software in python, and I want to
      start up a PyQt GUI in the plugin, without stalling the main thread
      while the gui is running (later i will want to pass messages between
      the main thread and the gui thread).

      I'm new to pyqt, so I'm probably doing something very silly, but for
      the moment I'm just trying to get the first pyqt tutorial example
      running in a seperate thread:

      ----8<--------

      import sys
      from PyQt4 import QtGui
      from PyQt4 import QtCore

      class MyThread( QtCore.QThread ):
      def __init__( self ):
      QtCore.QThread. __init__( self )

      def run( self ):
      app = QtGui.QApplicat ion( sys.argv )
      hello = QtGui.QPushButt on( 'Hello world!' )
      hello.resize( 500, 500 )
      hello.show()
      app.exec_()
      QtCore.QThread. terminate( )



      mt = MyThread()
      mt.start()
      print 'Main thread continuing...'
      mt.wait()
      print 'GUI thread finished.'

      ----8<--------

      The app starts up (with a warning WARNING: QApplication was not created
      in the main() thread. ). I get a window, but no button, and clicking on
      the close button does nothing and I have to force the program to quit.

      There's got to be a way to get this working, right? Can anyone help me
      along the right path?
      >
      Read http://doc.trolltech.com/4.2/threads.html
      >
      In particular the bit that says that exec_() must be called from the main
      thread and not from a QThread.
      >
      Phil

      OK so that's all good... so how do I go about doing what I want then?
      Can I set up a window in the second thread and start its event loop
      without running the event loop in the core app?

      Comment

      • Phil Thompson

        #4
        Re: PyQt app in seperate thread

        On Wednesday 22 November 2006 2:06 pm, anders wrote:
        Phil Thompson wrote:
        On Wednesday 22 November 2006 12:37 pm, anders wrote:
        I am writing a plugin for a piece of software in python, and I want to
        start up a PyQt GUI in the plugin, without stalling the main thread
        while the gui is running (later i will want to pass messages between
        the main thread and the gui thread).
        >
        I'm new to pyqt, so I'm probably doing something very silly, but for
        the moment I'm just trying to get the first pyqt tutorial example
        running in a seperate thread:
        >
        ----8<--------
        >
        import sys
        from PyQt4 import QtGui
        from PyQt4 import QtCore
        >
        class MyThread( QtCore.QThread ):
        def __init__( self ):
        QtCore.QThread. __init__( self )
        >
        def run( self ):
        app = QtGui.QApplicat ion( sys.argv )
        hello = QtGui.QPushButt on( 'Hello world!' )
        hello.resize( 500, 500 )
        hello.show()
        app.exec_()
        QtCore.QThread. terminate( )
        >
        >
        >
        mt = MyThread()
        mt.start()
        print 'Main thread continuing...'
        mt.wait()
        print 'GUI thread finished.'
        >
        ----8<--------
        >
        The app starts up (with a warning WARNING: QApplication was not created
        in the main() thread. ). I get a window, but no button, and clicking on
        the close button does nothing and I have to force the program to quit.
        >
        There's got to be a way to get this working, right? Can anyone help me
        along the right path?
        Read http://doc.trolltech.com/4.2/threads.html

        In particular the bit that says that exec_() must be called from the main
        thread and not from a QThread.

        Phil
        >
        OK so that's all good... so how do I go about doing what I want then?
        Can I set up a window in the second thread and start its event loop
        without running the event loop in the core app?
        No. Read the second sentence of the paragraph I referred to. Also read the
        section "QObject Reentrancy".

        Phil

        Comment

        • anders

          #5
          Re: PyQt app in seperate thread


          Phil Thompson wrote:
          On Wednesday 22 November 2006 2:06 pm, anders wrote:
          Phil Thompson wrote:
          On Wednesday 22 November 2006 12:37 pm, anders wrote:
          I am writing a plugin for a piece of software in python, and I want to
          start up a PyQt GUI in the plugin, without stalling the main thread
          while the gui is running (later i will want to pass messages between
          the main thread and the gui thread).

          I'm new to pyqt, so I'm probably doing something very silly, but for
          the moment I'm just trying to get the first pyqt tutorial example
          running in a seperate thread:

          ----8<--------

          import sys
          from PyQt4 import QtGui
          from PyQt4 import QtCore

          class MyThread( QtCore.QThread ):
          def __init__( self ):
          QtCore.QThread. __init__( self )

          def run( self ):
          app = QtGui.QApplicat ion( sys.argv )
          hello = QtGui.QPushButt on( 'Hello world!' )
          hello.resize( 500, 500 )
          hello.show()
          app.exec_()
          QtCore.QThread. terminate( )



          mt = MyThread()
          mt.start()
          print 'Main thread continuing...'
          mt.wait()
          print 'GUI thread finished.'

          ----8<--------

          The app starts up (with a warning WARNING: QApplication was not created
          in the main() thread. ). I get a window, but no button, and clicking on
          the close button does nothing and I have to force the program to quit.

          There's got to be a way to get this working, right? Can anyone help me
          along the right path?
          >
          Read http://doc.trolltech.com/4.2/threads.html
          >
          In particular the bit that says that exec_() must be called from the main
          thread and not from a QThread.
          >
          Phil
          OK so that's all good... so how do I go about doing what I want then?
          Can I set up a window in the second thread and start its event loop
          without running the event loop in the core app?
          >
          No. Read the second sentence of the paragraph I referred to. Also read the
          section "QObject Reentrancy".
          >
          Phil
          OK I see that now. Thanks for pointing that out. So basically, I can't
          do what I want at all. That's a bit of a pain. Is there no way of
          tricking Qt into thinking I'm running it in the main thread?

          A

          Comment

          • Chris Mellon

            #6
            Re: PyQt app in seperate thread

            On 22 Nov 2006 06:43:55 -0800, anders <anderslangland s@gmail.comwrot e:
            >
            Phil Thompson wrote:
            On Wednesday 22 November 2006 2:06 pm, anders wrote:
            Phil Thompson wrote:
            On Wednesday 22 November 2006 12:37 pm, anders wrote:
            I am writing a plugin for a piece of software in python, and I want to
            start up a PyQt GUI in the plugin, without stalling the main thread
            while the gui is running (later i will want to pass messages between
            the main thread and the gui thread).
            >
            I'm new to pyqt, so I'm probably doing something very silly, but for
            the moment I'm just trying to get the first pyqt tutorial example
            running in a seperate thread:
            >
            ----8<--------
            >
            import sys
            from PyQt4 import QtGui
            from PyQt4 import QtCore
            >
            class MyThread( QtCore.QThread ):
            def __init__( self ):
            QtCore.QThread. __init__( self )
            >
            def run( self ):
            app = QtGui.QApplicat ion( sys.argv )
            hello = QtGui.QPushButt on( 'Hello world!' )
            hello.resize( 500, 500 )
            hello.show()
            app.exec_()
            QtCore.QThread. terminate( )
            >
            >
            >
            mt = MyThread()
            mt.start()
            print 'Main thread continuing...'
            mt.wait()
            print 'GUI thread finished.'
            >
            ----8<--------
            >
            The app starts up (with a warning WARNING: QApplication was not created
            in the main() thread. ). I get a window, but no button, and clicking on
            the close button does nothing and I have to force the program to quit.
            >
            There's got to be a way to get this working, right? Can anyone help me
            along the right path?

            Read http://doc.trolltech.com/4.2/threads.html

            In particular the bit that says that exec_() must be called from the main
            thread and not from a QThread.

            Phil
            >
            OK so that's all good... so how do I go about doing what I want then?
            Can I set up a window in the second thread and start its event loop
            without running the event loop in the core app?
            No. Read the second sentence of the paragraph I referred to. Also read the
            section "QObject Reentrancy".

            Phil
            >
            OK I see that now. Thanks for pointing that out. So basically, I can't
            do what I want at all. That's a bit of a pain. Is there no way of
            tricking Qt into thinking I'm running it in the main thread?
            >
            It's possible with hackery from C++ but I seriously doubt you can do
            it from PyQt.

            Comment

            • Diez B. Roggisch

              #7
              Re: PyQt app in seperate thread

              OK I see that now. Thanks for pointing that out. So basically, I can't
              do what I want at all. That's a bit of a pain. Is there no way of
              tricking Qt into thinking I'm running it in the main thread?
              Maybe you can either invert the thread-roles - that is, run your "main"
              application in a thread, and if needed start the Qt-thing, or you might
              consider spawning a process and using pyro. Which will work very neat, done
              so myself.

              Diez

              Comment

              • anders

                #8
                Re: PyQt app in seperate thread


                Diez B. Roggisch wrote:
                OK I see that now. Thanks for pointing that out. So basically, I can't
                do what I want at all. That's a bit of a pain. Is there no way of
                tricking Qt into thinking I'm running it in the main thread?
                >
                Maybe you can either invert the thread-roles - that is, run your "main"
                application in a thread, and if needed start the Qt-thing, or you might
                consider spawning a process and using pyro. Which will work very neat, done
                so myself.
                >
                Diez
                Yeah I was thinking that's going to have to be the way to go... I can't
                run the main app in a child thread, so I'll have to spawn the GUI as a
                seperate process and communicate with it. Didn't know about pyro
                though, thanks for the link. You've used it successfully with PyQt in a
                seperate process?

                Cheers,

                Anders

                Comment

                • Diez B. Roggisch

                  #9
                  Re: PyQt app in seperate thread

                  anders wrote:
                  >
                  Diez B. Roggisch wrote:
                  OK I see that now. Thanks for pointing that out. So basically, I can't
                  do what I want at all. That's a bit of a pain. Is there no way of
                  tricking Qt into thinking I'm running it in the main thread?
                  >>
                  >Maybe you can either invert the thread-roles - that is, run your "main"
                  >application in a thread, and if needed start the Qt-thing, or you might
                  >consider spawning a process and using pyro. Which will work very neat,
                  >done so myself.
                  >>
                  >Diez
                  >
                  Yeah I was thinking that's going to have to be the way to go... I can't
                  run the main app in a child thread, so I'll have to spawn the GUI as a
                  seperate process and communicate with it. Didn't know about pyro
                  though, thanks for the link. You've used it successfully with PyQt in a
                  seperate process?
                  Yup. I used it to spawn a python interpreter in a subprocess, and
                  communicate some stuff between that and my main GUI app. The idea was to
                  have a scriptable application, which allowed you could kill the script.

                  Diez

                  Comment

                  • Jeremy Sanders

                    #10
                    Re: PyQt app in seperate thread

                    anders wrote:
                    OK I see that now. Thanks for pointing that out. So basically, I can't
                    do what I want at all. That's a bit of a pain. Is there no way of
                    tricking Qt into thinking I'm running it in the main thread?
                    I have an app which runs Qt in a separate thread and allows the user to send
                    it python commands from the main thread. Have a look at this code to see
                    how it works:



                    Jeremy

                    --
                    Jeremy Sanders

                    Comment

                    Working...