What can we do with QBitArray?



  • std::bitset has to_ulong() and to_string() members, they are very useful. QBitArray has none of them. I think i can't do any thing using QBitArray.



  • Well, you get the advantages you'll get with every Qt container: implicit sharing, QDataStream support, dynamic sizing.



  • [quote author="ibingow" date="1325065025"] std::bitset has to_ulong() and to_string() members, they are very useful. QBitArray has none of them. I think i can't do any thing using QBitArray.[/quote]

    I guess it would depend on what you want to achieve. I could imagine a <code>toByteArray()</code> method being useful, but I don't see how a <code>toULong()</code> would help (that would not work for all but very short bit arrays), nor how <code>toString()</code> would be useful. However, the <code>toByteArray()</code> is easy enough to do yourself (just use a QBuffer), so not much is really missing.



  • but qbitarray does not have any member to convert to another class.
    [quote author="Andre" date="1325072524"]
    [quote author="ibingow" date="1325065025"] std::bitset has to_ulong() and to_string() members, they are very useful. QBitArray has none of them. I think i can't do any thing using QBitArray.[/quote]

    I guess it would depend on what you want to achieve. I could imagine a <code>toByteArray()</code> method being useful, but I don't see how a <code>toULong()</code> would help (that would not work for all but very short bit arrays), nor how <code>toString()</code> would be useful. However, the <code>toByteArray()</code> is easy enough to do yourself (just use a QBuffer), so not much is really missing.

    [/quote]



  • Again, you are not telling us what you want to achieve, and again, you overlook the option to stream the array to a QByteArray using a QBuffer.



  • [quote author="ibingow" date="1325176036"]but qbitarray does not have any member to convert to another class.
    [quote author="Andre" date="1325072524"]

    I guess it would depend on what you want to achieve. I could imagine a...
    [/quote]
    [/quote]

    Seems you missed the highlighted words from Andre....



  • I want to convert a qbitarray to int. I know it's easy to do it myself, an to qbytearray is easy too. But if qbitarray has these methods would be better. std::bitset has to_ulong() because usually shot bitarrys are enough.
    In fact, i am writing a program to parse the swf file. Some flash information like width and height are stored in several bits, not bytes.
    [quote author="Andre" date="1325182451"]Again, you are not telling us what you want to achieve, and again, you overlook the option to stream the array to a QByteArray using a QBuffer.
    [/quote]



  • Are you sure you are not looking for QFlags then? You can define their meaning using an enum, so the bits get actual meaning in your application. At least, I find
    @
    if (myFlags.testFlag(CoolOption42)) {
    // some really cool feature
    }
    @

    easier to understand than

    @
    if (myBitArray.at(42)) {
    // some cool feature
    }
    @

    Note that if you want, you can of course just use std::bitset if it suits your needs better. Nothing is stopping you from using std or boost things mixed in with your Qt application.



  • [quote author="ibingow" date="1325211343"]
    In fact, i am writing a program to parse the swf file. Some flash information like width and height are stored in several bits, not bytes.[/quote]

    So QBitArray is cumbersome to use in the first place here. You would have to construct the bit array from a bunch of bytes into something that doesn't take bytes in its constructor or methods. All that only to split the array afterwards and reconstructing your data. Looks more than awkward to me.

    I would read the bytes as such and get the bits or bitfields by using masks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.