qvector

Populating QVector from QList

六眼飞鱼酱① 提交于 2020-01-04 08:16:28
问题 I have a QList and QVector. I populate Qlist and then tried to copy to QVector. Ovector has a fromList() method. But it do not work. My code : QList<int> listA; QVector<int> vectorA; //I also tried QVector<int>vectorA(100); for(int i=0; i<100; i++) { listA.append(i); } vectorA.fromList(listA); After this code vectorA returns empty I use Qt 4.8.5 under Linux 回答1: You should write: vectorA = QVector::fromList(listA); as fromList is a static method of the class QVector . The code you wrote

A function template that accepts both std::vector and QVector?

纵饮孤独 提交于 2020-01-04 05:24:11
问题 Suppose I have a function called loadData() which takes a container (to be filled with data) and a CSV file. I need the following overloads: loadData(std::vector<double>& data, const std::string& file); loadData(QVector<double>& data, const std::string& file); loadData(std::vector<std::complex<double>>& data, const std::string& file); loadData(QVector<std::complex<double>>& data, const std::string& file); loadData(std::vector<std::vector<double>>& data, const std::string& file); loadData

Change object of a QVector in an ordinary for loop

自古美人都是妖i 提交于 2019-12-13 07:06:28
问题 If I have a QVector I can use a range based loop, use a reference and change the objects in the QVector. But in the case where I need the index while modifying the object I have to use an ordinary for loop. But how can I then change the value of the object in the QVector? As workaround I used the replace method after changing the temporary object but that is kind of ugly. This is the code: struct Resource { int value = 0; }; int main(int argc, char *argv[]) { QVector<Resource> vector{Resource

Sort vectors by more conditions than one

隐身守侯 提交于 2019-12-12 03:44:01
问题 I asked in other post about: my_old_post But now I need more complex condition to sort my vector. I have a vector like this: vector_points_original. Then if I sort it for z of each point I have other vector like: vector_points_sorted_by_Z. But I need vector_sorted_by_z and after sort first four points and second four points by y component. Could you help me? 回答1: std::vector<CartesianPoint>v{...};//place your values here //First, sort by Z std::sort(v.begin(), v.end(), [](const auto& p1,

unique_ptr, qvector.resize() throws error 2280 attempting to reference a deleted function

删除回忆录丶 提交于 2019-12-11 07:35:43
问题 To prevent scope creep (on a previous Q), I've isolated the above error. My Voxel class definition: #ifndef VOXEL_H #define VOXEL_H #include <QObject> #include <QVector> #include <iostream> include <memory> class Voxel : public QObject { Q_OBJECT public: Voxel(); ~Voxel(); }; #endif // VOXEL_H The main file which triggers the error: #include <voxel.h> int main(int argc, char *argv[]) { QVector < QVector <std::unique_ptr <Voxel> > > cVoxel; cVoxel.resize(0); int rows = 80, cols = 80; for (int

QCache and QSharedPointer

 ̄綄美尐妖づ 提交于 2019-12-10 10:25:58
问题 The problem is, that I have a QVector of QSharedPointer and I want to put part of them to my QCache . How can I insert a shared pointer to QCache ? What happens if I set the pointer to my cache but destroy the QVector and the shared pointer in it? Will the memory be released and my cache points to nowhere? 回答1: It is a bug if you put just a pointer to your item to QChache and at the same time such pointer is managed by QSharedPointer . The item object can be destroyed by QSharedPointer

QCache and QSharedPointer

会有一股神秘感。 提交于 2019-12-05 18:29:42
The problem is, that I have a QVector of QSharedPointer and I want to put part of them to my QCache . How can I insert a shared pointer to QCache ? What happens if I set the pointer to my cache but destroy the QVector and the shared pointer in it? Will the memory be released and my cache points to nowhere? It is a bug if you put just a pointer to your item to QChache and at the same time such pointer is managed by QSharedPointer . The item object can be destroyed by QSharedPointer destructor, so QChache will have invalid pointer. It is a generic issue that you cannot have different owners of a

What is the reason of QVector's requirement for default constructor?

旧城冷巷雨未停 提交于 2019-11-29 10:31:48
I can see that that classes are treated as complex objects which are required for calling default constructor: void QVector<T>::defaultConstruct(T *from, T *to) { if (QTypeInfo<T>::isComplex) { while (from != to) { new (from++) T(); } ... } But it's not clear why is it needed to construct objects in the 'hidden' area of QVector. I mean these objects are not accessible at all, so why not just to reserve the memory instead of the real object creation? And as a bonus question, I would like to ask, if I want to have an array of non-default-constractible objects, can I safely replace QVector<T>

QList vs QVector revisited

橙三吉。 提交于 2019-11-28 20:23:06
问题 My question is basically when to choose QVector and when to choose QList as your Qt container. What I already know: Qt docs: QList class For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory. It also expands to less code in your executable. The same is written is this very popular Q&A: QVector vs QList. It also favors QList. But: on

Why do Qt's container classes not allow movable, non-copyable element types?

◇◆丶佛笑我妖孽 提交于 2019-11-28 13:18:48
The Qt container classes QList<T> , QVector<T> etc. require their element types to be copyable. Since C++11, the STL containers require their element type to be copyable or movable only. Why do the Qt containers not support move-only element types? Qt bug #54685 has explicit confirmation from Qt developers that move-only types are not (and will never be) supported because of Qt containers' principle of implicit sharing. When you copy one Qt container to another, you're not doing a deep copy—the containers share their contents internally. Only when a modifying function is called on a container