1

I have do some research about QMouseEvents but I am stuck about passing a QMouseEvent to child widgets. I have a program with a structure like this :

MainWindow > DockWidget > WidgetList > WidgetTarget

MainWindow is parent of DockWidget etc... My primary goal is to know when I clicked in MainWindow and pass this QMouseEvent to WidgetTarget.

I read the doc about QMouseEvent and see the useful functions ignore() of QEvent but it does the contrary of what I want. The QMouseEvent is passed to the parent, so If I clik in WidgetTarget, the QMousseEvent will be pass to WidgetList.

So is there a way to pass a QMousseEvent to a child widget instead of its parent widget ? I have see some trick with the flag Qt::WA_TransparentForMouseEvents but I don't know if this is a correct way

EDIT : I will put some detail about the work in WidgetTarget with mousePressEvent(QMouseEvent *event). It basicaly to make a eyedropper. Here the code I have in mind :

 void WidgetTarget::mousePressEvent(QMouseEvent *event)
    {
        if(eyeDropperActivated) //true when clicked on button eyedropper
        {
           QLabel *label = (QLabel*)MainWindow->childAt(event->x(),event->y());
           QColor color; //Get the pixel value at x,y event from the QLabel pixmap
           setColor(color) //Set the color parameter of WidgetTarget                                                                                              
        }

    }

I read your useful comments and yes I think It will be easy If I implement this code on MainWindow, but the eyedropper function is in WidgetTarget, so basically I have to find way to activate the eyedropper in WidgetTarget, check in the MainWindow if the eyedropper is activated, and send after to the WidgetTarget a signal with a QColor for example ?

Best Regards

user3627590
  • 281
  • 1
  • 4
  • 10
  • You realize, that mouse coordinates stored in the event will be out of range of the child widget (most likely), do you? Anyway, I think the simplest way is to just handle the mouse event (override mouse*Event() methods family) and explicitly pass the events into the child object. Good thing is that you can do coordinates transformation (manually) in the middle of this. – Googie Jul 08 '14 at 12:03
  • DocWidget is parent of MainWindow? How can this be? I think MainWindow is the parent of DockWidget. – vahancho Jul 08 '14 at 12:14
  • Yeah I inverted the two, thanks. I edited the message – user3627590 Jul 08 '14 at 12:31
  • @Googie : Yes It will be out of range for the widget. I need this coordonate, to grab a QLabel, get its pixmap associated, and get a pixel value,thanks to the coordinate. Its basicaly a eyedropper. – user3627590 Jul 08 '14 at 12:33
  • If you need to get a clicked `QLabel`, then why don't you just inherit `QLabel` and override `mousePressEvent()` to handle the event, then use your own class instead of that `QLabel`? You can also install event filter on that `QLabel`, see `QObject::installEventFilter()` in Qt's documentation. – Googie Jul 08 '14 at 12:39
  • All the workCode need to be in WidgetTarget, in order to not mix the work of each widget. By passing the QMouseEvent to its children, you mean. Ex : MainWindow::mousePressEvent(QMouseEvent *e){DockWidget.mousePressEvent(e);} ? – user3627590 Jul 08 '14 at 12:44
  • I think you are doing the wrong way. Handle mouse even in the parent, and fire signals to trigger actions. if the child widget is listening, execute the code in a slot. A widget is conceived such that events he process are the ones generated in its display area. – UmNyobe Jul 08 '14 at 12:58
  • @user3627590, the idea is following: if a widget gets the event, it handles it. If event is ignored, it is propagated to the parent widget and so on. How do you see, that event does not reach to the child? Maybe you click not on a child at all? – vahancho Jul 08 '14 at 13:08
  • @vahancho : Yes I have this tested this way. If I click in WidgetTarget, the event is propagated to his parent. But the contrary is not true,If I click in MainWindow, the event is not propageted to its children – user3627590 Jul 08 '14 at 13:25
  • @user3627590, of course it won't. Why do expect that? If you don't click on a child, why should it receive a click event? – vahancho Jul 08 '14 at 13:36
  • If the parent can receive the event from a child, the child could receive an event from its parent, that sounds logical. But apparently that's not the case, so like others users says, I am thinking the wrong way. – user3627590 Jul 08 '14 at 13:41

0 Answers0