QWidget childAt(x, y) method always return NULL [Solved]
-
Hi all,
I'm rather new to Qt and have been playing around with it to develop a C++ desktop application for a school project. I must say I'm quite surprised with what the frameworks is capable of and how easy it is to use in combination with QtCreator. I have however run into an issue I seem unable to solve. I have the folowing problem:
I used QtCreator to create a form, a QMainWindow. Added a centralwidget to this, but promoted it to being of my custom widget class MouseWidget. The idea would be for this to function as container and capture and handle mouse events. Next, I set the central widget to use a grid layout. So far, so good. It is very capable of doing this.
Now what I did, from the constructor of my QMainWindow I added 24 x 24 instances of my custom QWidget class called Pixel to the layout of the central widget. These Pixel widgets are just squares which are either black or white. Also not a problem, they are perfectly shown in a 24 by 24 grid.
My problem is basically, when moving the mouse pointer I would like to be able to translate the mouse pointer position to the underlying pixel instances which is shown. By implementing mouseMoveEvent() function for the central widget class I track mouse movement and get its coordinates by using @event->pos()->x()@ and @event->pos()->y()@
Now, I know, the central widget's layout contains 24 x 24 Pixel widgets. When I ask for the amount of children the central widget has by calling @children()->size()@ this seems to be correct. However when I try to retrieve a child, a Pixel instance, by calling the @childAt(x, y)@ method this always returns 0, no matter what values I pass as arguments.
Hopefully someone is capable of helping me with this issue, I can't seem to solve it unfortunately ...
Arjen
-
childAt() returns the visible child at the position (x,y) - is the pixel widget really visible?
Did you try to check with a hard coded position, say childAt(10,10)?
-
Thank you for your response. I checked a couple of things (again). Tried the hard coded position, no such luck either.
Something else I tried. onMousePress loop through all children (they seem to be all in this collection) and print whether the Pixel instances are visible and also print their position coordinates. Which gives me output like:
Pixel isVisible = true X = 391 Y = 11
Pixel isVisible = true X = 411 Y = 11
Pixel isVisible = true X = 431 Y = 11
Pixel isVisible = true X = 451 Y = 11
Pixel isVisible = true X = 471 Y = 11
Pixel isVisible = true X = 11 Y = 31
Pixel isVisible = true X = 31 Y = 31
Pixel isVisible = true X = 51 Y = 31Now, I would expect @QWidget *w = childAt(11, 31);@ to be some value non-zero. However, still not the case. Again, I seem to be stuck here ...
-
Alright, so I figured I'd share the reason it probably behaves like this is because of @setAttribute(Qt::WA_TransparentForMouseEvents);@ which is set for all Pixel instances. Apparently it is then no longer visible. I think this is unexpected behaviour though. Being transparent for mouse events to me does not make a widget invisible for everything else as well.
-
Ok, so I solved my problems by ignoring mouse events in the children (the Pixel instances) by calling @event->ignore()@ and have them propagated up the chain to the parent container (the MouseWidget instance).