Important: Please read the Qt Code of Conduct -

Qt QDeclarativeItem subclass, detect when obscured

  • I have created a custom QML component based on this article

    It is working well, the item is created in my QML scene and is rendered how I want it. I have also created a method to expand the item to fill its parent when it's clicked on.

    On the mouse event I change the size and shape of my subclass using

    This then fills it's parent the way I want. I keep a record of it's previous settings so I can restore it to it's previous size on a subsequent click.

    So here is the problem. I need to know if the widget is visible (to the user) and if it is obscured at all. The visible part if working OK. I have implemented the itemChange function, which gives me if the widget is visible.
    QVariant itemChange(GraphicsItemChange change, const QVariant &value)
    if (change == ItemVisibleHasChanged) {
    if (value.toBool() == false) {
    qDebug() << "Widget is invisible";

    But I can't find a change event to discover if the widget is partially obscured. I've read in the documentation that setting this flag in the constructor

    setFlag(QGraphicsItem::ItemUsesExtendedStyleOption, true);
    then checking the paint event to see if the exposed area is equal to the boundingRect of the widget. If it's not then the widget is at least partially obscured, but this doesn't seem to work for me. They always seem equal, even when another widget covers the entire window.
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    if (option->exposedRect != boundingRect()) {
    qDebug() << "This widget is at least partially obscured";
    I've also tried isObscured(), which the documentation states will return true if the widget has another widget on top (i.e. a higher Z). But again this doesn't return true, plus I can't get a signal from this.

    So does anyone know how to detect if a subclassed QDeclarativeItem is partially obscured from the users perspective by another QML component? I was sure there would be some way to get a signal that the widget has been covered, but I can't seem to find it anywhere.

Log in to reply