I used ActiveRow variable to store the last row number,but it's not really necessary.Don't quite understood your last question but...seems like you need a little bit more explanation.
This is what your main widget class might look like,for time sake i'm gonna put all in constructor :
{
model=new CustomModel( rows , columns , this)
view->setModel(model);
connect(createButton,SIGNAL( clicked() ),SLOT( createRow_Slot() ) )
connect(createButton,SIGNAL( clicked() ),SLOT( createRow_Slot() ) )
mainLayout->addWidget(view);
mainLayout->addWidget(createButton);
mainLayout->addWidget(submitButton);
}
MainWidget::MainWidget(QWidget *parent):QWidget(parent)
{
view=new QTableView;
model=new CustomModel( rows , columns , this)
view->setModel(model);
createButton=new QPushButton(tr(" New Row "));
connect(createButton,SIGNAL( clicked() ),SLOT( createRow_Slot() ) )
submitButton=new QPushButton(tr(" Submit Row "));
connect(createButton,SIGNAL( clicked() ),SLOT( createRow_Slot() ) )
QVBoxLayout *mainLayout=new QVBoxLayout(this);
mainLayout->addWidget(view);
mainLayout->addWidget(createButton);
mainLayout->addWidget(submitButton);
}
To copy to clipboard, switch view to plain text mode
Buttons' slots might look like this:
void MainWidget::createRow_Slot()
{
if ( model->addRow( model->rowCount() ) )
view
->setEditTriggers
(QAbstractItemView::DoubleClicked);
// make sure that view will be editable once the row is added
else // call an error message or smth. to indicate a problem while inserting a new row
}
void MainWidget::submitRow_Slot()
{
view
->setEditTriggers
(QAbstractItemView::NoEditTriggers);
// once submitted,the view will be no longer editable
}
void MainWidget::createRow_Slot()
{
if ( model->addRow( model->rowCount() ) )
view->setEditTriggers(QAbstractItemView::DoubleClicked); // make sure that view will be editable once the row is added
else // call an error message or smth. to indicate a problem while inserting a new row
}
void MainWidget::submitRow_Slot()
{
view->setEditTriggers(QAbstractItemView::NoEditTriggers); // once submitted,the view will be no longer editable
}
To copy to clipboard, switch view to plain text mode
And finally you CustomModel class.The only method you need is "flags",so this is it:
{
if(index.row()== ( rowCount() -1 ) )
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; //only last row is editable
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
Qt::ItemFlags flags(const QModelIndex &index) const
{
if(index.row()== ( rowCount() -1 ) )
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; //only last row is editable
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
To copy to clipboard, switch view to plain text mode
Bookmarks