vizkit3d
QtThreadedWidget.hpp
Go to the documentation of this file.
1 #ifndef __ENVIEW_QTTHREADEDWIDGET__
2 #define __ENVIEW_QTTHREADEDWIDGET__
3 
4 #include <QApplication>
5 
6 #include <boost/thread/thread.hpp>
7 #include <boost/thread/mutex.hpp>
8 #include <boost/thread/condition_variable.hpp>
9 
11 {
12  int argc;
13  char **argv;
14  boost::shared_ptr<QApplication> app;
15 
16  boost::mutex mut;
17  boost::condition_variable cond;
18  boost::thread gui_thread;
19  bool running;
20  bool restart;
21  bool destroy;
22 
23 public:
25  virtual ~QtThreadedWidgetBase();
26 
27  virtual void start();
28  virtual void stop();
29  bool isRunning();
30  QWidget* getWidget();
31 
32 protected:
33  QWidget *widget;
34  void run();
35  virtual QWidget* createWidget() = 0;
36 };
37 
38 template <class T>
40 {
41 public:
42  QWidget* createWidget()
43  {
44  return new T();
45  }
46 
47  T *getWidget()
48  {
49  if(!isRunning())
50  {
51  throw std::runtime_error("Tried to access widget before thread was started");
52  }
53 
54  return dynamic_cast<T*>(QtThreadedWidgetBase::getWidget());
55  }
56 };
57 
58 
59 #endif
virtual ~QtThreadedWidgetBase()
Definition: QtThreadedWidget.cpp:11
QWidget * createWidget()
Definition: QtThreadedWidget.hpp:42
QWidget * widget
Definition: QtThreadedWidget.hpp:33
bool isRunning()
Definition: QtThreadedWidget.cpp:64
QtThreadedWidgetBase()
Definition: QtThreadedWidget.cpp:6
QWidget * getWidget()
Definition: QtThreadedWidget.cpp:70
Definition: QtThreadedWidget.hpp:39
T * getWidget()
Definition: QtThreadedWidget.hpp:47
virtual void stop()
Definition: QtThreadedWidget.cpp:52
void run()
Definition: QtThreadedWidget.cpp:75
Definition: QtThreadedWidget.hpp:10
virtual QWidget * createWidget()=0
virtual void start()
Definition: QtThreadedWidget.cpp:26