Populating a QStandardItemModel with data

  • Please help with populating a QTableWidget with data from a QStandardItemModel. I have looked at the documentation and can't seem to figure it out. This is my attempt at it:

    @void Widget::setupGui()
    model = new QStandardItemModel(this);
    model->setHeaderData(1,Qt::Horizontal,QString("Album Title"),Qt::DisplayRole);
    model->setHeaderData(2,Qt::Horizontal,QString("Replacement Cost"),Qt::DisplayRole);
    QHeaderView* hv = ui->tableWidget->horizontalHeader();

    CDInfo *cd1 = new CDInfo("Abba","Essential Collection",120.89,7);
    CDInfo *cd2 = new CDInfo("Hugh Masekela","Jabulani",199.05,6);
    CDInfo *cd3 = new CDInfo("Karl Jenkins","Stabat Mater",256.00,9);
    for (int row = 0; row < 3; ++row) {
                for (int column = 0; column < 4; ++column) {
                    QModelIndex index = model->index(row, column, QModelIndex());
                    model->setData(index, QVariant((row+1) * (column+1)));


  • Lifetime Qt Champion


    Since you have a QStandardItemModel, why don't you use a QTableView ?

  • Are you saying that it can't be done with a QStandardItemModel?

  • Lifetime Qt Champion

    No, I am just asking why you are using a QTableWidget since your are using a QStandardItemModel.

    Either use QTableView + QStandardItemModel or use only QTableWidget.

    QTableWidget already has a model

  • oh alright thanks

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.