Question

I find it difficult to translate binary into picture, I use a pixmap. transfer into the binary is correct but when I show using this program actually does not work.

this is my code:

if (binaryNumber[0]==1)ui->led16->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led16->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[1]=1) ui->led15->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led15->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[2]==1)ui->led14->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led14->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[3]==1)ui->led13->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led13->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[4]==1)ui->led12->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led12->setPixmap(QPixmap("../../picture/ball-gray.png"));


bool ok2 = false;
QByteArray binaryNumber = QByteArray::number(DO.toLongLong(&ok2, 16), 2);



qDebug()<<binaryNumber<<binaryNumber[0]<<binaryNumber[1]<<binaryNumber[2 <<binaryNumber[3];

i.e

binaryNumber =1011
binaryNumber[0] = 1
binaryNumber[1] = 0
binaryNumber[2] = 1
binaryNumber[3] = 1

but when

binaryNumber =100
binaryNumber[0] = 1
binaryNumber[1] = 0
binaryNumber[2] = 0

so when i use a pixmap, then led the flame does not correspond to the binary number because array [0] is different when the size is different. is there any simple code for me?

Was it helpful?

Solution

Note that QByteArray::number() returns alphanumeric characters ('0' = 48, '1' = 49 etc.), not characters with the numerical values 0, 1 etc. This is an important difference!

If you do binaryNumber = QByteArray::number(value, 2), this returns a byte array like for example "1010". Thus, binaryNumber[0] == '1', NOT binaryNumber[0] == 1:

if (binaryNumber[0]=='1')ui->led16->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led16->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[1]=='1')ui->led15->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led15->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[2]=='1')ui->led14->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led14->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[3]=='1')ui->led13->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led13->setPixmap(QPixmap("../../picture/ball-gray.png"));
if (binaryNumber[4]=='1')ui->led12->setPixmap(QPixmap("../../picture/ball-yellow.png"));
else ui->led12->setPixmap(QPixmap("../../picture/ball-gray.png"));

Note that your code is riddled with redundant code lines, resulting in bad quality software. You should try to write the code above in a loop or at least move out the pixmaps. Moving the pixmap initialisations in some static variables or in the constructor of the containing class results in some performance boost, too.

So your class could look similar to this: (I only included the relevant parts, of course, there also has to be the code for the UI stuff.)

class LEDNumberView
{
private:
    // member variables:
    QPixmap bitOn;
    QPixmap bitOff;

    // helper function
    inline QPixmap getBitPixmap(bool bitVal)
    {
        return bitVal ? bitOn : bitOff;
    }

public: 
    // constructor
    LEDNumberView()
    {
        QString path = "../../picture/ball-%1.png";
        bitOn = QPixmap(path.arg("yellow"));
        bitOff = QPixmap(path.arg("gray"));
    }

    // call whenever you want to change the binary number displayed by the LEDs
    void setBinaryNumber(int value)
    {
        QByteArray binaryNumber = QByteArray::number(value, 2);
        ui->led16->setPixmap(getBitPixmap(binaryNumber[0] == '1'));
        ui->led15->setPixmap(getBitPixmap(binaryNumber[1] == '1'));
        ui->led14->setPixmap(getBitPixmap(binaryNumber[2] == '1'));
        ui->led13->setPixmap(getBitPixmap(binaryNumber[3] == '1'));
        ui->led12->setPixmap(getBitPixmap(binaryNumber[4] == '1'));
        ui->led11->setPixmap(getBitPixmap(binaryNumber[5] == '1'));
    }
};

To combine the answer of Kuba Ober with mine, write the setBinaryNumber function as he suggested. It's up to you which method of binary conversion you prefer - bit manipulation (use his method) or convert to and then work with bytes (yours).

OTHER TIPS

Your use of a QByteArray to store bits of a number is unnecessary. In C/C++, you can access the bits directly by doing a bitwise AND (&) with a mask.

template <typename T> static QPixmap setPixmap(T * p, int value, int bitNo)
{
  const bool bit = value & (1<<bitNo);
  p->setPixmap(bit ? QPixmap("../../picture/ball-yellow.png")   
                   : QPixmap("../../picture/ball-gray.png"));
}

void Class::setDisplay(int val)
{
  setPixmap(ui->led12, val, 0);
  setPixmap(ui->led13, val, 1);
  setPixmap(ui->led14, val, 2);
  setPixmap(ui->led15, val, 3);
  setPixmap(ui->led16, val, 4);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top