开发者

QTableView: how do I correctly create a QModelIndex?

I'm trying to enter edit mode on a specific cell like this:

void MainWindow::on_addButton_released() {
    tm->addRow();
    tableView->scrollToBott开发者_StackOverflowom();
    int ec=tm->firstWritableColumn();
    int r=tm->rowCount(QModelIndex());
    QModelIndex id = tm->index(r, ec, QModelIndex());
    tableView->setCurrentIndex(id);
    tableView->edit(id);
    qDebug() << "row:" << r << " col:" << ec << "index:" << id;
}

My model creates an index like this:

QModelIndex TableModel::index(int row,int column,QModelIndex parent) const {
    Q_UNUSED(parent);
    return createIndex(row,column,0);
}

The debug output looks like this:

row: 9  col: 1 index: QModelIndex(9,1,0x0,TableModel(0xbf3f50) )  

I'm fairly sure that the index is somehow invalid as setCurrentIndex() doesn't seem to be working.


OMG! ground swallow me up!

row numbers start with row 0, I needed to do

int r=tm->rowCount(QModelIndex())-1;
QModelIndex id=tm->index(r,ec,QModelIndex());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜