QListView is asking the model for data but not displaying it.
-
I have a QListView and I've provided it with a QAbstractListModel descendant to serve it data.
I know that the model is called repeatedly for the row count upon being provided to the listview, and again after the model calls endResetModel() (I also call beginResetModel()). Its data() member is also called an appropriate number of times, and I return descriptive strings for each requested row.
But nothing is shown in the listview. I'm continuing to research this, but is there some typically-overlooked thing I might not be doing here? Thanks!
-
hi @Stokestack
did you maybe forget to give your delegate a hight and width? -
@J.Hilk Thanks for your reply.
Well... maybe. Height and width of what? The control itself is laid out on a form using the design tool. Based on your question, I'm looking for inquiries that request width & height but haven't found any yet...
-
Hi,
Can you show your model implementation ?
-
@SGaist Sure. Well, any part that's relevant to the listview. It's a pretty big class; because this listview is only to provide a display of detected Bluetooth devices, I've made my Bluetooth-finder class the model as well.
// Methods to support QAbstractListModel int DeviceFinder::rowCount(const QModelIndex& parent) const { return static_cast<int>(m_foundDevices.size()); } QVariant DeviceFinder::data(const QModelIndex& index, int role) const { shared_ptr<BTDevice> pThisDevice = m_foundDevices.at(static_cast<unsigned long int>(index.row())); qDebug() << "DeviceFinder::data returning " << pThisDevice->getUUID() << " for row " << index.row(); return pThisDevice->getUUID(); }
The debug output shows I'm getting legit strings.
-
@Stokestack
I'm sorry, I don't know why, but thought this to be in the QML sub forum.My previous post relates to the QML ListView object. My bad.
But, I think the issue might be similar.
To display data in a ListView you need 3 things,- A model
- A ListView
- A Delegate
In your op you mention point 1 and 2, what about the delegate, the object that is drawn inside your ListView and changes depending on the data in the model?
Did you touch that or is it the "default" one? -
@J.Hilk Thanks. It must be the default one, because I haven't seen any mention of a delegate in the docs.
Interestingly, if I double-click in the top of the list area where items should be, the data model will be queried for the appropriate row...
-
take a look here for more information
https://doc.qt.io/qt-5/modelview.html#3-4-delegates -
And you really must respect the given role to data() and not simply return getUUID() for all roles...
-
@J.Hilk Thanks. I don't think it's a delegate problem. I've done demos from an instructional book as well, and no delegates were used for this kind of listview.
The clicking behavior is pretty telling; the items are in the list and they occupy space. They're just invisible.
-
@Stokestack said in QListView is asking the model for data but not displaying it.:
They're just invisible.
Did you read my post?
-
@Christian-Ehrlicher Yes, and I was just about to go and see if the listview was requesting decoration info or something and getting messed up by the strings...
-
There is nothing to see - a view takes all it's information from the model. You simply return a value for every role instead just Qt::DisplayRole and wonder why it's displaying mess.
-
@Christian-Ehrlicher And sure enough that was it. Thanks Christian! I thought that, by default, it was just going to ask for strings for each "role" of each element, with the roles basically being equivalent to columns. I didn't really care if it showed the same string for every role, but if it got a string it didn't understand for the Decoration role... I see where that might disrupt the whole display.