[Paraview] Plugin with own thread

Utkarsh Ayachit utkarsh.ayachit at kitware.com
Wed Mar 9 11:25:26 EST 2011


You might want to read
http://doc.qt.nokia.com/latest/threads-qobject.html especially the
section about signal/slots and threads.

In your code, contrary to what you expect, Trigger() is being called
in your thread and not the "main thread" and hence the issues you
might be seeing. If you simply change the connection to use a
Qt::QueuedConnection you'd get the desired effect.

Utkarsh

On Wed, Mar 9, 2011 at 8:10 AM, Stephan Rogge
<Stephan.Rogge at tu-cottbus.de> wrote:
> Hello,
>
>
>
> after reading the tutorials and How-to’s for writing ParaView Plugins I’ve
> started with the Autostart-Example project. My goal was to create a plugin
> which is executed in an external thread. It might happens that I need this
> in the near future.
>
>
>
> Well, it works quit well, I guess. But to avoid some errors and
> miss-behaviors I need a confirmation that I am on the right way.
>
>
>
> My approach is to use the signal-slot-pattern to communicate between the
> main thread of ParaView and my own plugin-thread. The class
> pqMyApplicationStarter represents the ParaView-plugin (which is running in
> the main thread of ParaView?). There is an object in this class which
> executes a loop within an external thread ( class Thread ), which extends
> QThread.
>
>
>
> As I read somewhere that vtk isn’t implemented threaded-safe at all (at
> least the rendering stuff) I decided to do all render calls in
> pqMyApplicationStarter. When the UpdatedData()signal is fired the active
> view needs to re-rendered.
>
>
>
> The question I want to ask is very simple: Is this the right / best way to
> create a thread and communicate with it via signals?
>
>
>
>
>
> /////////////// Thread.h ////////////////////////////
>
> class Thread : public QThread
>
> {
>
> Q_OBJECT
>
> public:
>
> Thread(QObject* parent = 0);
>
> virtual ~Thread();
>
> virtual void run();
>
> signals:
>
>       void UpdatedData();
>
> };
>
>
>
>
>
>
>
> void Thread::run()
>
> {
>
>       running = true;
>
>
>
>       while( running )
>
>       {
>
> // Sleep awhile
>
>             msleep( 800 );
>
>             // Fire the signal
>
>             emit UpdatedData();
>
>       }
>
> }
>
> /////////////////////////////////////////////////////
>
>
>
>
>
> /////////////// pqMyApplicationStarter //////////////
>
> class pqMyApplicationStarter : public QObject
>
> {
>
>       Q_OBJECT
>
>       typedef QObject Superclass;
>
>
>
> public:
>
>       pqMyApplicationStarter(QObject* p=0);
>
>       ~pqMyApplicationStarter();
>
>
>
>       // Callback for startup.
>
>       void onStartup();
>
>
>
> public slots:
>
>       void Trigger();
>
> };
>
>
>
>
>
>
>
> void pqMyApplicationStarter::onStartup()
>
> {
>
>       myThread = new Thread();
>
>       myThread->start();
>
>
>
>       pqActiveObjects::instance().connect( myThread, SIGNAL(UpdatedData()),
> this, SLOT(Trigger()) );
>
> }
>
>
>
> void pqMyApplicationStarter::Trigger()
>
> {
>
>       pqActiveObjects* object = &(pqActiveObjects::instance());
>
>       pqView* view            = object->activeView();
>
>
>
>       if (view)
>
>       {
>
>             view->render();
>
>       }
>
>
>
>       view  = NULL;
>
> }
>
>
>
>
>
> Thanks a lot.
>
>
>
> Cheers,
>
> Stephan
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the ParaView Wiki at:
> http://paraview.org/Wiki/ParaView
>
> Follow this link to subscribe/unsubscribe:
> http://www.paraview.org/mailman/listinfo/paraview
>
>


More information about the ParaView mailing list