3

What is the easiest way to print a vector from the Eigen library in a Qt interface?

Is there an easy way to convert the vector to a QString so that I can use setText()?

Or is there an easier way of doing it?

László Papp
  • 51,870
  • 39
  • 111
  • 135
user3482499
  • 123
  • 1
  • 8
  • Cannot you use QVectorXD? Is it some performance critical stuff? In any case, please give some input and expected output examples. What format would you like to get it printed in? – László Papp May 25 '14 at 11:11
  • @LaszloPapp I did some computations using Eigen, so therefore I have everything in such a vector, but if I could somehow cleverly transfer it into the format that you suggest that would be fine. Performance is not a big issue. I would want the entries of the vector to be printed underneath eachother. Since there are not many entries in the vector, I thought of using Qlabels. But if there is a more clever way to print them all at once I'd prefer that. – user3482499 May 25 '14 at 11:13
  • Use a QtextOStream and operator<< maybe? – Marc Glisse May 25 '14 at 11:30
  • @MarcGlisse: That you will not use with setText() without additional work. – László Papp May 25 '14 at 11:30
  • @LaszloPapp ok, I am not familiar with Qt, but since Eigen provides an operator<< it would be sad if Qt had no way to use that. – Marc Glisse May 25 '14 at 11:34
  • @MarcGlisse: but that is for stream, not strings, but if operator<< is overloaded the way OP wants, you could pass your QString as the constructor, yes, you are right. – László Papp May 25 '14 at 11:36

1 Answers1

1

I would write the following for an N dimension vector:

QString myString;
for (int i = 0; i < svector; ++i)
    myString.append(QString(vector[i]) + "\n");
myLabel.setText(myString);
László Papp
  • 51,870
  • 39
  • 111
  • 135