実行時に項目を QList に追加しようとしていますが、エラー メッセージが表示されます。基本的に私がやろうとしているのは、QLists の QList を作成し、いくつかの customClass オブジェクトを各内部リストに追加することです。これが私のコードです:
ウィジェット.h:
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
public slots:
static QList<QList<customClass> > testlist(){
QList<QList<customClass> > mylist;
for(int w=0 ; w<5 ; w++){
mylist.append(QList<customClass>());
}
for(int z=0 ; z<mylist.size() ; z++){
for(int x=0 ; x<10 ; x++){
customClass co = customClass();
mylist.at(z).append(co);
}
}
return mylist;
}
};
customclass.h:
class customClass
{
public:
customClass(){
this->varInt = 1;
this->varQString = "hello world";
}
int varInt;
QString varQString;
};
main.cpp:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QList<QList<customClass> > list;
list = w.testlist();
w.show();
return a.exec();
}
しかし、アプリケーションをコンパイルしようとすると、次のエラーが発生します。
error: passing `const QList<customClass>' as `this' argument of `void List<T>::append(const T&) [with T = customClass]' discards qualifiers
また、foreach を使用してオブジェクトを挿入しようとしました。
foreach(QList<customClass> list, mylist){
for(int x=0 ; x<10 ; x++){
list.append(customClass());
}
}
エラーはなくなりましたが、customClass オブジェクトは追加されませんでした。メインのデバッグ ループを使用して、内側の QList のサイズがゼロであることを確認できました。私は何を間違っていますか?