[Solved] Help using QStandardItemModel



  • Hi, I have the following code, taken from SpinBoxDelegate Example, but in my example, the QTableView does not shown the data. Any clue?

    Best regards.

    David

    @
    mdiMain = new QMdiArea;
    mdiMain->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiMain->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(mdiMain);

    QMdiSubWindow *sub = new QMdiSubWindow;
    QTableView *table = new QTableView;
    QStandardItemModel model(4, 2);
    for (int row = 0; row < 4; ++row)
    for (int column = 0; column < 2; ++column)
    model.setData(model.index(row, column, QModelIndex()), QVariant((row+1) * (column+1)));
    table->setModel(&model);
    sub->setWidget(table);
    sub->show();
    @



  • setRole for your data. For example Qt::DisplayRole.

    @
    model.setData(model.index(row, column, QModelIndex()), QVariant((row+1) * (column+1)), Qt::DisplayRole);
    @



  • Hi stima_ua, thanks for your replay, but it did not work.



  • How long is your model alive!?!?



  • Hi Scylla, thanks for your replay.

    What do you mean how long is my model alive?

    I have noticed that in my example, when I refresh the model contents by filling it, the QTableView seems to be filled but just for about a second.



  • I don't know where your code is located. If it's not in the main() your model will die on the end of the scope. That's what I mean.

    So if i's not your issue, please show more code.



  • Great!!! Scylla.

    You hit it, :-) in my example model has to be declared in the same place mdiMain was decladed.

    I really appreciate your help and stima_ua, thanks for your help too.

    @
    mdiMain = new QMdiArea;
    mdiMain->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiMain->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(mdiMain);

    QMdiSubWindow *sub = new QMdiSubWindow;
    QTableView *table = new QTableView;
    model = new QStandardItemModel(4, 2);
    for (int row = 0; row < 4; ++row)
    for (int column = 0; column < 2; ++column)
    model.setData(model.index(row, column, QModelIndex()), QVariant((row+1) * (column+1)));
    table->setModel(&model);
    sub->setWidget(table);
    sub->show();
    @



  • Now, can I add a QIcon to the vertical o horizontal headers like in QTableWidget? I mean, can I do something like this with QTableView?

    @
    result->setHorizontalHeaderItem(row, new QTableWidgetItem(QIcon(":/MyIcon.png"), "Header"));
    @



  • somthing like that:
    @m_model->horizontalHeaderItem(0)->setIcon(QIcon(":/ressources/clean.png"));@



  • Read manual.
    @
    QTableWidgetItem *item = new QTableWidgetItem;
    item->setData(QVariant(), Role);
    item->setIcon(path);
    @


Log in to reply
 

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