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. I ask you an advice on multiple QLabel...
Forum Updated to NodeBB v4.3 + New Features

I ask you an advice on multiple QLabel...

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 6 Posters 540 Views 2 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.
  • S Offline
    S Offline
    Stefanoxjx
    wrote on last edited by
    #1

    Hi boys,
    I would like to ask you an advice about manage of multiple QLabel.
    My situation is this:

    • I've many QLabels.
    • I receive from a external source some bytes where, any single bit correspond to a QLabel to be made visible or invisible based on the value of each bit (0=invisible/1=visible).

    What strategy you can suggest me to reduce at the minimum the code lines and avoid a lot of if/then?

    Thanks
    Best regards

    Stefano

    JonBJ 1 Reply Last reply
    0
    • S Stefanoxjx

      Hi boys,
      I would like to ask you an advice about manage of multiple QLabel.
      My situation is this:

      • I've many QLabels.
      • I receive from a external source some bytes where, any single bit correspond to a QLabel to be made visible or invisible based on the value of each bit (0=invisible/1=visible).

      What strategy you can suggest me to reduce at the minimum the code lines and avoid a lot of if/then?

      Thanks
      Best regards

      Stefano

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Stefanoxjx said in I ask you an advice on multiple QLabel...:

      I've many QLabels.

      Just how many are you talking about, at least approximately?

      S 1 Reply Last reply
      1
      • JonBJ JonB

        @Stefanoxjx said in I ask you an advice on multiple QLabel...:

        I've many QLabels.

        Just how many are you talking about, at least approximately?

        S Offline
        S Offline
        Stefanoxjx
        wrote on last edited by
        #3

        @JonB: Hi, I've about 40 QLabels.

        JonBJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          The short version: use a QMap to associate each value with the label you want to show/hide.

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

          1 Reply Last reply
          2
          • S Stefanoxjx

            @JonB: Hi, I've about 40 QLabels.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Stefanoxjx
            Oh OK. So store pointers to the labels in an array/vector, in the same order is the matching bit for it. Use that to decide which label to show/hide, no "if"s.

            1 Reply Last reply
            1
            • S Offline
              S Offline
              Stefanoxjx
              wrote on last edited by
              #6

              Thanks for your answers.
              To a array/vector I was already thought, to QMap no.
              But my doubt is, how I can associate the bit to an array pointer.
              For example, if I've:

              QLabel1
              QLabel2
              QLabel3
              QLabel4
              QLabel5
              QLabel6
              QLabel7
              QLabel8
              

              and a pointer vector with any QLabel above.
              If I receive a byte with value 25 (decimal) 11001 (binary) means that I must set visible QLabel1, QLabel4 and QLabel5 and make the rest invisible.
              Ho I can do this without some if's?
              This is my problem :(

              JoeCFDJ ? C 3 Replies Last reply
              0
              • S Stefanoxjx

                Thanks for your answers.
                To a array/vector I was already thought, to QMap no.
                But my doubt is, how I can associate the bit to an array pointer.
                For example, if I've:

                QLabel1
                QLabel2
                QLabel3
                QLabel4
                QLabel5
                QLabel6
                QLabel7
                QLabel8
                

                and a pointer vector with any QLabel above.
                If I receive a byte with value 25 (decimal) 11001 (binary) means that I must set visible QLabel1, QLabel4 and QLabel5 and make the rest invisible.
                Ho I can do this without some if's?
                This is my problem :(

                JoeCFDJ Offline
                JoeCFDJ Offline
                JoeCFD
                wrote on last edited by
                #7

                @Stefanoxjx use std::bitset to get 0/1s

                1 Reply Last reply
                2
                • S Stefanoxjx

                  Thanks for your answers.
                  To a array/vector I was already thought, to QMap no.
                  But my doubt is, how I can associate the bit to an array pointer.
                  For example, if I've:

                  QLabel1
                  QLabel2
                  QLabel3
                  QLabel4
                  QLabel5
                  QLabel6
                  QLabel7
                  QLabel8
                  

                  and a pointer vector with any QLabel above.
                  If I receive a byte with value 25 (decimal) 11001 (binary) means that I must set visible QLabel1, QLabel4 and QLabel5 and make the rest invisible.
                  Ho I can do this without some if's?
                  This is my problem :(

                  ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  @Stefanoxjx maybe you can try use QLabel's name to do someting, for example, "QLabel1" is one QLabel's objectName, you can get objectName's last char,the char is '1', so it's visiable is bit1's value.

                  1 Reply Last reply
                  1
                  • S Stefanoxjx

                    Thanks for your answers.
                    To a array/vector I was already thought, to QMap no.
                    But my doubt is, how I can associate the bit to an array pointer.
                    For example, if I've:

                    QLabel1
                    QLabel2
                    QLabel3
                    QLabel4
                    QLabel5
                    QLabel6
                    QLabel7
                    QLabel8
                    

                    and a pointer vector with any QLabel above.
                    If I receive a byte with value 25 (decimal) 11001 (binary) means that I must set visible QLabel1, QLabel4 and QLabel5 and make the rest invisible.
                    Ho I can do this without some if's?
                    This is my problem :(

                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #9

                    @Stefanoxjx Something like this:

                    // main.cpp
                    #include "bitbuttons.h"
                    
                    #include <QApplication>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                        BitButtons w;
                        w.show();
                        return a.exec();
                    }
                    
                    // bitbuttons.h
                    #ifndef BITBUTTONS_H
                    #define BITBUTTONS_H
                    
                    #include <QPushButton>
                    #include <QWidget>
                    
                    class BitButtons : public QWidget
                    {
                        Q_OBJECT
                    
                    public:
                        BitButtons(QWidget *parent = nullptr);
                        ~BitButtons();
                    
                    public slots:
                        void setBits(unsigned long value);
                    
                    
                    private:
                        QVector<QPushButton*> m_buttons;
                    
                        quint8 m_testValue;
                    };
                    #endif // BITBUTTONS_H
                    
                    // bitbuttons.cpp
                    #include "bitbuttons.h"
                    
                    #include <QTimer>
                    #include <QVBoxLayout>
                    #include <bitset>
                    
                    namespace {
                        const int BUTTON_COUNT = 8;
                    }
                    
                    BitButtons::BitButtons(QWidget *parent)
                        : QWidget(parent)
                        , m_buttons(BUTTON_COUNT, nullptr)
                        , m_testValue(0)
                    {
                        QVBoxLayout *layout = new QVBoxLayout(this);
                        for (int i = 0; i < m_buttons.size(); ++i) {
                            m_buttons[i] = new QPushButton(QString::number(i), this);
                            layout->addWidget(m_buttons.at(i));
                        }
                    
                        setBits(0);
                    
                        // Something to generate test data
                        QTimer *timer = new QTimer(this);
                        connect(timer, &QTimer::timeout,
                                this, [this](){setBits(m_testValue++);}
                        );
                        timer->setInterval(500);
                        timer->start();
                    }
                    
                    BitButtons::~BitButtons()
                    {
                    }
                    
                    void BitButtons::setBits(unsigned long value)
                    {
                        std::bitset<BUTTON_COUNT> bits(value);
                        for (int i = 0; i < BUTTON_COUNT; ++i) {
                            m_buttons.at(i)->setEnabled(bits.test(i));
                        }
                    }
                    
                    1 Reply Last reply
                    3
                    • S Offline
                      S Offline
                      Stefanoxjx
                      wrote on last edited by
                      #10

                      Hi boys, thanks at all for help.
                      In the end, I used the solution with QVector and bitset and works very well.
                      The code lines are reduced to a minimum how I wanted.
                      I would say very well :)

                      Thanks a lot.

                      Stefano

                      1 Reply Last reply
                      0

                      • Login

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