关于获取QTableWidget item文本和判断item是否为空

itemAt(x,y) 是屏幕像素坐标
item(x,y)=itemAt(y*单元格高,x*单元格宽)
1.获取item文本
开始使用在网上查的方法ui->tableWidget->itemAt(x,y)->text()获取item的值,可以发现无论怎么变x和y的值,取出来的都是第一行第一列的item文本,后来上网上一查,原来要用item(x,y),而不是itemAt(x,y),即 ui->tableWidget->item(x,y)->text()。
根据qt文档说明:
item(x,y)—— Returns the item for the given row and column if one has been set
x是行号,y是列号。
itemAt(x,y)——Returns the item at the position equivalent to QPoint(ax, ay) in the table widget's coordinate system
x,y是tableWidget的坐标系统坐标,至于什么是坐标系统,我还不清楚。
2.判断item是否为空
错误的用法:
if(ui->tableWidget->item(x,y)->text().isEmpty)
{
    ...
}
这样用,调式模式运行到此处会出现“Signal received” The inferior stopped because it received a signal....就是那个Qt比较常见的错误。LineEdit控件这样判断文本是否为空是可以的,至于tableWidget为什么不行,我也不清楚,后来我试出了一种方法:
if(ui->tableWidget->item(x,y)==0)
{
    ...
}
这样是可以的。
以下是Qt文档的说明:
QTableWidgetItem * QTableWidget::item ( int row, int column ) const
Returns the item for the given row and column if one has been set; otherwise returns 0.

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值