Search code examples
c++cssqtwidgetqframe

Custom checkable / toggleble QFrame widget


How can I make a custom widget (derived from "QFrame") contain a support to be checkable/toggleble? How to make Qt's stylesheet (CSS) to be aware that my widget was checked/toggled by user?

Is there some kind of signal or property that I need to use in my custom widget?

I don't want to use (i.e.) QPushButton because I need my button widget to use Qt's .ui file.

I.e. when using QPushButton - things are simple:

QPushButton#btnName:focus:pressed { border-image: url(ThemesPathKey/menu-top-h.png);    }
QPushButton#btnName:checked {   border-image: url(ThemesPathKey/menu-top-h.png);    }
QPushButton#btnName:hover { border-image: url(ThemesPathKey/menu-top-h.png);    }

I need something similar for my custom button widget.

Thanks.


Solution

  • You can use hover and focus for a custom widget, but checked is supported for buttons and check boxes only.

    To replace checked you can use a custom property:

    QPushButton#btnName[checked="true"] {   border-image: url(ThemesPathKey/menu-top-h.png);    }
    

    when your widget is clicked toggle checked property like this:

    void mousePressEvent(QMouseEvent*) override
    {
        bool checked = property("checked").toBool();
        setProperty("checked", !checked);
        style()->polish(this);
    }