Medical Imaging Interaction Toolkit  2026.06.00
Medical Imaging Interaction Toolkit
mitk::CallbackFromGUIThread Class Reference

Allows threads to call some method from within the GUI thread. More...

#include <mitkCallbackFromGUIThread.h>

Public Member Functions

void CallThisFromGUIThread (itk::Command *, itk::EventObject *e=nullptr)
 Schedule the given command for execution on the GUI thread. More...
 

Static Public Member Functions

static CallbackFromGUIThread * GetInstance ()
 Return the singleton instance. More...
 
static void RegisterImplementation (CallbackFromGUIThreadImplementation *implementation)
 Register a toolkit-specific implementation. More...
 

Protected Member Functions

 CallbackFromGUIThread ()
 Hidden constructor (singleton pattern). More...
 

Detailed Description

Allows threads to call some method from within the GUI thread.

This class is useful for use with GUI toolkits that are not thread-safe, e.g. Qt. Any thread that needs to work with the GUI at some time during its execution (e.g. at the end, to display some results) can use this class to ask for a call to a member function from the GUI thread.

Usage example

We assume that you have a class ThreadedClass, that basically lives in a thread that is different from the GUI thread. Now this class has to change some element of the GUI to indicate its status. This could be dangerous (with Qt it is for sure).

The solution is, that ThreadedClass asks mitk::CallbackFromGUIThread to call a method from the GUI thread (main thread).

Here is part of the header of ThreadedClass:

class ThreadedClass : public ParentClass
{
public:
... // All you need
// This function runs in its own thread !
void ThreadedFunction();
// This should be called from the GUI thread
void ChangeGUIElementsToIndicateProgress(const itk::EventObject&);
...
};
#include <itkCommand.h>
// This method runs in a thread of its own! So it can't manipulate GUI elements directly without causing trouble
void ThreadedClass::ThreadedFunction()
{
...
// Create a command object (passing parameters comes later)
itk::ReceptorMemberCommand<ThreadedClass>::Pointer command = itk::ReceptorMemberCommand<ThreadedClass>::New();
command->SetCallbackFunction(this, &ThreadedClass::ChangeGUIElementsToIndicateProgress);
// Ask to execute that command from the GUI thread
...
}
// Do dangerous GUI changing stuff here
void ThreadedClass::ChangeGUIElementsToIndicateProgress(const itk::EventObject& e)
{
Application::GetButtonGrid()->AddButton("Stop"); // this is pseudo code
}
void CallThisFromGUIThread(itk::Command *, itk::EventObject *e=nullptr)
Schedule the given command for execution on the GUI thread.
static CallbackFromGUIThread * GetInstance()
Return the singleton instance.

This obviously won't allow you to pass parameters to ChangeGUIElementsToIndicateProgress. If you need to do that, you have to create a kind of itk::EventObject that can be asked for a parameter (this solution is not nice, if you see a better solution, please open an issue on GitHub).

The itk::EventObject has to be created with "new" (which can also be done by calling MakeObject on an existing EventObject).

const mitk::OneParameterEvent* event = new mitk::OneParameterEvent(1); // this class is not yet defined but will
be
itk::ReceptorMemberCommand<ThreadedClass>::Pointer command = itk::ReceptorMemberCommand<ThreadedClass>::New();
command->SetCallbackFunction(this, &ThreadedClass::ChangeGUIElementsToIndicateProgress);
// DO NOT delete event now. This will be done by CallThisFromGUIThread after the command will executed.
See also
CallbackFromGUIThreadImplementation CallbackEventOneParameter

Definition at line 172 of file mitkCallbackFromGUIThread.h.

Constructor & Destructor Documentation

◆ CallbackFromGUIThread()

mitk::CallbackFromGUIThread::CallbackFromGUIThread ( )
protected

Hidden constructor (singleton pattern).

Member Function Documentation

◆ CallThisFromGUIThread()

void mitk::CallbackFromGUIThread::CallThisFromGUIThread ( itk::Command *  ,
itk::EventObject *  e = nullptr 
)

Schedule the given command for execution on the GUI thread.

The optional event object can carry parameters that are passed to the command. If provided, ownership of the event is transferred and it will be deleted after the command executes.

Parameters
eOptional event object carrying parameters (may be nullptr).

◆ GetInstance()

static CallbackFromGUIThread* mitk::CallbackFromGUIThread::GetInstance ( )
static

Return the singleton instance.

Returns
Pointer to the single CallbackFromGUIThread instance.

◆ RegisterImplementation()

static void mitk::CallbackFromGUIThread::RegisterImplementation ( CallbackFromGUIThreadImplementation *  implementation)
static

Register a toolkit-specific implementation.

Must be called once by a toolkit-specific sub-class of CallbackFromGUIThreadImplementation before CallThisFromGUIThread can be used.

Parameters
implementationThe toolkit-specific implementation to register.

The documentation for this class was generated from the following file: