2010-03-22 269 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

相关问题