RE: [ROOT] TQtWidget question

From: Valeri Fine (fine@bnl.gov)
Date: Tue Nov 09 2004 - 18:57:56 MET


Dear Sergei,

First of all thank your very much for your example and screenshot.
 
> Hi,
> 
> I'm  developing a histogram browser for MINOS online monitoring system,
> using Qt ROOT layer - btw I think it's great that it has been included in
> the standard ROOT distribution. It has been very helpful (at least for me)
> I've used Qt designer to create gui main app window with two ROOT canvases
> embedded as TQtWidgets - a screenshot of how the browser looks is in
> 
> http://www.slac.stanford.edu/~avva/OmHistory.png 
> 
> The question I have - I can't figure out how to process mouse clicks in
> TQtWidget embedded ROOT canvases - I'd like to make it possible to select
> a certain histogram bin by clicking on it. Is there any example/tutorial
> around that shows how to do it?

  There are several ways to do what you want.

  1. The "embedded" TCanvas is just a ROOT TCanvas object and by this reason the
regular TExec class approach (see: http://root.cern.ch/root/htmldoc/TExec.html)
should work for you. Try to make the "embedded TCanvas" to be the "current one" by
clicking over that canvas as usually with the "middle mouse button" (ROOT
approach) or with TQtWidget::cd() method (Qt layer approach) and then start some
macros from the $ROOTSYS/tutorials directory like "exec1.C", "exec2.C" or
"exec3.C" and then  move the mouse across your TCanvas.

  2. If you need more fine grain manipulation, then you may have regarded the Qt
approach also.

The Qt solution is to install so-called "Event Filer" for the TQtWidget object.

http://doc.trolltech.com/3.3/qobject.html#eventFilter 
http://doc.trolltech.com/3.3/qobject.html#installEventFilter 
http://doc.trolltech.com/3.3/qevent.html  
http://doc.trolltech.com/3.3/qmouseevent.html 

It should look like the Qt docs suggests:

1. You should create a C++ subclass of QObject to re-implement the virtual
"eventFilter" method 

class MousePressCatcher : public QObject
    {
        ...
    protected:
        bool eventFilter( QObject *o, QEvent *e );
    };

    bool MousePressCatcher::eventFilter( QObject *o, QEvent *e )
    {
        if ( e->type() == QEvent:: MouseButtonPress) {
            // special processing for the TCanvas Qt mouse press event
            QMouseEvent *k = (QMouseEvent *)e;
            qDebug( "Catching the mouse press  key press %d", k->x(),k->y() );
        }
        //   standard ROOT event processing
        //  (do not return TRUE unless you know what you are doing)
        return FALSE;
    }

2. You should instantiate this class somewhere in your code and install it as
TQtWidget event filter.

        MousePressCatcher *myMouse = new MousePressCatcher ( this );
        TQtWidget *histogramWindow = new TQtWidget( this );

        histogramWindow ->installEventFilter(myMouse );

  The eventFilter method is to be called for ALL QEvent's sent to your TQtWidget
obejct. The example above shows how to catch the "MouseButtonPress" events only,
but you can add there the custom response to any Qt event (see: list of the
events: http://doc.trolltech.com/3.3/qevent.html) 

  From the "eventFilter method above you are free to call any ROOT class methods

  Be aware that returning TRUE value the "eventFilter" method will effectively
block the regular ROOT event flaw and this way suspend the regular ROOT mouse
event response, which may be "good" or "bad" depends of your goal.

In addition you may want to apply 

http://doc.trolltech.com/3.3/qwidget.html#setMouseTracking  

method if you want to catch the mouse movement with no button pressed over the
"embedded" TCanvas object.

Please, let me know how it works.
Let me know whether you need a further explanation.

   Best regards, Valeri


> Thanks,
> 
> Sergei Avvakumov



This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:10 MET