Appending a QList<QStringList>
-
I have the following code.
QStringList configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ configorder.append(ui->tableWidget_Configs->item(i,1)->text()); } //Followed by a function that takes a QStringList
I want to swap from a QStringList to a QList<QStringList> so I can hold multiple variables into the same object.
QList<QStringList> configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ configorder.at(i).append(ui->tableWidget_Configs->item(i, 1)->text()); //Error configorder.at(i).append(ui->tableWidget_Configs->item(i, 2)->text()); //Error configorder.at(i).append(ui->tableWidget_Configs->item(i, 3)->text()); //Error } //Followed by a function that takes a QList<QStringList>
I am encountering this error.
error: no matching member function for call to 'append' qlist.h:210:10: note: candidate function not viable: no known conversion from 'const QStringList' to 'QList<QString>' for object argument qlist.h:211:10: note: candidate function not viable: no known conversion from 'const QStringList' to 'QList<QString>' for object argument
Per my understanding of C++, QList<QStringList> is being declared in a way such that QStringList has to be a const. I am unsure how to declare QList<QStringList> in such a way such that it has no const, since I did not declare it as such.
My workaround:
QList<QStringList> configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ QStringList stringList; stringList.append(ui->tableWidget_Configs->item(i,1)->text()); stringList.append(ui->tableWidget_Configs->item(i,2)->text()); stringList.append(ui->tableWidget_Configs->item(i,3)->text()); configorder.append(stringList); } //This works.
Furthermore, I am unsure why my workaround worked, while the original did not. I did not find any documentation on QList<QStringList> being always a const so I am unsure why original code did not work. Please let me know if more info is required.
-
I have the following code.
QStringList configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ configorder.append(ui->tableWidget_Configs->item(i,1)->text()); } //Followed by a function that takes a QStringList
I want to swap from a QStringList to a QList<QStringList> so I can hold multiple variables into the same object.
QList<QStringList> configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ configorder.at(i).append(ui->tableWidget_Configs->item(i, 1)->text()); //Error configorder.at(i).append(ui->tableWidget_Configs->item(i, 2)->text()); //Error configorder.at(i).append(ui->tableWidget_Configs->item(i, 3)->text()); //Error } //Followed by a function that takes a QList<QStringList>
I am encountering this error.
error: no matching member function for call to 'append' qlist.h:210:10: note: candidate function not viable: no known conversion from 'const QStringList' to 'QList<QString>' for object argument qlist.h:211:10: note: candidate function not viable: no known conversion from 'const QStringList' to 'QList<QString>' for object argument
Per my understanding of C++, QList<QStringList> is being declared in a way such that QStringList has to be a const. I am unsure how to declare QList<QStringList> in such a way such that it has no const, since I did not declare it as such.
My workaround:
QList<QStringList> configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ QStringList stringList; stringList.append(ui->tableWidget_Configs->item(i,1)->text()); stringList.append(ui->tableWidget_Configs->item(i,2)->text()); stringList.append(ui->tableWidget_Configs->item(i,3)->text()); configorder.append(stringList); } //This works.
Furthermore, I am unsure why my workaround worked, while the original did not. I did not find any documentation on QList<QStringList> being always a const so I am unsure why original code did not work. Please let me know if more info is required.
@Dummie1138 If you read documentation: https://doc.qt.io/qt-5/qlist.html#at you will see that at() returns a CONST reference. Simply use [] instead of at...
-
I have the following code.
QStringList configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ configorder.append(ui->tableWidget_Configs->item(i,1)->text()); } //Followed by a function that takes a QStringList
I want to swap from a QStringList to a QList<QStringList> so I can hold multiple variables into the same object.
QList<QStringList> configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ configorder.at(i).append(ui->tableWidget_Configs->item(i, 1)->text()); //Error configorder.at(i).append(ui->tableWidget_Configs->item(i, 2)->text()); //Error configorder.at(i).append(ui->tableWidget_Configs->item(i, 3)->text()); //Error } //Followed by a function that takes a QList<QStringList>
I am encountering this error.
error: no matching member function for call to 'append' qlist.h:210:10: note: candidate function not viable: no known conversion from 'const QStringList' to 'QList<QString>' for object argument qlist.h:211:10: note: candidate function not viable: no known conversion from 'const QStringList' to 'QList<QString>' for object argument
Per my understanding of C++, QList<QStringList> is being declared in a way such that QStringList has to be a const. I am unsure how to declare QList<QStringList> in such a way such that it has no const, since I did not declare it as such.
My workaround:
QList<QStringList> configorder; for (int i = 0; i < ui->tableWidget_Configs->rowCount(); i++){ QStringList stringList; stringList.append(ui->tableWidget_Configs->item(i,1)->text()); stringList.append(ui->tableWidget_Configs->item(i,2)->text()); stringList.append(ui->tableWidget_Configs->item(i,3)->text()); configorder.append(stringList); } //This works.
Furthermore, I am unsure why my workaround worked, while the original did not. I did not find any documentation on QList<QStringList> being always a const so I am unsure why original code did not work. Please let me know if more info is required.
@Dummie1138
In addition to @jsulm. Your "workaround" is good. It takes advantage of void QList::append(const QList<T> &value), which allows you to append one list to another (of the same type) without iterating adding each member individually. You could even just go:configorder.append( { ui->tableWidget_Configs->item(i,1)->text(), ui->tableWidget_Configs->item(i,2)->text(), ui->tableWidget_Configs->item(i,3)->text() } );
-
D Dummie1138 has marked this topic as solved on