A Qt Model for all C++ Ranges
8 points by raymii
8 points by raymii
While the introduction of QRangeModel was exciting, the implementation leaves much to be desired. The use of templates as constructors poses a challenge in getting this data structure across language boundaries. On one hand, there is a stated goal of the Qt Company to embrace other languages and on the other, language conventions that are not portable work against that goal. It would be great if non-templated constructors could be introduced for QRangeModel while 6.10 is still in the beta or even in the next point release if possible.
The use of templates as constructors poses a challenge in getting this data structure across language boundaries
but the point of QRangeModel is explicitly to provide a helper type useful for generic C++ types (likely 90%+ of code using Qt) ; there’s zero reason for it to care about other languages interop ; it shouldn’t even be exposed over dll boundaries. Other languages should write their own QRangeModel-like type that will be adapted to their own generic collection types, that’s the only purpose of QRangeModel in C++ - working with std::vector and the likes.
the point of QRangeModel is explicitly to provide a helper type useful for generic C++ types
The point was to simplify the initialization of models.
From the demonstration code:
int main(int argc, char **argv) {
QApplication app(argc, argv);
std::vector<int> data = { 1, 2, 3, 4, 5 };
QRangeModel model(data);
QListView view;
view.setModel(&model);
view.show();
return app.exec();
}
This reduces much of the boilerplate usually required. The data
variable in this case can be implemented fairly trivially in most languages but why wouldn’t it make sense for the QRangeModel constructor to be available over the language boundary? This could have been constructed similar to how QVariant is handled. There are templated constructors but most of the constructors are currently not templated. As a result, it’s possible to get QVariant across language boundaries.
QRangeModel is meant to be a convenience model. Shouldn’t that convenience be extended to all consumers of Qt? Often times, working with bindings means working with pointers to heap-allocated memory in Qt. Having more native ways to operate on the data behind those pointers means less currying of data for consumers using other languages than C++, resulting in better performing applications.
Other languages should write their own QRangeModel-like type that will be adapted to their own generic collection types
From the QRangeModel documentation:
The range must be provided when constructing the model;
there is no API to set the range later, and there is no API
to retrieve the range from the model.
The current implementation is basic but still capable. I hope it isn’t asking for too much to have a few constructors defined for common types/containers that will survive the language boundary. I don’t have the strongest comfort with C++ but I would even be willing to contribute it if it would be accepted upstream.
Consumers would love a chance to convert this:
QStringList data = {"one", "two", "three"};
QRangeModel model(&data);
into this:
const char* data[] = {"one", "two", "three", NULL};
QRangeModel* model = q_rangemodel_new(data);
or this:
data := []string{"one", "two", "three"};
model := qt.NewQRangeModel(data);
or this:
var data = [_][]const u8{ "one", "two", "three" };
const model = qrangemodel.New(&data);