【问题标题】:QTableView: how do I correctly create a QModelIndex?QTableView:如何正确创建 QModelIndex?
【发布时间】:2011-01-30 08:26:45
【问题描述】:

我正在尝试在特定单元格上进入编辑模式,如下所示:

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() 似乎不起作用。

【问题讨论】:

  • 您的index() 方法实际上并没有覆盖任何东西(QModelIndex 与const QModelIndex &amp;)。复制粘贴错误?

标签: qt qtableview qmodelindex


【解决方案1】:

天哪!地面吞噬我!

行号从第 0 行开始,我需要这样做

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

【讨论】:

  • 我经常使用小部件工具包忘记这一点,所以我尽量不要单独使用row 或column。相反,我使用rowIndex(用于从零开始),并且很少使用rowNumber(用于从一开始)。
  • 'tm' 一定是这里的表模型??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多