Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Led Like buttons/Widgets
Forum Updated to NodeBB v4.3 + New Features

Led Like buttons/Widgets

Scheduled Pinned Locked Moved General and Desktop
39 Posts 8 Posters 33.6k Views 4 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bruschetta
    wrote on 25 Nov 2016, 13:40 last edited by Bruschetta
    #1

    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?

    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 25 Nov 2016, 13:48 last edited by
      #2

      Hi
      This is how you make your own
      http://www.ics.com/blog/integrating-custom-widget-qt-designer

      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
      http://doc.qt.io/qt-4.8/designer-using-custom-widgets.html

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

      1 Reply Last reply
      3
      • V Offline
        V Offline
        VRonin
        wrote on 25 Nov 2016, 13:52 last edited by VRonin
        #3
        class Led : public QLabel {
          Q_OBJECT
          Led(const Led&) = delete;
          Led& operator=(const Led&) = delete;
         public slots:
          void setOn() {
            onPix.fill(Qt::red);
            setPixmap(onPix);
          }
          void setOff() {
            onPix.fill(Qt::black);
            setPixmap(onPix);
          }
          void setPower(bool val) {
            if(val) setOn();
            else setOff();
          }
         public:
          virtual ~Led() = default;
          explicit Led(QWidget* parent = nullptr)
            : QLabel(parent)
            , onPix(48, 48) {
            onPix.fill(Qt::black);
            setPixmap(onPix);
            setScaledContents(true);
          }
         private:
          QPixmap onPix;
        };
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        6
        • ? Offline
          ? Offline
          A Former User
          wrote on 25 Nov 2016, 13:53 last edited by
          #4

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

          1 Reply Last reply
          4
          • V Offline
            V Offline
            VRonin
            wrote on 25 Nov 2016, 15:48 last edited by VRonin
            #5

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

            class Led : public QWidget{
                Q_OBJECT
                Q_PROPERTY(bool power READ power WRITE setPower NOTIFY powerChanged)
                Led(const Led&)=delete;
                Led& operator=(const Led&)=delete;
            public:
                explicit Led(QWidget* parent=nullptr)
                    :QWidget(parent)
                    , m_power(false)
                {}
                bool power() const
                {
                    return m_power;
                }
            public slots:
                void setPower(bool power)
                {
                    if(power!=m_power){
                        m_power = power;
                        emit powerChanged();
                        update();
                    }
                }
            signals:
                void powerChanged();
            protected:
                virtual void paintEvent(QPaintEvent *event) override{
                    Q_UNUSED(event)
                    QPainter ledPainter(this);
                    ledPainter.setPen(Qt::black);
                    if(m_power)
                        ledPainter.setBrush(Qt::red);
                    else
                        ledPainter.setBrush(Qt::NoBrush);
                    ledPainter.drawEllipse(rect());
                }
            private:
                bool m_power;
            };
            

            You can change paintEvent to make it look prettier

            Edit:

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

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            M 1 Reply Last reply 25 Nov 2016, 16:51
            6
            • V VRonin
              25 Nov 2016, 15:48

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

              class Led : public QWidget{
                  Q_OBJECT
                  Q_PROPERTY(bool power READ power WRITE setPower NOTIFY powerChanged)
                  Led(const Led&)=delete;
                  Led& operator=(const Led&)=delete;
              public:
                  explicit Led(QWidget* parent=nullptr)
                      :QWidget(parent)
                      , m_power(false)
                  {}
                  bool power() const
                  {
                      return m_power;
                  }
              public slots:
                  void setPower(bool power)
                  {
                      if(power!=m_power){
                          m_power = power;
                          emit powerChanged();
                          update();
                      }
                  }
              signals:
                  void powerChanged();
              protected:
                  virtual void paintEvent(QPaintEvent *event) override{
                      Q_UNUSED(event)
                      QPainter ledPainter(this);
                      ledPainter.setPen(Qt::black);
                      if(m_power)
                          ledPainter.setBrush(Qt::red);
                      else
                          ledPainter.setBrush(Qt::NoBrush);
                      ledPainter.drawEllipse(rect());
                  }
              private:
                  bool m_power;
              };
              

              You can change paintEvent to make it look prettier

              Edit:

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

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 25 Nov 2016, 16:51 last edited by
              #6

              @VRonin
              Np, only discovered it as i made a test project :)
              https://www.dropbox.com/s/you53vbvl5z73ql/myleds.zip?dl=0

              alt text

              To show off the promote thing and the Led.

              1 Reply Last reply
              3
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 25 Nov 2016, 21:45 last edited by
                #7

                Hi,

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

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                M 1 Reply Last reply 25 Nov 2016, 22:09
                4
                • SGaistS SGaist
                  25 Nov 2016, 21:45

                  Hi,

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 25 Nov 2016, 22:09 last edited by
                  #8

                  @SGaist
                  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.
                  https://www.dropbox.com/s/38endyfoexb28nd/QLeds57.zip?dl=0

                  B B 2 Replies Last reply 1 Dec 2016, 07:57
                  2
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on 25 Nov 2016, 22:16 last edited by
                    #9

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

                    • kled.h
                    • kled.cpp
                    1 Reply Last reply
                    5
                    • B Offline
                      B Offline
                      Bruschetta
                      wrote on 1 Dec 2016, 07:38 last edited by
                      #10

                      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!

                      1 Reply Last reply
                      0
                      • M mrjj
                        25 Nov 2016, 22:09

                        @SGaist
                        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.
                        https://www.dropbox.com/s/38endyfoexb28nd/QLeds57.zip?dl=0

                        B Offline
                        B Offline
                        Bruschetta
                        wrote on 1 Dec 2016, 07:57 last edited by Bruschetta 12 Jan 2016, 07:59
                        #11

                        @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

                        M 1 Reply Last reply 1 Dec 2016, 12:34
                        0
                        • B Bruschetta
                          1 Dec 2016, 07:57

                          @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

                          M Offline
                          M Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 1 Dec 2016, 12:34 last edited by mrjj 12 Jan 2016, 12:44
                          #12

                          @Bruschetta
                          Hi.
                          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
                          http://doc.qt.io/qt-5/designer-using-custom-widgets.html

                          Alternative, you can have my VS DLL
                          https://www.dropbox.com/s/1f65fzcew6pjm2z/VSqledplugin.zip?dl=0
                          Copy to C:\Qt\Tools\QtCreator\bin\plugins\designer
                          Disclaimer: Normally I do not recommend downloading DLLS from forums.

                          B 1 Reply Last reply 1 Dec 2016, 13:14
                          2
                          • M mrjj
                            1 Dec 2016, 12:34

                            @Bruschetta
                            Hi.
                            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
                            http://doc.qt.io/qt-5/designer-using-custom-widgets.html

                            Alternative, you can have my VS DLL
                            https://www.dropbox.com/s/1f65fzcew6pjm2z/VSqledplugin.zip?dl=0
                            Copy to C:\Qt\Tools\QtCreator\bin\plugins\designer
                            Disclaimer: Normally I do not recommend downloading DLLS from forums.

                            B Offline
                            B Offline
                            Bruschetta
                            wrote on 1 Dec 2016, 13:14 last edited by
                            #13

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

                            M 1 Reply Last reply 1 Dec 2016, 13:21
                            2
                            • B Bruschetta
                              1 Dec 2016, 13:14

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

                              M Offline
                              M Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 1 Dec 2016, 13:21 last edited by
                              #14

                              @Bruschetta
                              Np.
                              I think its needs a .pri file to be complete
                              http://doc.qt.io/qt-5/designer-creating-custom-widgets.html
                              section "Splitting up the Plugin"
                              To make it easy to use in a clean project.
                              Then all you need to do is
                              include(QLeds.pri)
                              to use in a project. Didn't try that yet.

                              B 1 Reply Last reply 1 Dec 2016, 14:50
                              2
                              • M mrjj
                                1 Dec 2016, 13:21

                                @Bruschetta
                                Np.
                                I think its needs a .pri file to be complete
                                http://doc.qt.io/qt-5/designer-creating-custom-widgets.html
                                section "Splitting up the Plugin"
                                To make it easy to use in a clean project.
                                Then all you need to do is
                                include(QLeds.pri)
                                to use in a project. Didn't try that yet.

                                B Offline
                                B Offline
                                Bruschetta
                                wrote on 1 Dec 2016, 14:50 last edited by Bruschetta 12 Jan 2016, 15:00
                                #15

                                @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:

                                INCLUDEPATH += $$PWD
                                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?

                                M 1 Reply Last reply 1 Dec 2016, 15:05
                                0
                                • B Bruschetta
                                  1 Dec 2016, 14:50

                                  @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:

                                  INCLUDEPATH += $$PWD
                                  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?

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 1 Dec 2016, 15:05 last edited by
                                  #16

                                  @Bruschetta
                                  Hi.
                                  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.

                                  B 1 Reply Last reply 2 Dec 2016, 08:21
                                  1
                                  • M mrjj
                                    1 Dec 2016, 15:05

                                    @Bruschetta
                                    Hi.
                                    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.

                                    B Offline
                                    B Offline
                                    Bruschetta
                                    wrote on 2 Dec 2016, 08:21 last edited by Bruschetta 12 Feb 2016, 08:22
                                    #17

                                    @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

                                    1 Reply Last reply
                                    0
                                    • M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 2 Dec 2016, 08:25 last edited by mrjj 12 Feb 2016, 08:38
                                      #18

                                      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
                                      qled.pri

                                      QT += svg
                                      INCLUDEPATH += $$PWD
                                      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.

                                      B 1 Reply Last reply 2 Dec 2016, 08:32
                                      3
                                      • M mrjj
                                        2 Dec 2016, 08:25

                                        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
                                        qled.pri

                                        QT += svg
                                        INCLUDEPATH += $$PWD
                                        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.

                                        B Offline
                                        B Offline
                                        Bruschetta
                                        wrote on 2 Dec 2016, 08:32 last edited by
                                        #19

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

                                        M 1 Reply Last reply 2 Dec 2016, 08:38
                                        2
                                        • B Bruschetta
                                          2 Dec 2016, 08:32

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

                                          M Offline
                                          M Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 2 Dec 2016, 08:38 last edited by
                                          #20

                                          @Bruschetta
                                          Super :)

                                          1 Reply Last reply
                                          1

                                          1/39

                                          25 Nov 2016, 13:40

                                          • Login

                                          • Login or register to search.
                                          1 out of 39
                                          • First post
                                            1/39
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved