Is there a possibility to set a QBitArray using a long int?
-
Qt5 or Qt6?
-
@Christian-Ehrlicher Qt6.
But I am looking for fromUInt32 and not toUInt32 unfortunately. -
-
@Christian-Ehrlicher
Could you help me out with an example please?
I triedQBitArray bitArray; quint32 intvalue = 9999999; bitArray.fromBits(intvalue,32); qInfo() << "Size:" << bitArray.size();and it throws an error saying
main.cpp:12:23: Cannot initialize a parameter of type 'const char *' with an lvalue of type 'quint32' (aka 'unsigned int')If I try the one below, the program crashes.
QBitArray bitArray; quint32 intvalue = 9999999; bitArray.fromBits((char*)(intvalue),32); qInfo() << "Size:" << bitArray.size(); -
@Christian-Ehrlicher
Could you help me out with an example please?
I triedQBitArray bitArray; quint32 intvalue = 9999999; bitArray.fromBits(intvalue,32); qInfo() << "Size:" << bitArray.size();and it throws an error saying
main.cpp:12:23: Cannot initialize a parameter of type 'const char *' with an lvalue of type 'quint32' (aka 'unsigned int')If I try the one below, the program crashes.
QBitArray bitArray; quint32 intvalue = 9999999; bitArray.fromBits((char*)(intvalue),32); qInfo() << "Size:" << bitArray.size();@Kevin470 said in Is there a possibility to set a QBitArray using a long int?:
bitArray.fromBits((char*)(intvalue),32);
Shouldn't it be
bitArray.fromBits((char*)(&intvalue), 32);?
-
@Kevin470 said in Is there a possibility to set a QBitArray using a long int?:
bitArray.fromBits((char*)(intvalue),32);
Shouldn't it be
bitArray.fromBits((char*)(&intvalue), 32);?
-
@jsulm Oops sorry. Yes it should be
bitArray.fromBits((char*)(&intvalue), 32);But it still prints
Size: 0@Kevin470 said in Is there a possibility to set a QBitArray using a long int?:
But it still prints Size: 0
Yes, because https://doc.qt.io/qt-6/qbitarray.html#fromBits is a static method...
-
@Kevin470 said in Is there a possibility to set a QBitArray using a long int?:
But it still prints Size: 0
Yes, because https://doc.qt.io/qt-6/qbitarray.html#fromBits is a static method...
-
@jsulm Thanks a lot. Works Perfectly!
QBitArray bitArray; quint32 intvalue = 9999999; bitArray = QBitArray::fromBits((char*)(&intvalue),32); qInfo() << "Size:" << bitArray.size();@Kevin470 Then please mark this topic as solved, thx.
-
where's the downvote button!?!?! c-style casts 🫢
-
@J-Hilk said in Is there a possibility to set a QBitArray using a long int?:
where's the downvote button
Casted to
(void)downvoteBtn;:)
-
@J-Hilk
I always have to go look it up, should this one bereinterpret_cast<>, or has C++ introduced yet another new one of these casting templates to use for this case?Not to mention that the other day @Chris-Kawa wrote in https://forum.qt.io/topic/139176/qgraphicsitem-conversion-into-long-using-reinterpret_cast/2
I'll just mention that on some platforms pointers to different types can have different sizes too i.e.
sizeof(char*) != sizeof(int*), so, although unlikely that it affects you, what you're doing is a risky business.which seems to me to make a nonsense of half of these castings anyway...!
-
- static_cast for basic types
- dynamic_cast for pointers or references to polymorphic class types;
- const_cast const or volatile qualifiers;
- reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.
-
@J-Hilk
I always have to go look it up, should this one bereinterpret_cast<>, or has C++ introduced yet another new one of these casting templates to use for this case?Not to mention that the other day @Chris-Kawa wrote in https://forum.qt.io/topic/139176/qgraphicsitem-conversion-into-long-using-reinterpret_cast/2
I'll just mention that on some platforms pointers to different types can have different sizes too i.e.
sizeof(char*) != sizeof(int*), so, although unlikely that it affects you, what you're doing is a risky business.which seems to me to make a nonsense of half of these castings anyway...!
@JonB said in Is there a possibility to set a QBitArray using a long int?:
which seems to me to make a nonsense of half of these castings anyway...!
They add validation if the cast is valid which is not available with the c-style cast.
-
- static_cast for basic types
- dynamic_cast for pointers or references to polymorphic class types;
- const_cast const or volatile qualifiers;
- reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.
@J-Hilk said in Is there a possibility to set a QBitArray using a long int?:
reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.
And that (
reinterpret_cast<>) is what should be used in the case we are discussing (int *->char *), right?[Separate: did I see recently that C++20 or whatever has introduced yet another new
somethingelse_cast<>construct beyond these 4, or do you think I'm imagining this?]@Christian-Ehrlicher said in Is there a possibility to set a QBitArray using a long int?:
They add validation if the cast is valid which is not available with the c-style cast.
But not in the
reinterpret_cast<>case which (I believe) we are going to use here, right? -
@J-Hilk said in Is there a possibility to set a QBitArray using a long int?:
reinterpret_cast is for special circumstances and you think you know better than the compiler. Converting between pointers or references and completely unrelated types.
And that (
reinterpret_cast<>) is what should be used in the case we are discussing (int *->char *), right?[Separate: did I see recently that C++20 or whatever has introduced yet another new
somethingelse_cast<>construct beyond these 4, or do you think I'm imagining this?]@Christian-Ehrlicher said in Is there a possibility to set a QBitArray using a long int?:
They add validation if the cast is valid which is not available with the c-style cast.
But not in the
reinterpret_cast<>case which (I believe) we are going to use here, right?@JonB said in Is there a possibility to set a QBitArray using a long int?:
[Separate: did I see recently that C++20 or whatever has introduced yet another new somethingelse_cast<> construct beyond these 4, or do you think I'm imagining this?]
std::bit_cast
but, it's a library feature not a language feature.
Technically, if the OP uses c++20 it would be the "saver" option, otherwise reinterpret_cast