currentRowChanged 的注意事项

Qt中QListWidget的currentRowChanged详解
本文详细介绍了Qt中的QListWidget类提供的currentRowChanged信号的使用方法,特别是如何正确处理没有当前项时的特殊情况。

 

Qt中的表单控件QListWidget类提供了许多信号函数,可以和用户交互,其中有个currentRowChanged ( int currentRow ) 是检测当前选中行是否发生了改变,如果改变了,该信号函数被触发。

void QListWidget::currentRowChanged ( int currentRow ) [signal]

This signal is emitted whenever the current item changes.
currentRow is the row of the current item. If there is no current item, the currentRow is -1.

 

我们要注意的是最后一句话,当没有当前项时,currentRow 赋值为-1,由于有这点存在,所以再写该信号函数的内容开始,一定要先判断currentRow的值是否大于等于0,若忽略了这一步,在下面直接用currentRow当做参数取访问数组时会出错,而通常这错误得费老半天劲才能找的出来,正确写法如下:

 

void YourClass::on_lwidget_currentRowChanged(int currentRow) {
    // Note: lwdiget is the object name of listwidget
    if (currentRow >= 0) {
        // Implement here!
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值