qlist

Making a QList of an abstract class objects in C++/QT?

我与影子孤独终老i 提交于 2019-12-11 02:53:56
问题 although I've been helped countless times by other questions/answers here, this is my first question here, so don't be too harsh on me! :) I've been learning QT/C++ and let's assume I have something like this: class AbstractMasterClass{ public: virtual void foo(void) = 0; //Pure virtual method } This class will have plenty subclasses, each one of them implementing their own foo() method. And the question is: How can I create a QList which I'll populate with AbstractMasterClass's subclasses?

dynamic memory in QList

穿精又带淫゛_ 提交于 2019-12-09 19:06:56
问题 I don't have much experience with QT and this problem came out today. QList<int> memList; const int large = 100000; getchar(); for (int i=0; i<large; i++) { memList.append(i); } cout << memList.size() << endl; getchar(); for (int i=0; i<large; i++) { memList.removeLast(); } cout << memList.size() << endl; getchar(); After first loop when I check memory usage it goes up as new elements are appended to the memList but after removing them within second loop the memory usage stays at the same

View, edit and update data (from C++ ) in QML with multiple views, while the Data stays in C++ (subscribe to data)

和自甴很熟 提交于 2019-12-08 00:12:20
问题 I have some data stored in instances of a C++ class (Data.cpp). Now i want to be able to view and edit this data from 2 seperate representations in QML, so that if the values in View1 are changed, the data itself (C++) is changed and the value displayed by View2 as well (because it gets notified when the C++ data changes). Here is what I got so far: Data.h class Data : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) public: Data(std::string name);

What is the right way for an object to have a collection of a QObject derived class?

六眼飞鱼酱① 提交于 2019-12-07 18:18:48
问题 I'm trying to make a class exposing a collection(or several) of a QObject derived class(with its own qt properties) qt properties I can use in qml. According to http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#no-copy-constructor-or-assignment-operator qt doesn't play well with copy constructors. So I used QList<QObject derived class> (my first idea) I can't pass the list by reference(or at least I think thats what the compiler errors implies)(needs copies) and am having a hard time

Qt - QList const correctness

本小妞迷上赌 提交于 2019-12-07 02:54:53
问题 A QList<T *> can't easily be const-correct. Consider the function void f(QList<T *> list) { list[0]->constFunction(); } I can change f to void f(QList<const T *> list) but then I can't do f(QList<T *>()); //Compile error anymore, since the compiler can't implicitely cast QList<T *> to QList<const T *> . However, I can explicitely reinterpret-cast the QList as follows: template <typename T> inline QList<const T *> &constList(const QList<T *> &list) { return (QList<const T *> &)list; } This

View, edit and update data (from C++ ) in QML with multiple views, while the Data stays in C++ (subscribe to data)

ⅰ亾dé卋堺 提交于 2019-12-06 06:15:13
I have some data stored in instances of a C++ class (Data.cpp). Now i want to be able to view and edit this data from 2 seperate representations in QML, so that if the values in View1 are changed, the data itself (C++) is changed and the value displayed by View2 as well (because it gets notified when the C++ data changes). Here is what I got so far: Data.h class Data : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) public: Data(std::string name); QString name(); void setName(const QString &n); signals: void nameChanged(); private: std::string

Constant class members, assignment operator and QList

北城余情 提交于 2019-12-06 03:38:36
问题 Please conform if I am correct and tell me whether there is a better solution: I understand that objects with constant members like int const width; can not be handled by the synthetic assignment operator that is implicitly created by the compiler. But QList (and I suppose std::list, too) needs a working assignment operator. So when I want to use objects with constant members and QList I have three possibilities: Don't use constant members. (Not a solution) Implement my own assignment

Qt - QList const correctness

亡梦爱人 提交于 2019-12-05 06:35:24
A QList<T *> can't easily be const-correct. Consider the function void f(QList<T *> list) { list[0]->constFunction(); } I can change f to void f(QList<const T *> list) but then I can't do f(QList<T *>()); //Compile error anymore, since the compiler can't implicitely cast QList<T *> to QList<const T *> . However, I can explicitely reinterpret-cast the QList as follows: template <typename T> inline QList<const T *> &constList(const QList<T *> &list) { return (QList<const T *> &)list; } This enables me to use the constList template function to cast any QList<T *> into a QList<const T *> , as in f

dynamic memory in QList

青春壹個敷衍的年華 提交于 2019-12-04 15:21:53
I don't have much experience with QT and this problem came out today. QList<int> memList; const int large = 100000; getchar(); for (int i=0; i<large; i++) { memList.append(i); } cout << memList.size() << endl; getchar(); for (int i=0; i<large; i++) { memList.removeLast(); } cout << memList.size() << endl; getchar(); After first loop when I check memory usage it goes up as new elements are appended to the memList but after removing them within second loop the memory usage stays at the same level. I thought that QList was dynamic and it would free memory when element is removed. So either I'm

QList and delete

久未见 提交于 2019-12-04 10:49:34
问题 I have a QList with pointers to objects with class type Model . I would like to delete appropriately this QList after it has being used. I know Qt philosophy is to avoid C-style memory management. How do I delete this QList ? 回答1: You could use qDeleteAll: qDeleteAll(lstMdls); lstMdls.clear(); 回答2: As seen from an earlier revision, this was OP's approach: QList<Model*>lstMdls; get Data(lstMdls); /* * Do other things */ for(int i=0;i<lstMlds.size();i++) { delete lstMdls.at(i); } 来源: https:/