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. QBitArray instantiate with a sequence of bits

QBitArray instantiate with a sequence of bits

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 259 Views
  • 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.
  • R Offline
    R Offline
    roditu
    wrote on last edited by roditu
    #1

    Hello
    I have a question. How to instantiate QBitArray with a sequence of bits?

    QBitArray data = QBitArray::fromBits("1110", 4);
    
        qInfo() << data[0];
        qInfo() << data[1];
        qInfo() << data[2];
        qInfo() << data[3];
    

    This prints "1000", obviously, perfect and logical.
    It's not correct in my case though.

    Now how to do it so that it sets 1110 instead, so i don't have to do:

    data[0] = 1;
    data[1] = 1;
    data[2] = 1;
    data[3] = 0;
    

    which is stupid.

    J.HilkJ 1 Reply Last reply
    0
    • R roditu

      Hello
      I have a question. How to instantiate QBitArray with a sequence of bits?

      QBitArray data = QBitArray::fromBits("1110", 4);
      
          qInfo() << data[0];
          qInfo() << data[1];
          qInfo() << data[2];
          qInfo() << data[3];
      

      This prints "1000", obviously, perfect and logical.
      It's not correct in my case though.

      Now how to do it so that it sets 1110 instead, so i don't have to do:

      data[0] = 1;
      data[1] = 1;
      data[2] = 1;
      data[3] = 0;
      

      which is stupid.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @roditu you don't initialiser list is not supported for QBitArray or for QByteArray

      you can use the from bits function correctly:

      const char *data = "\x7"; // 1110 in binary depending on endian
      QBitArray bits = QBitArray::fromBits(data, 4);
      

      keep in mind, that data needs to live as long as bits!

      or you could make a helper function :

      QBitArray bitArrayFromList(std::initializer_list<bool> list) {
          QBitArray bits(list.size());
          int i = 0;
          for (bool b : list) {
              bits[i] = b;
              ++i;
          }
          return bits;
      }
      
      QBitArray bits = bitArrayFromList({true, true, true, false});
      qDebug() << bits << bits[0] << bits[1] << bits[2] << bits[3];
      //QBitArray(1110) true true true false
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      3

      • Login

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