PDA

View Full Version : Qtableview add item and reload table



GeneCode
21st September 2017, 08:45
I use QStandardItemModel to display data in QTableVIew.
i set already tableview model to the itemmodel


table->setModel(yourMomModel);

then i added the item in model


yourMomModel.append(item2);

but the data not showing in QtableView.
anybody know whats wrong thanks.

Santosh Reddy
21st September 2017, 08:59
How did you create the model and item?

d_stranz
21st September 2017, 19:13
These two lines of code are incompatible:



table->setModel(yourMomModel);

// and

yourMomModel.append(item2);


In the first line QAbstractItemView::setModel() takes a pointer to a model. In the second line, you use "dot" notation to dereference, which implies "yourMomModel" is a stack or member variable, not a pointer. Which is it? Do they refer to the same instance?

GeneCode
25th September 2017, 03:45
nvm. i solved it.

GeneCode
26th September 2017, 03:54
Here's how i did it:

I used QstandardItemModel and add QStandardItem to it.
then apply the model to my QTableView.

code:


// header
private:
QStandardItemModel *myModel



// cpp
// in constructor
myModel = new QStandardItemModel(0, 0, this); // an empty model

ui->myTableView->setModel(myModel);

// create the item
QStandardItem *item1 = new QStandardItem("Test");

// set the item to the model
myModel->setItem(0,0,item1); // 0,0 is row/colum.

d_stranz
26th September 2017, 18:58
Ah, this is a common misunderstanding. You have created an empty model (0 rows and 0 columns). Calling setItem( row, column, item ) requires that the row and column already exist. That is not the case for an empty model, so nothing happens.

You should take one of the following steps:

1 - create your model with non-zero row and column counts
2 - use setRowCount() and setColumnCount() to grow the model as needed
3 - use appendRow() or appendColumn() to add items and grow the model at the same time

A typical scenario is to create a model with 0 rows and "n" columns (since you usually know how many columns your table will have). Then you create the list of new items for a row and call appendRow() with the list. That sticks the row onto the end of the model and grows rowCount() by 1.

GeneCode
27th September 2017, 02:20
Ah, this is a common misunderstanding. You have created an empty model (0 rows and 0 columns). Calling setItem( row, column, item ) requires that the row and column already exist. That is not the case for an empty model, so nothing happens.

You should take one of the following steps:

1 - create your model with non-zero row and column counts
2 - use setRowCount() and setColumnCount() to grow the model as needed
3 - use appendRow() or appendColumn() to add items and grow the model at the same time

A typical scenario is to create a model with 0 rows and "n" columns (since you usually know how many columns your table will have). Then you create the list of new items for a row and call appendRow() with the list. That sticks the row onto the end of the model and grows rowCount() by 1.

Actually what I need was the tableview start as empty. And then I setitem starting from 0,0. Seems to add items to it nicely.

d_stranz
27th September 2017, 17:12
void QStandardItemModel::setItem(int row, int column, QStandardItem *item)

Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item.

You are correct, my mistake. I rarely use QStandardItemModel, which behaves differently in this regard. Other types of models typically require the code to set the size of the model before adding to it.