How to pass binary data from c++ to js?
QWebChannel always use json object,QByteArray must be convert to string.
Can QByteArray convert to ArrayBuffer?
Related
I have user-provided format string (e.g. "%.2f") and a QVariant type that I am attempting to combine to output into a (formatted) string.
I had gone down the path of using QString::asprintf(const char *cformat, ...) to achieve this, where I would supply the appropriate converted data type, like this:
QString result_str = QString::asprintf(disp_fmt.toUtf8(),variant_type.toUInt());
This works fine for the most part, especially when I have a floating point as the input. However, if my format string in this particular integer (.toUInt()) conversion case includes decimal formatting (e.g. "%.2f"), then I get a constant result of "0.00". This caught me by surprise as I expected to instead just get ".00" tacked onto the integer, as I have seen in other languages like Perl.
What am I missing here? Also, I know asprintf() was added fairly recently and the documentation already now advises to use QTextStream or arg() instead. I don't believe this to be an option, however, for me to use this style of format string. Thanks.
The format string is expecting a double, but you're providing an int. It works if you provide an actual double, like this:
QString result_str = QString::asprintf(disp_fmt.toUtf8(),variant_type.toDouble());
Also note, this behavior is identical to how the standard C library functions work (std::sprintf, etc).
I have a library that gives me a string_view. What's the best way to get it into a QString (not a QStringView)?
I made QString::fromStdString(std::string(key).c_str()), but is that the best?
Drop the c_str(), you don't need it, since fromStdString() takes a std::string (hence the name):
QString::fromStdString(std::string(key))
You can also drop the explicit string construction, since std::string can be constructed from a std::string_view:
QString::fromStdString(key)
That being said, if the std::string_view is null-terminated (which is not guaranteed), you can use the QString constructor that accepts a char*:
QString(key.data())
Or, if the std::string_view is encoded in Latin-1, you can use:
QString::fromLatin1(key.data(), key.size())
Or, if encoded in UTF-8:
QString::fromUtf8(key.data(), key.size())
Or, if encoded in the user's default locale:
QString::fromLocal8Bit(key.data(), key.size())
I'm receiving a binary thru FTP and storing it as a QString. The original image was BMP but to pass through FTP I converted it to QPixmap. I'm able to successfully receive this binary but I can't convert back to a QPixmap or some sort of format to display that image.
Does anyone have any suggestion?
1) Convert QString to QByteArray data with QString::toLatin1(), QString::toLocal8Bit(). Think work only one of methods
2) Read QByteArray with
bool QPixmap::loadFromData(const QByteArray & data, ...
Possibly functions in point one will encode data to another(text) encoding. Then try to convert QChar* from QString::data to QByteArray. Or write that QChar's into file and read QPixmap from file.
I'm converting some legacy code to ITK 4.7 for dicom manipulation. I'm reading private image tags but getting results in Base64 encoded format for few private tags.
I wondered about the value I'm getting for a tag as
MlwtNVwyNSA=
Basically it is encoded value for
2\-5\25 (in base64)
I know there is Base64.h that comes with gdcm library but question is is that header/functions part of ITK as well or do i need to create gdcm objects to convert the encoded values? or write my own C++ function for that conversion?
What shall be the most efficient (if not native) way within ITK 4.7 library?
By looking at the source code (gdcmBase64.h and .cxx), gdcm::Base64 is a self-contained class, which is independent from the rest of GDCM. Just #include "gdcmBase64.h", and call Encode and Decode as needed.
Alternatively, you can find implementations of base64 encoding and decoding and put them in your source file. base64 encoding is pretty simple.
I am obtaining the content from a QTextEdit object by using the following code:
QString text=my_QTextEdit.toPlainText();
What is the encoding that QTextEdit uses, a what encoding is used in the QString I get back from the toPlainText() call?
Thanks.
QTextEdit.toPlainText() returns a QString object, which is always a unicode character string (see documentation).
The QString class provides the functions toLatin1(), toAscii() and toUtf8(), which allow you to convert the string from unicode to an 8-bit string that you can process further. So Qt handles the encoding & decoding of the string for you.
If you want to create a QString instance from a given byte-string, you can use the functions fromAscii(), fromLatin1() or fromUtf8().
All controls in Qt are enabled for 16-bit characters. That means that content of a QTextEdit is Unicode (or UTF-32/UCS-4) (see also http://developer.nokia.com/Community/Discussion/showthread.php/215203-how-to-correctly-display-Unicodes-in-QPlainTextEdit).
When getting the content of a QTextEdit control (via plainText()), you get back a QString which contains Unicode.
From there on, you can convert to other format as you like: toUTF8(), toUCS4(), ...