MistyD
MistyD

Reputation: 17253

Displaying Vector in QListView

I wanted to know what was the best way displaying contents of a vector in a QlistView.I am fairly new to QT hence the question. The vector content changes very fast hence speed is essential. I know you could set models in QT like by setting models

QStringList list;
list << "item1" << "item2" << "item3" << "item4" << "item5";
ui.listView->setModel(new QStringListModel(list));

How would i do something like that with a vector or a deque type.

Upvotes: 0

Views: 2461

Answers (2)

Ilya Kobelevskiy
Ilya Kobelevskiy

Reputation: 5345

As Matt Philips have mentioned, it can be done by subclassing QAbstractListModel. However, simpler approach would be to convert QVector/std::vector to QList using built in QList static member:

ui.listView->setModel(new QStringListModel(QList<QString>::fromVector(yourQVector)));

or

ui.listView->setModel(new QStringListModel(QList<QString>::fromStdVector(yourStdVector)));

I doubt there would be high overhead due to that conversion, and if you can optimize significantly by sub classing QAbstractListModel...

Upvotes: 4

Matt Phillips
Matt Phillips

Reputation: 9691

You need to subclass QAbstractListModel. This will let you use any data structure you want. To do so, you will need to implement a couple pure virtual functions, rowCount() and data(). Qt's Model/View programming framework is nice but takes some getting used to, it's probably a good idea to read over their guide.

Upvotes: 1

Related Questions