ner
ner

Reputation: 711

create a table in Qt and fill it by the user

i want to create an Qt application containing a table of 3 columns and n row, the user will choose the number of row by putting it in the edit button and the table will have 3 columns and the number given by the user. then fill it with element

i have search a lot but only found how to fill the able with sql data,

please is anybody having an idea?

here is what i did so far, i have fixed the number of rows and columns but it is not what i want, besides, i want to use either QtableWidget or QtavleViewItem

 int n;

        n = ui->spinBox->value();
    QStandardItemModel *model = new QStandardItemModel(n,3,this); //2 Rows and 3 Columns
     model->setHorizontalHeaderItem(0, new QStandardItem(QString("x")));
     model->setHorizontalHeaderItem(1, new QStandardItem(QString("y")));
     model->setHorizontalHeaderItem(2, new QStandardItem(QString("z")));


    ui->tableView->setModel(model);

Upvotes: 0

Views: 1670

Answers (1)

Quim
Quim

Reputation: 66

you can go through the elements in a QTableView and do things with them:

for(int r=0; r<N_ROWS; r++)
{
    for(int c=0; c<N_COLS; c++)
    {
        QModelIndex index = ui->tableView->model()->index(r,c, QModelIndex());

        // Do something with the QVariant that index.data() returns
        qDebug() << r << c << index.data().toString();
    }
}

Regards.

Upvotes: 1

Related Questions