Re: [gtkmm] Problem with refreshing a DrawingArea in a multi-threaded program



Am 18.07.2002 21:08 schrieb(en) Johan Rutgeerts:
 Hi,

I'm having a problem realising some program with Gtkmm, and I don't
manage
to
solve it! (don't have all too much programming experience, actually, and
never
mind Gtkmm experience). Maybe one of you can help me...

I'm writing a program to visualize data from a laserscanner. I'm using a

Gtkmm::DrawingArea to draw points at coordinates that I get from the
laserscanner.

The laserscanner is implemented as an active object. It inherits from
some
class "EventSource".

For the DrawingArea, I wrote a derived class "LaserArea", which inherits
from
"DrawingArea" and "EventListener". Being an EventListener, the laserarea
has
a
member function "onEvent(Event& e)", and can be attached to an
EventSource
(in
this case the laserscanner).

Whenever the laserscanner has new data, it calls the "onEvent" of the
laserarea. This causes the new data to be copied to the laserarea, and
then
it
calls draw_laserview(), which does the actual drawing of the points:


void LaserArea::onEvent(MoRE_utilities::Event& event)
{
  _mutex.acquire();
    for(unsigned int i = 0; i < _nrOfRanges; i++ )
    {
      _range[i] =
_laserscanner->rangeDouble(i);
//_range, _cartx and _carty are arrays of doubles
      _cartx[i] = _range[i] * sin(PI/180 * double(i)/2 - _angle);
      _carty[i] = _range[i] * cos(PI/180 * double(i)/2 - _angle);
    }
  _mutex.release();

  draw_laserview();

No, don't ever draw without request from the gtk event system!
Override Gtk::DrawingErea::on_expose_event() and do all drawing there.
Call quere_draw() or queue_draw_area() whenever you need to refresh
the contents of the widget.

Also you shouldn't mess around with mutexes in you gui specific code.
The gtk event loop is quite sensitive! If you need to manipulate the
gui from within another thread look at the Glib::Dispatcher stuff. I
haven't tried it yet but it is the right way to access the one and only
gui thread with the running gtk event loop and to trigger events there.

Regards,

  Martin



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]