2010-03-22 25 views
10

我想在這樣一個特定的細胞進入編輯模式:QTableView:我如何正確創建一個QModelIndex?

void MainWindow::on_addButton_released() { 
    tm->addRow(); 
    tableView->scrollToBottom(); 
    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; 
} 

我的模型創建這樣一個指標:

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

調試輸出是這樣的:

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

我很確定索引是無效的,因爲setCurrentIndex()似乎沒有工作。 OMG!

+0

你'指數()'方法實際上並沒有覆蓋任何東西('QModelIndex'與'常量QModelIndex&')。複製粘貼錯誤? – 2012-06-24 08:08:00

回答

13

OMG!地面吞噬我!

行號從0開始的行,我需要做的

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

我經常用小工具工具箱忘記它,所以我儘量不要單獨使用'row'或'column'。相反,我使用'rowIndex'(基於零),更少見的是'rowNumber'(基於一個)。 – kevinarpe 2015-05-12 04:50:38

+0

'tm'必須是這裏的表模型? – oya163 2016-07-04 11:00:50

+0

是tm是表模型 – 2016-07-07 10:07:58

相關問題