| 8 | #include "T_TableViewModel.h" |
| 9 | |
| 10 | #include <QHeaderView> |
| 11 | #include <QItemSelectionModel> |
| 12 | #include <QSortFilterProxyModel> |
| 13 | #include <QVBoxLayout> |
| 14 | |
| 15 | T_TableView::T_TableView(QWidget* parent) |
| 16 | : T_BasePage(parent) |
| 17 | { |
| 18 | setWindowTitle("表格视图"); |
| 19 | createCustomWidget("在多列数据中搜索、排序与选择,拖动表头边界可调整列宽。", "表格视图"); |
| 20 | auto* mainLayout = createPageLayout(); |
| 21 | auto* section = addExampleSection(mainLayout, "音乐资料库 · ElaTableView", "输入歌名、歌手或专辑筛选,点击列标题切换排序。"); |
| 22 | auto* searchEdit = new ElaLineEdit(this); |
| 23 | searchEdit->setFixedWidth(280); |
| 24 | searchEdit->setPlaceholderText("搜索歌名、歌手或专辑"); |
| 25 | searchEdit->setClearButtonEnabled(true); |
| 26 | auto* alternatingCheckBox = new ElaCheckBox("交替行背景", this); |
| 27 | alternatingCheckBox->setFixedHeight(searchEdit->height()); |
| 28 | alternatingCheckBox->setChecked(true); |
| 29 | auto* optionsLayout = new ElaFlowLayout(0, 20, 12); |
| 30 | optionsLayout->addWidget(searchEdit); |
| 31 | optionsLayout->addWidget(alternatingCheckBox); |
| 32 | _tableView = new ElaTableView(this); |
| 33 | auto* libraryModel = new T_TableViewModel(this); |
| 34 | auto* filterModel = new T_MusicFilterModel(this); |
| 35 | filterModel->setSourceModel(libraryModel); |
| 36 | filterModel->setFilterKeyColumn(-1); |
| 37 | filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); |
| 38 | _tableView->setModel(filterModel); |
| 39 | _tableView->setAlternatingRowColors(true); |
| 40 | // 初始保持源模型顺序 点击表头后再按对应列排序 |
| 41 | _tableView->horizontalHeader()->setSortIndicator(-1, Qt::AscendingOrder); |
| 42 | _tableView->setSortingEnabled(true); |
| 43 | _tableView->setIconSize(QSize(32, 32)); |
| 44 | _tableView->verticalHeader()->hide(); |
| 45 | _tableView->verticalHeader()->setDefaultSectionSize(44); |
| 46 | _tableView->horizontalHeader()->setMinimumSectionSize(48); |
| 47 | _tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); |
| 48 | _tableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); |
| 49 | _tableView->setColumnWidth(0, 52); |
| 50 | _tableView->setColumnWidth(2, 130); |
| 51 | _tableView->setColumnWidth(3, 150); |
| 52 | _tableView->setColumnWidth(4, 68); |
| 53 | _tableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 54 | _tableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 55 | _tableView->setFixedHeight(380); |
| 56 | auto* resultText = new ElaText("点击一行查看选择结果", this); |
| 57 | resultText->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); |
| 58 | connect(searchEdit, &ElaLineEdit::textChanged, filterModel, &QSortFilterProxyModel::setFilterFixedString); |
| 59 | connect(alternatingCheckBox, &ElaCheckBox::toggled, _tableView, &ElaTableView::setAlternatingRowColors); |
| 60 | connect(_tableView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, [resultText](const QModelIndex& current) { |
nothing calls this directly
no test coverage detected