QWidget iterator
-
Hello,
I'm working on my project to update code written in 2001. I'm having trouble with a function that uses a QListViewItemIterator:
//////////////////////////////////// //// Write each channel to file /// //////////////////////////////////// bool BookmarkData::WriteCycle() { BookmarkItem *bm; bookmark_t data; QListViewItemIterator it ( bmList ); //<- error: unknown type name "QListViewItemIterator" // iterate through all items in the list for( ; it.current(); ++it){ bm = (BookmarkItem *)it.current(); bm->getBookmark( &data ); // add number of child in case of folder if(bm->isFolderBegin()){ data.freq = bm->childCount(); } WriteEntry( &data ); } return true; }BookmarkItem was a QListViewItem and I changed to QListWidgetItem.
bmList is a QListView*.I'm not quite sure how to accomplish the same functionality as the original code. I haven't used iterators and would appreciate some advice on how to get this working in modern Qt.
-
Hello,
I'm working on my project to update code written in 2001. I'm having trouble with a function that uses a QListViewItemIterator:
//////////////////////////////////// //// Write each channel to file /// //////////////////////////////////// bool BookmarkData::WriteCycle() { BookmarkItem *bm; bookmark_t data; QListViewItemIterator it ( bmList ); //<- error: unknown type name "QListViewItemIterator" // iterate through all items in the list for( ; it.current(); ++it){ bm = (BookmarkItem *)it.current(); bm->getBookmark( &data ); // add number of child in case of folder if(bm->isFolderBegin()){ data.freq = bm->childCount(); } WriteEntry( &data ); } return true; }BookmarkItem was a QListViewItem and I changed to QListWidgetItem.
bmList is a QListView*.I'm not quite sure how to accomplish the same functionality as the original code. I haven't used iterators and would appreciate some advice on how to get this working in modern Qt.
@MScottM You can iterate ovet items in QListWidget using https://doc.qt.io/qt-6/qlistwidget.html#itemAt-1
-
@jsulm Qt is telling me: 'No member named 'itemAt in QListWidgetItem' - what am I doing wrong?
QListWidgetItem* it;then,
it->itemAt(x) // No member... -
QListWidgetItem is an item type of QListWidget. QListWidget is a convenience class that combines a view and a model
Since
bmListis a QListView you can't use QListWidgetItem with it. QListView is a view class. There is no way to get an item because there are no items in it. It operates only on an abstract model. You can callmodel()to get it. It is of typeQAbstractItemModel. From it you can get rowCount(), then in a loop get index() for each row and for that index you can read data().