table_model
Paginated Qt table model with lazy loading support.
Tree-mode batch fetching
When a subtree is expanded, Qt calls fetchMore() for every child node that declares has_children=True. Those calls all arrive in the same event-loop tick. Without batching each call produces a separate async task and therefore a separate server round-trip.
Supply the optional fetch_page_batch callback to collapse all of those calls into a single round-trip::
def fetch_batch(
requests: list[BatchFetchRequest],
) -> dict[str | None, list[dict]]:
# One HTTP call for all parent_ids in the batch.
...
The model accumulates pending fetch requests during an event-loop tick, dispatches them as one :class:AsyncTask via a zero-delay QTimer.singleShot(0), and fans out the results to the per-node _on_page_ready handler when they arrive.
BatchFetchRequest dataclass
Describes a single child-page request within a batch fetch call.
A list of these is passed to the optional fetch_page_batch callback of :class:PaginatedTableModel. Each entry corresponds to one node whose children need to be fetched; the callback should return a dict mapping each parent_id to its list of row dicts.
Attributes:
| Name | Type | Description |
|---|---|---|
page | int | Page number (0-based). |
page_size | int | Maximum number of rows to return. |
sort_key | str | None | Column key for server-side sorting, or |
descending | bool |
|
parent_id | str | None | The |
Source code in client/ayon_ui_qt/components/table_model.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | |
PaginatedTableModel
Bases: QAbstractItemModel
A Qt model that lazily loads rows page-by-page via a callback.
Rows are fetched on demand using Qt's canFetchMore / fetchMore mechanism. Each call to fetchMore retrieves one page of data from the supplied fetch_page callable.
In flat mode (default) the model behaves like a plain table: no disclosure triangles, no nesting. In tree mode rows whose dict contains "has_children": True become expandable folders; expanding them triggers a fresh fetch_page call with the folder's "id" value passed as parent_id.
Batch fetching (tree mode only): when fetch_page_batch is supplied, all fetchMore() calls that arrive in the same event-loop tick (e.g. Qt calling fetchMore for every child of a just-expanded folder) are coalesced into a single :class:BatchFetchRequest list and dispatched as one async task. This reduces N sibling fetches from N server round-trips to one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fetch_page | Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]] | Callable with signature | required |
fetch_page_batch | Callable[[list[BatchFetchRequest]], dict[str | None, list[dict[str, Any]]]] | None | Optional callable with signature | None |
columns | list[TableColumn] | None | Column definitions. When | None |
page_size | int | Number of rows per page. | 50 |
no_async | bool | When | False |
parent | QObject | None | Optional parent QObject. | None |
Source code in client/ayon_ui_qt/components/table_model.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 | |
columns property
Return the current column definitions.
Returns:
| Type | Description |
|---|---|
list[TableColumn] | List of TableColumn instances. |
is_loading property
Return True while at least one fetch task is in-flight.
Returns:
| Type | Description |
|---|---|
bool | True if any page fetch is currently pending or running. |
page_count property
Return the number of root pages fetched so far.
Returns:
| Type | Description |
|---|---|
int | Current root page index. |
tree_position property
Return the current tree column index, or 0 if tree mode is off.
__init__(fetch_page, fetch_page_batch=None, columns=None, page_size=50, no_async=False, parent=None)
Initialise the model and fetch the first page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fetch_page | Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]] | Callable | required |
fetch_page_batch | Callable[[list[BatchFetchRequest]], dict[str | None, list[dict[str, Any]]]] | None | Optional batch callable | None |
columns | list[TableColumn] | None | Explicit column definitions, or | None |
page_size | int | Rows per page. | 50 |
no_async | bool | When | False |
parent | QObject | None | Parent QObject. | None |
Source code in client/ayon_ui_qt/components/table_model.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
canFetchMore(parent=QModelIndex())
Return whether more rows can be fetched for parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent | QModelIndex | QPersistentModelIndex | Parent index (invalid = root). | QModelIndex() |
Returns:
| Type | Description |
|---|---|
bool |
|
Source code in client/ayon_ui_qt/components/table_model.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
columnCount(parent=QModelIndex())
Return the number of columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent | QModelIndex | QPersistentModelIndex | Unused; column count is the same for all nodes. | QModelIndex() |
Returns:
| Type | Description |
|---|---|
int | Number of columns. |
Source code in client/ayon_ui_qt/components/table_model.py
374 375 376 377 378 379 380 381 382 383 384 385 386 | |
data(index, role=Qt.ItemDataRole.DisplayRole)
Return data for the given index and role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index | QModelIndex | QPersistentModelIndex | Model index identifying the cell. | required |
role | int | Qt item data role. | DisplayRole |
Returns:
| Type | Description |
|---|---|
Any | Cell value appropriate for the requested role, or |
Source code in client/ayon_ui_qt/components/table_model.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
fetchMore(parent=QModelIndex())
Fetch the next page of rows for parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent | QModelIndex | QPersistentModelIndex | Parent index (invalid = root). | QModelIndex() |
Source code in client/ayon_ui_qt/components/table_model.py
436 437 438 439 440 441 442 443 444 445 | |
get_distinct_values(key)
Return sorted distinct non-empty string values for a column.
In flat mode scans root-level rows; in tree mode scans all loaded nodes across all levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | str | Column key to inspect. | required |
Returns:
| Type | Description |
|---|---|
list[str] | Sorted list of unique string values found in loaded rows. |
Source code in client/ayon_ui_qt/components/table_model.py
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 | |
hasChildren(parent=QModelIndex())
Return whether the node at parent has or can have children.
Controls whether Qt draws a disclosure triangle even before children are loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent | QModelIndex | QPersistentModelIndex | Parent index (invalid = root). | QModelIndex() |
Returns:
| Type | Description |
|---|---|
bool | True if the node has loaded children, has more pages, or |
bool | (in tree mode only) the row data declares |
Source code in client/ayon_ui_qt/components/table_model.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | |
headerData(section, orientation, role=Qt.ItemDataRole.DisplayRole)
Return header data for the given section and orientation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section | int | Column (horizontal) or row (vertical) index. | required |
orientation | Orientation | Header orientation. | required |
role | int | Qt item data role. | DisplayRole |
Returns:
| Type | Description |
|---|---|
Any | Column label for horizontal DisplayRole, otherwise |
Source code in client/ayon_ui_qt/components/table_model.py
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
index(row, column, parent=QModelIndex())
Return a model index for row/column under parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row | int | Row number under parent. | required |
column | int | Column number. | required |
parent | QModelIndex | QPersistentModelIndex | Parent index (invalid = root). | QModelIndex() |
Returns:
| Type | Description |
|---|---|
QModelIndex | Valid QModelIndex, or invalid if out of range. |
Source code in client/ayon_ui_qt/components/table_model.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
parent(index)
Return the parent index of the given index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index | QModelIndex | QPersistentModelIndex | Child index. | required |
Returns:
| Type | Description |
|---|---|
QModelIndex | Parent QModelIndex, or invalid for root-level items. |
Source code in client/ayon_ui_qt/components/table_model.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | |
reset_data()
Reset the model and re-fetch from page 0.
Source code in client/ayon_ui_qt/components/table_model.py
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
rowCount(parent=QModelIndex())
Return the number of loaded children under parent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent | QModelIndex | QPersistentModelIndex | Parent index (invalid = root). | QModelIndex() |
Returns:
| Type | Description |
|---|---|
int | Number of loaded child rows. |
Source code in client/ayon_ui_qt/components/table_model.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | |
set_columns(columns)
Set the columns and reset the model from page 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns | list[TableColumn] | List of columns to display. | required |
Source code in client/ayon_ui_qt/components/table_model.py
591 592 593 594 595 596 597 598 | |
set_page(page)
Reset the model and begin fetching from the given page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
page | int | 0-based page number to start from. | required |
Source code in client/ayon_ui_qt/components/table_model.py
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
set_page_size(size)
Update the page size and reset the model from page 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size | int | New page size (rows per page). | required |
Source code in client/ayon_ui_qt/components/table_model.py
582 583 584 585 586 587 588 589 | |
set_tree_mode(enabled)
Switch between flat table mode and hierarchical tree mode.
Emits tree_mode_changed and reloads from page 0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
enabled | bool |
| required |
Source code in client/ayon_ui_qt/components/table_model.py
544 545 546 547 548 549 550 551 552 553 554 555 556 | |
sort(column, order=Qt.SortOrder.AscendingOrder)
Set the active sort column and order, then reload data.
Sorting always resets from page 0 at all levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column | int | Zero-based index of the column to sort by. If out of range, the call is ignored. | required |
order | SortOrder | Sort order (ascending or descending). | AscendingOrder |
Source code in client/ayon_ui_qt/components/table_model.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
TableColumn dataclass
Describes a single column in a PaginatedTableModel.
Attributes:
| Name | Type | Description |
|---|---|---|
key | str | Dictionary key used to look up cell values in row data. |
label | str | Display text shown in the header. |
width | int | Preferred column width hint in pixels. 0 means auto. |
sortable | bool | Whether the column can be sorted by clicking the header. |
icon | str | None | Optional material icon name shown in the filter dropdown. |
tree_position | bool | Whether the column is used for tree indentation. |
widget_factory | 'Callable[[Any, Any], Any] | None' | Optional callable |
Source code in client/ayon_ui_qt/components/table_model.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
make_hierarchical_test_fetch(data)
Create a fetch_page callback from hierarchical test data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str | None, list[dict[str, Any]]] | Mapping of parent_id -> list[row_dict]. | required |
Returns:
| Type | Description |
|---|---|
Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]] | A callable suitable for PaginatedTableModel in tree mode. |
Source code in client/ayon_ui_qt/components/table_model.py
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 | |
make_hierarchical_test_fetch_batch(data)
Create a fetch_page_batch callback from hierarchical test data.
Wraps :func:make_hierarchical_test_fetch so that several child fetch requests are resolved in one call, mimicking a batched server API. Use together with fetch_page_batch= on :class:PaginatedTableModel to exercise the batch code path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str | None, list[dict[str, Any]]] | Mapping of parent_id -> list[row_dict]. | required |
Returns:
| Type | Description |
|---|---|
Callable[[list[BatchFetchRequest]], dict[str | None, list[dict[str, Any]]]] | A callable suitable for |
Callable[[list[BatchFetchRequest]], dict[str | None, list[dict[str, Any]]]] | in tree mode. |
Source code in client/ayon_ui_qt/components/table_model.py
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 | |
make_test_fetch(data)
Create a flat fetch_page callback from static data.
parent_id is accepted but ignored — all data lives at root level.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | list[dict[str, Any]] | The full dataset to paginate. | required |
Returns:
| Type | Description |
|---|---|
Callable[[int, int, str | None, bool, str | None], list[dict[str, Any]]] | A callable suitable for PaginatedTableModel. |
Source code in client/ayon_ui_qt/components/table_model.py
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 | |