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! } }
