代码之家  ›  专栏  ›  技术社区  ›  Joseph

Qt/c++:在QTable视图中获取某个单元格中的数据

  •  16
  • Joseph  · 技术社区  · 15 年前

    我想把短信发到某个手机里 QTableView . 例如:

    QString codestring = "*" + ui->tblInventory->indexAt(QPoint(0,2)).data().toString() + "*";
    

    这将在我的 Q表视图 . 问题是,这不是它在做什么!。不管我传递给 QPoint() indexAt() ,我在0,0号单元格获取文本。我不知道这是为什么。。。有什么帮助吗?谢谢!

    [编辑]
    我也试过:

    QString codestring = "*" + ui->tblInventory->model()->data(ui->tblInventory->indexAt(QPoint(0,2))).toString() + "*";
    

    [编辑2] 为了找出发生了什么,我输入了以下代码:

    qDebug()<< ui->tblInventory->indexAt(QPoint(2,2)).row() << " and " <<  ui->tblInventory->indexAt(QPoint(2,2)).column();
    

    它应该得到 QModelIndex 在单元格2,2处输出其行和列,当然应该是2和2。但是,我得到了0和0!所以看起来这可能是个问题 QTableView::indexAt() ,无论是我的用法还是某种错误。有人能照一下吗?

    4 回复  |  直到 10 年前
        1
  •  26
  •   Joseph    15 年前

    解决方式:

    ui->tblInventory->model()->data(ui->tblInventory->model()->index(0,2)).toString()
    

    不太清楚为什么上面的方法不起作用,但这确实起作用。谢谢你的帮助。

        2
  •  9
  •   Mahir Zukic    13 年前

    这个也能用,而且更短:

    QModelIndex index = model->index(row, col, QModelIndex());
    
    ui->tblInventory->model()->data(index).toString();
    

    ( model used top是与此绑定的QAbstractModel tblInventory )

        3
  •  0
  •   dpq    15 年前

    检查 data() 由QTableView使用的模型提供的函数,您描述的效果可能是由于其中的错误而观察到的。

        4
  •  0
  •   SIFE    14 年前

    试试这个:

    QModelIndex index = ui->tblInventory->indexAt(p); // p is a QPoint you get from some where, may be you catch right click
    QString codestring = "*" + index->data().toString() + "*";
    
    推荐文章