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. QVariant to QBitArray
Forum Updated to NodeBB v4.3 + New Features

QVariant to QBitArray

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 6.1k Views 1 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.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    After next code:
    @QHashIterator<QString, QVariant> iter(props);

                    while(iter.hasNext()) {
                        iter.next();
                        
                        QString property = iter.key();
    
                        if (property.startsWith("mus")) {
                            int num = QString("%1").arg(property.remove("mus")).toInt();
                            QBitArray bitValue = iter.value().toBitArray();
    
                            m_address = sender;
                            m_socket->bind(m_address, 0x6358);
                            connect(m_socket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    
                            if (num > 0 && num <= m_musList.count()) {
                                MusWidget * musWg = m_musList.at(num-1);
                                if (musWg)
                                    musWg->changeState(bitValue);
                            }
                        }
                    }@
    

    I've got an empty bit array with zero size in bitValue variable. But if I look debug information of my IDE I can see that iter.value() contains some data in format of bits like "11111". So question how can I convert QVariant value to QBitArray value ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      guziemic
      wrote on last edited by
      #2

      Hi,

      I am not an expert in this area, but maybe this will help

      • did you try to check what type is stored by
        "QVariant:Type":http://qt-project.org/doc/qt-4.8/qvariant.html#type

      • and if type is different maybe you could cast your Variant to desired type with "QVarian:convert":http://qt-project.org/doc/qt-4.8/qvariant.html#convert

      I am not sure if Qt cast Variant to BitArray automatically.

      BR,

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        QVariant::toBitArray() is correct way - given the QVariant really contains a QBitArray object.

        Are you sure you have a QBitArray inside your QVariant? Maybe it's a QByteArray or something different.

        In any case, you should check QVariant::typeName() to see what it actually is...

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Anticross
          wrote on last edited by
          #4

          It's a string value inserted inside which now stored in QHash iter.value(). So i need to convert from string but now it's type is QVariant. Something like this.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            guziemic
            wrote on last edited by
            #5

            Do I understand correctly that you are trying to achieve such conversion
            String -> QVariant -> QBitArray

            AFAIK you will be not able to make such conversion.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              Anticross
              wrote on last edited by
              #6

              I mean I insert string value into hash QHash<QString,QVariant>, but this string has format of bits "1111" or "1010" or any other binary value. After that I need to read value from hash and thats why I get QVarant value and after that I need to convert this QVariant value tu QBitArray.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                guziemic
                wrote on last edited by
                #7

                Well, if you have those bits in String then maybe better solution is to convert String to unsigned int and then with bitwise operators extract bits values.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Anticross
                  wrote on last edited by
                  #8

                  I can convert to int: @iter.value().toString().toInt(NULL,2)@ but how now I can insert integer value in QBitArray ?

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    guziemic
                    wrote on last edited by
                    #9

                    The snippet below shows how you can convert some integer value to bitArray. It is small main so you can run it and test.

                    @
                    #include <QBitArray>
                    #include <QDebug>

                    int main(int argc, char *argv[])
                    {
                    int someVal = 10; //value to convert
                    QBitArray bitArray(sizeof(int)*8);
                    long long refVal = 1;

                    //check in loop which bit is set
                    for ( int i = 0; i < static_cast<int>(sizeof(int)*8); i++)
                    {
                    qDebug() << "i" << i << "refVal" << refVal;

                        if (false != (someVal & refVal))
                        {
                            bitArray.setBit(i, true);
                        }
                    
                        refVal <<= 1;
                    }
                    

                    //display bit array
                    for ( int i = 0; i < bitArray.size(); i++)
                    {
                    qDebug() << "Bit at i" << i << " value = "<< bitArray.at(i);
                    }

                    return 0;
                    

                    @

                    Hope it will help

                    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