Led Like buttons/Widgets

  • Hello everyones,
    i'm looking for some buttons/widget to rappresent boolean variables in UI.

    I have found some old widgets but they are incredibily hard to install on QT5 under Windows.
    Can someone point me to some easy to use widgets or give me some hints to reach my objective?


  • Qt Champions 2016

    This is how you make your own

    However If you do not need to have it visual seen in design mode, you can skip ALL the parts
    with making it a plugin. and simply use the promote feature

    That makes it far easier and you can have a simple LED pretty fast.

  • class Led : public QLabel {
      Led(const Led&) = delete;
      Led& operator=(const Led&) = delete;
     public slots:
      void setOn() {
      void setOff() {
      void setPower(bool val) {
        if(val) setOn();
        else setOff();
      virtual ~Led() = default;
      explicit Led(QWidget* parent = nullptr)
        : QLabel(parent)
        , onPix(48, 48) {
      QPixmap onPix;

  • Hi! You could also use a QPushButton with a LED-like icon on it.

  • My first implementation was quite embarrassing, this is a bit better:

    class Led : public QWidget{
        Q_PROPERTY(bool power READ power WRITE setPower NOTIFY powerChanged)
        Led(const Led&)=delete;
        Led& operator=(const Led&)=delete;
        explicit Led(QWidget* parent=nullptr)
            , m_power(false)
        bool power() const
            return m_power;
    public slots:
        void setPower(bool power)
                m_power = power;
                emit powerChanged();
        void powerChanged();
        virtual void paintEvent(QPaintEvent *event) override{
            QPainter ledPainter(this);
        bool m_power;

    You can change paintEvent to make it look prettier


    Thanks @mrjj for the suggestion about better handling of the update/repaint

  • Qt Champions 2016

    Np, only discovered it as i made a test project :)

    alt text

    To show off the promote thing and the Led.

  • Lifetime Qt Champion


    Just in case, there's QLed that might also be of interest.

  • Qt Champions 2016

    Funny enough I also found that ;)
    Its Qt4-ish and needed to be updated to new plugin export macros and includes for designer
    interface. the pro file also had old keywords and some more stuff needed.

    Here is version that can compile on Windows Qt5.7.

  • There's also a LED widget in KDE4, KLed Class Reference.

  • Guys thank a lot for all your hints. I will find a solution that better fits in my project for sure. Thumbs up for all your answers!

  • @mrjj
    This is a very interesting plugin , i would like to use it.
    I can build the project i downloaded from your dropbox but i still do not find the plugin into my designer.

    I'm under windows , are there any particular passages i have to do to make it work?
    Thank a lot for your time.

    My QT version is 5.7

  • Qt Champions 2016

    Plugins are Dlls. Creator are compiled with Visual Studio. (2013)
    So to load custom plugin, it must be compiled with visual studio and not mingw.
    Then the dll should be copied to "\bin\plugins\designer"

    Alternative you can use the Qleds by Promotion

    Alternative, you can have my VS DLL
    Copy to C:\Qt\Tools\QtCreator\bin\plugins\designer
    Disclaimer: Normally I do not recommend downloading DLLS from forums.

  • @mrjj
    Your answer is very exaustive, thanks for the heads up!

  • Qt Champions 2016

    I think its needs a .pri file to be complete
    section "Splitting up the Plugin"
    To make it easy to use in a clean project.
    Then all you need to do is
    to use in a project. Didn't try that yet.

  • @mrjj
    with the dll now i see the widget in the Designer.
    Anyway if i use it in i have serveral errors.

    In particular i tried to create the PRI file containing:

    HEADERS += $$PWD/qled.h
    SOURCES += $$PWD/qled.cpp

    That i added to the plugin and the project file

    Whan i try to compile now i get this error

    mingw32-make[1]: *** No rule to make target '../../QTProjects-New/Alpha2/qled.cpp', needed by 'debug/qled.o'. Stop.:

    What i'm doing wrong with this pri files?
    Is again mingw fault?

  • Qt Champions 2016

    seems fine.
    I have not tried this yet.
    No, should not be mingw fault. :)

    maybe we be need
    QT += QLed

    Sounds like its not linking the plugin but I not sure if that will do it.

    I also really want to know how to do this the right way. as I think its very little needed to fully work but
    not sure what it is.

  • @mrjj
    Unfortunately for me .pri files are not working, neither copying the qled.h and qled.cpp into the project.

    I got those warnings during compile time

    warning: 'virtual const QMetaObject* QLed::metaObject() const' redeclared without dllimport attribute: previous dllimport ignored
     warning: 'void QLed::setOffColor(QLed::ledColor)' redeclared without dllimport attribute: previous dllimport ignored

    and errors

    QTProjects\build-Alpha2-Desktop_Qt_5_7_0_MinGW_32bit-Debug/debug/moc_qled.cpp:151: undefined reference to `_imp___ZN4QLed10setOnColorENS_8ledColorE
    QTProjects\build-Alpha2-Desktop_Qt_5_7_0_MinGW_32bit-Debug/debug/moc_qled.cpp:153: undefined reference to `_imp___ZN4QLed8setShapeENS_8ledShapeE

    tried to add qledplugin.dll as lib in the .pro file too without success.

    Have you any suggestion ? :)
    Thanks again for the attention and time

  • Qt Champions 2016

    Yes. If you remove the QDESIGNER_WIDGET_EXPORT
    from the qled.h it will link and run
    as in

    class /*QDESIGNER_WIDGET_EXPORT*/ QLed : public QWidget {

    You also need the qres in PRI file

    QT += svg
    HEADERS += $$PWD/qled.h
    SOURCES += $$PWD/qled.cpp
    RESOURCES += $$PWD/qled.qrc

    Note that this is slightly hack-ish as you need QDESIGNER_WIDGET_EXPORT when compiling as plugin.
    But not when using it via .PRI file.

  • @mrjj
    you have been fast as lightning! And it's working! Thahks a lot!

  • Qt Champions 2016

    Super :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.