[Qt-interest] Display QWidget in foreground without taking focus and events

Zeljko zeljko at holobit.net
Fri Apr 15 10:35:27 CEST 2011


Bertrand ROOS wrote:

> Hi all,
> 
> I'am trying to display a QWidget in foreground of a parent QWidget,
> without taking focus and events.
> I take an example to explain :
> I have a QTextBrowser where I want to display a transparent picture in
> foreground, but I want the user to be able to select text behind.
> 
> I tried to overload the event fonction of the picture (a QWidget) to
> return false, and then all event will be processed by the QTextBrowser.
> I show you a minimal code source example :
> void Picture::event(QEvent * e)
> {
>      if(e->type() == QEvent::Paint)
>      {
>          return true;
>      }
>      else
>      {
>          return false;
>      }
> }
> 
> 
> I also tried to use an event filter to redirect event to the
> QTextbrowser by using a eventFilter using the following code :
> TextBrowser::TextBrowser(QWidget * parent) : QTextBrowser(parent)
> {
>      Picture = new MyPicture(this);
> 
>      QGridLayout* layout = new QGridLayout(this);
>      layout->addWidget(Picture,0,0,1,1);
>      setLayout(layout);
> 
>      Logo->installEventFilter(this);
>      Logo->setFocusProxy(this);
> }
> 
> bool TextBrowser::eventFilter(QObject *obj, QEvent *event)
> {
>      if (obj == Picture)
>      {
>          if (event->type() == QEvent::Paint)
>          {
>              return false;
>          }
>          else
>          {
>              return QTextBrowser::eventFilter(obj, event);
>          }
>      }
>      else
>      {
>          return QTextBrowser::eventFilter(obj, event);
>      }
> }
> 
> I don't know if it is my implementation that is wrong, or if I must use
> another function which will fit better.
> If someone could give me an advice, I would be pleased.

Set needed attributes (FocusOnActivate or similar) over your widget and focus 
policy (Qt::NoFocus). Then it shouldn't take focus in any case, but can react on 
mouse events (eg click ), so you must stop that events only.

zeljko

> 
> Best regards.




More information about the Qt-interest-old mailing list