Nominate our 2022 Qt Champions!

How to insert a row at a specific location in QTable View and QTreeView

  • Hi All
    I have QTablview Vie for example with 5 columns
    1 A F G H L
    2 A U J K P
    3 A U L I M
    4 A U L O P
    5 A T Y P Q

    I want to insert a row between 2 and 3 . Could you please let me know how to do that

    The new QTableView should be
    0 A B C D E
    1 A F G H L
    2 A U J K P
    3 A J L I N
    4 A U L I M
    5 A U L O P
    6 A T Y P Q

  • You can use insertRow function to insert a new row before index 3 and use setItem to insert a new QStandardItem in each element of new row.

    >> Example:

        model->setItem(3, 0, new QStandardItem("A"));
        model->setItem(3, 1, new QStandardItem("J"));
        model->setItem(3, 2, new QStandardItem("L"));
        model->setItem(3, 3, new QStandardItem("I"));
        model->setItem(3, 4, new QStandardItem("N"));

    >> Read More:

Log in to reply