Qt World Summit: Submit your Presentation

Populating a TableWidget...Best Way of doing it?

  • I have a data set, in the form of a matrix... column names are people names, row names are certain attributes... each cell could be a list of 3 radio buttons OR a dropdown box. So i was thinking of using a TableWidget for all of this.

    column names are read from file, row names are passed from another function.

    should i use TableWidgetItem to add the cells and information? or use the StandardItemModel to add cells and information?

    and can somebody show me some sample code in adding such information??

    thanks a bunch!

  • I have tried with both TabelWidgetItem and StandardItemModel, both of them dont work....
    @QFile file("../engD.txt");
    if (!file.exists())
    QMessageBox error;
    if (!file.open(QFile::ReadOnly|QFile::Text))
    QTextStream stream(&file);
    QString mText;
    while (!stream.atEnd())
    for (int row =0; row<4; row++)
    for (int col =0; col<4;col++)
    mText= stream.readLine();
    QTableWidgetItem *newItem = new QTableWidgetItem;
    ui->tableWidget->setItem(row, col, newItem);

    QStandardItemModel *model = new QStandardItemModel(4,4);
    file.close(); @

  • First, please re-edit your post an indent your code, it's quite unreadable this way.

    For your actual problem:
    You set the items in your loops and in the end you destroy all the work you've done by setting a new (and empty!) model on your table widget.

    The way to go is:

    Either use [[Doc:QTableWidget]] together with [[Doc:QTableWidgetItem]]s

    Or use [[Doc:QTableView]] together with a [[Doc:QStandardItemModel]]

    You should not mix a QTableWidget with any item model, being it standard or custom.

Log in to reply