0

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.

2
  • How fast is "very fast"? Commented Apr 2, 2013 at 17:54
  • Changes are made to the vector multiple times in a sec Commented Apr 2, 2013 at 17:58

2 Answers 2

4

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...

Sign up to request clarification or add additional context in comments.

1 Comment

so, are you telling me to "change" model every time I press my add button?
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.