What is the best way to make table from ListView
?
Say, given a 2d array of strings and delegate
for all the columns are Label
s. How and when to calculate maximum item width for each column while using only QML? Content of each Label
is not constant (i.e. implicitWidth
is mutable during lifetime).
Practical reason to invent the TableView
is the fact, that 1 step to TreeView
will remain.
Questions about creating tables in QML seem to get posted fairly frequently, but I am yet to see an answer compiling all the different options. There are lots of ways to achieve what you are asking. I hope to provide in this answer a number of alternatives.
TableView (5.12 and later)
(Updated 14 Jan 2019)
Qt 5.12 includes a new Qt Quick item called
TableView
, which has been redesigned from the ground up to have good performance for a data model with any number of rows or columns. It resolves the performance problems which were present in the previousTableView
from`Quick Controls 1.I cannot provide a usage example for this approach, because I haven't used it yet (I am still using Qt 5.9 for my projects), but there are examples in the Qt documentation for 5.12 and later.
If you are using Qt 5.12, and you know that you will need both horizontal scrolling and vertical scrolling for your table (there are more rows AND columns than can fit in the view), then this would seem to be the first choice solution.
Below are a summary of alternative approaches for Qt 5.11 and earlier, or if for some reason you do not want to use the Qt 5.12
TableView
(perhaps one of these alternative approaches better suits your data model?).GridLayout
Vertical ListView
Creating a table with the Vertical
ListView
has its advantages and disadvantages. Pros:Cons:
ListView
(which is usually what people want), dynamic column width is difficult to achieve... i.e. column width is set to completely fit all values in the columnColumn widths must be calculated using a loop over all the model data inside that column, which could be slow and is not something you would want to perform often (for example if user can modify cell contents and you want the column to resize).
A reasonable compromise can be achieved by only calculating the column widths once, when the model is assigned to the
ListView
, and having a mixture of fixed-width and calculated-width columns.Warning: Below is an example of calculating column widths to fit longest text. If you have a large model, you should consider scrapping the Javascript loop and resort to fixed width columns (or fixed proportions relative to the view size).
TableView (5.11 and earlier)
(from Quick Controls 1)
QC1 has a
TableView
component. QC2 does not (in Qt 5.9). There is one in development, but with no guaranteed timescale.TableView
has been unpopular due to performance issues, but it did receive improvements between Quick Controls 1.0 to 1.4, and it remains a useable component. QC1 and QC2 can be mixed in the same application.Pros
ListView
, so handles large numbers of rows well.QTableView
from WidgetsCons
ListView
.Example: