-2

I have a previous code written in QT. The output generated is very slow. I have a very basic knowledge of QT. I wish to know the of QByteArray object and QChar to normal c++ equivalent.

The Datapackage class in Qt.

class Datapackage
{
public:
    Datapackage(QByteArray datas,int start);
    QVector<double> getX() const;
    QVector<double> getY() const;

private:
    QVector<QChar> intensity;
    QVector<double>x;
    QVector<double>y;
    qint8 header[288];
};

My tried normal c++ Datapackage class

class Datapackage
{
public:
    Datapackage( std::vector<char> datas , int start );
    std::vector<double> getX() const;
    std::vector<double> getY() const;

private:
    std::vector<char> intensity;
    std::vector<double> x;
    std::vector<double> y;
   int8_t header[288];

};

My results are absurd. I am doing the wrong conversion. Could you suggest a good way. The above class is not a complete code. I removed some data not to make the code too long.

AAEM
  • 1,837
  • 2
  • 18
  • 26
Launa
  • 183
  • 2
  • 13
  • Are you trying to change all instances of QByteArrays to vectors in your code? or do you want to convert the values and then do your operations on vectors/chars? – Olivier Poulin Jun 22 '15 at 13:31
  • 1
    please expand on "absurd" – Andy Newman Jun 22 '15 at 13:32
  • Maybe you should use char* QByteArray::data() method? Normal equivalent is std::string or std::wstring, based on character set you used. – UndeadDragon Jun 22 '15 at 13:33
  • @Andy Newman : The absurd results are as . I get the result with QT as x = 0.0941 which is the correct . while with my conversion with normal c++ code I get the result as x = -18.493 and sometimes pecuiliar symobol as smiley and arrows. – Launa Jun 22 '15 at 13:36
  • 4
    @Launa : Give us code sample. And right spelling for library is Qt, QT - Quick Time – UndeadDragon Jun 22 '15 at 13:37
  • @Olivier Poulin: I want to change all instance of QByteArrays to suitable c++ data. Changing QString to std::string , and other wont give an incorrect result. But the above two conversion is generating an error. – Launa Jun 22 '15 at 13:38
  • 1
    We need a code example. You are talking about an array but then telling me about a float? What line of code do you execute, what output do you expect, and what do you get instead? – Andy Newman Jun 22 '15 at 13:40
  • 2
    **You've put the cart in front of your horse.** You assume, without any ground for it, that `QByteArray` is your problem. Most likely it isn't, you're doing something wrong, and we won't know until you show us the code. – Kuba hasn't forgotten Monica Jun 22 '15 at 13:46
  • I tried to submit a code in the comment . It's not allowing due to character limit . – Launa Jun 22 '15 at 13:53
  • 1
    Edit the code into your question, adding code to comments makes it unreadable. – cmannett85 Jun 22 '15 at 14:11
  • The code you're giving us right now looks fine, can you give us more? – Olivier Poulin Jun 22 '15 at 14:28
  • You're looking for [this answer](http://stackoverflow.com/a/21216034/1329652), I think, and I also think that this is really a duplicate of [this question](http://stackoverflow.com/q/21208733/1329652). Your problem is that of storing data structures in a file or some other generic stream-of-bytes. – Kuba hasn't forgotten Monica Jun 22 '15 at 15:09
  • @Kuba Ober :) not. I just want to transfer the code in Qt to normal c++ code. I am doing a hit and trial . Other conversion as Qstring to std::string. Qvector to std::vector works fine. I tried as you said a try to wchar_t for QChar. But found wchar_t is of size of 2 bytes and QChar of 1. I am only having problem at conversion with QbyteArray and QChar . Looking the Qt documentation I tried to replace them by std::vector and char .. though a wrong conversion. – Launa Jun 22 '15 at 15:16
  • @Launa But what are you doing with that data? And *what code* does show bad results? And how can intensity be stored as a vector of chars?! Remember: the problem isn't in your data structure, it is *elsewhere*, where you actually try to use that data, or do I/O with it (file, network, etc.). – Kuba hasn't forgotten Monica Jun 22 '15 at 15:18
  • @Kuba Ober: The intensity is cast to double afterwards. As you said I will check the other parts again and find the errors. – Launa Jun 22 '15 at 15:32
  • @Launa I have no idea what would make someone "cast" a `QChar` (an opaque class) to a double. That's just ridiculous. Never mind that a `QChar` is usually smaller than a double, so you're casting junk - unless you meant casting `QChar`'s *value*. Even then, the use of `QChar` is pure nonsense unless you're not telling us something... – Kuba hasn't forgotten Monica Jun 22 '15 at 15:39
  • @Kuba Ober My apology the intensity is cast to int8_t afterwards not to double . I did the following coversion is it correct ? QDataStream ds(datas.right(datas.size()-start)); ds.setByteOrder(QDataStream::LittleEndian); std::stringstream ds( std::string (datas.rbegin() , datas.rend() - 1) ); // tried normal c++ conversion of above 2 lines – Launa Jun 22 '15 at 15:47

2 Answers2

4

QChar is not char.

QByteArray is not std::vector<char>.

A QChar is more like wchar_t, although it is a class and offers more functionality than merely storing characters. Don't forget that a QChar stores a UTF-16 code unit. Some Unicode code points are stored as two UTF-16 code units (called a surrogate pair). So you should never assume that one QChar represents a complete code point. Similarly, you should never assume that one code point represents a character. Some characters are represented by multiple code points. In general:

number of printable characters <= number of code points <= number of code units (QChars)

A QByteArray offers functionality beyond that of std::vector, i.e. usable with QDataStream etc.

I have a previous code written in QT. The output generated is very slow.

Your problem is that you're not showing us any of that code, and you're presupposing a solution without understanding what the problem is. You've already decided that std::vector will be a solution, but you have really no idea where the problem is.

We'll need to see a minimal example that reproduces the problem, and only then can one decide whether QByteArray itself is the culprit, or (more likely) the incorrect way that you've designed your solution.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • I have stated that I am doing the wrong conversion. QChar is obviously not a char. I assumed char could replace it by somemeans. Watching the output of my code. I know that it is not a suitable conversion. QByteArray has a lot of features as seen in the Qt documentation. It was just a try , a wrong try. I couldn't show the minimal example since the code is too long. – Launa Jun 22 '15 at 14:52
0

If you're trying to change all instances of QByteArrays into std::vectors and QChars into chars in your code, then you're going to have to give us some errors that you're getting for us to help you.

However, if you want to do a conversion to vectors/chars and then do your computations on those, then you can use these:

std::vector<unsigned char> newVector(
oldByteArray.begin(), oldByteArray.end());

and

myChar = oldQChar.toAscii();
or
myChar = oldQChar.toLatin1();
AAEM
  • 1,837
  • 2
  • 18
  • 26
Olivier Poulin
  • 1,778
  • 8
  • 15