The problem
So. I was stuck with a container of tuples that I wanted to see in a Qt view (QTableView, QtQuick ListView or similar). So how to do that?
Another problem: I haven’t been doing fun things with templates recently.
A solution?
After a bit of hacking, it seems like it can just be done like
1 2 3 4 5 6 7 |
typedef std::tuple<std::string, QString> Element; typedef std::vector List; List list = { std::make_tuple("first", "second"), std::make_tuple("third", "fourth") }; std::unique_ptr<TableModel> magic = createTableModel(list); QTableView view; view.setModel(magic->model()); |
Of course, we are also QtQuick friendly
1 2 |
std::unique_ptr<ListModel>List>> magic = createListModel(list); // expose magic->model() to your quickview |
and a delegate containing the following
1 2 3 4 5 6 |
Text { text: role0 } Text { text: role1 } |
But enough about creation.
Whattabout manipulation?
Luckily we got you covered. Insert two extra rows at position 1?
1 2 3 |
auto lines = { std::make_tuple("extra", "extra"), std::make_tuple("extra2","extra2") }; magic->insertRows(1,lines.begin(), lines.end()); |
Append a row?
1 |
magic->appendRow(std::make_tuple("","")); |
Remove 2 rows at position 3?
1 |
magic->removeRow(3,2); |
Replace the underlying list?
1 2 3 |
List newList; // fill list magic->reset(newList); |
Read-only looping over the elements?
1 2 3 4 |
for(const Element& e : magic->list()) { ... } |
The Qt model of course also accepts setData calls.
Future?
If anyone is interested I will polish the code a bit and publish it. If that’s the case, how should I name this thing?
And I did get around doing fun things with templates again.
Nice one. But maybe instead of typedef I*d prefer using:
using Element = std::tuple;