Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Appending a QList<QStringList>
Forum Updated to NodeBB v4.3 + New Features

Appending a QList<QStringList>

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 878 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on last edited by
    #1

    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.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • Dummie1138D Dummie1138

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • Dummie1138D Dummie1138

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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() } );
        
        1 Reply Last reply
        1
        • Dummie1138D Dummie1138 has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved