I ask you an advice on multiple QLabel...
-
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 regardsStefano
-
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 regardsStefano
@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?
-
@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?
@JonB: Hi, I've about 40 QLabels.
-
Hi,
The short version: use a QMap to associate each value with the label you want to show/hide.
-
@JonB: Hi, I've about 40 QLabels.
@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. -
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 :( -
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 :(@Stefanoxjx use std::bitset to get 0/1s
-
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 :(@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.
-
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 :(@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)); } }
-
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