2

I'm trying to create a Qvector from and std::Vector.

I can of course copy it element by element using a for loop, but I'm looking for a better way to do it.

So I tried:

QVector<double> qv  = QVector<double>::fromStdVector(values) ;

This works fine but I get a warning that it's deprecated.

I followed Hadi Navapour's suggestion from copying a std::vector to a qvector

and tried the following:

    QVector<double> qv  = QVector<double>(values.begin(), values.end());

but this code with begin() and end() does not compile, I get the following error:

error: no matching function for call to ‘QVector::QVector(std::vector::iterator, std::vector::iterator)’ QVector qv = QVector(values.begin(), values.end());

I believe this is supposed to use the following constructor QVector(InputIterator first, InputIterator last)

I can't get it to work.

Could someone please explain the right non-deprecated way to copy an std::vector to a Qvector?

Thanks in advance!

user206904
  • 504
  • 4
  • 16
  • The `QVector` constructor taking a pair of iterators was introduced in Qt 5.14. You are probably using an earlier version. – Igor Tandetnik Dec 25 '20 at 21:23
  • good point, I will check my qt version once I'm on my pc that has QT again and get back to you! but assuming I'm using a version earlier than 5.14, and don't want to use the deprecated fromStdVector, what should I do? – user206904 Dec 25 '20 at 21:37
  • 2
    Well, `fromStdVector` is deprecated as of Qt 5.14, precisely because a replacement is made available. If you can't use either, then you are left with pushing elements one by one in a loop; you could write a helper function for that, similar to `fromStdVector` – Igor Tandetnik Dec 25 '20 at 21:46
  • `Qvector`, `qvector`, `std::Vector` -- who cares, as long as it compiles? Oh, wait... – TonyK Dec 26 '20 at 00:08
  • @IgorTandetnik, just checked my qt creator help > about, "Based on Qt 5.15.1 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)" my qt 5.15 should be able to support the iterators constructor... I can do a manual copy or just use the deprecated function, I'm just curious to know why I'm getting this weird behavior... – user206904 Dec 26 '20 at 11:18
  • Iterator constructor works for me here, building with 5.15.1. You must be including wrong headers from older version somehow. Double-check your build configuration. – Igor Tandetnik Dec 26 '20 at 13:04
  • Can you try a newer GCC Version? Despite not explicitly stating it, GCC 5 is just C++11, while if I recall correctly, Qt may need C++14 for such range constructor. – Pato Sandaña Jan 07 '21 at 13:54

1 Answers1

2

The documentation https://doc.qt.io/qt-5/qvector.html#fromStdVector enforces to use range constructors from version 5.14.

so may be you can use version flags for your implementation.

try as said below, if you are interested.

#if QT_VERSION <= QT_VERSION_CHECK(5,14,0)
    QVector<double> qv  = QVector<double>::fromStdVector(values) ;
#else
    QVector<double> qv  = QVector<double>(values.begin(), values.end());
#endif
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • 1
    thanks for your reply, as I just mentioned in my comment on the original post my qt version is 5.15, it should be able to support the iterators constructor... I can do a manual copy or just use the deprecated function, I'm just curious to know why I'm getting this weird behavior... – user206904 Dec 26 '20 at 11:24